module MultiJson::ConvertibleHashKeys
Constants
- SIMPLE_OBJECT_CLASSES
Private Instance Methods
Source
# File lib/multi_json/convertible_hash_keys.rb, line 37 def handle_array(array, &key_modifier) array.map { |value| prepare_hash(value, &key_modifier) } end
Source
# File lib/multi_json/convertible_hash_keys.rb, line 41 def handle_hash(original_hash, &key_modifier) original_hash.each_with_object({}) do |(key, value), result| result[key_modifier.call(key)] = prepare_hash(value, &key_modifier) end end
Source
# File lib/multi_json/convertible_hash_keys.rb, line 31 def handle_simple_objects(obj) return obj if simple_object?(obj) || obj.respond_to?(:to_json) obj.respond_to?(:to_s) ? obj.to_s : obj end
Source
# File lib/multi_json/convertible_hash_keys.rb, line 20 def prepare_hash(value, &) case value when Array handle_array(value, &) when Hash handle_hash(value, &) else handle_simple_objects(value) end end
Source
# File lib/multi_json/convertible_hash_keys.rb, line 47 def simple_object?(obj) SIMPLE_OBJECT_CLASSES.any? { |klass| obj.is_a?(klass) } end
Source
# File lib/multi_json/convertible_hash_keys.rb, line 14 def stringify_keys(hash) prepare_hash(hash) do |key| key.respond_to?(:to_s) ? key.to_s : key end end
Source
# File lib/multi_json/convertible_hash_keys.rb, line 8 def symbolize_keys(hash) prepare_hash(hash) do |key| key.respond_to?(:to_sym) ? key.to_sym : key end end