diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/hashie/extensions/mash/symbolize_keys.rb | 10 | ||||
-rw-r--r-- | lib/hashie/extensions/symbolize_keys.rb | 13 | ||||
-rw-r--r-- | lib/hashie/hash.rb | 4 | ||||
-rw-r--r-- | lib/hashie/mash.rb | 19 | ||||
-rw-r--r-- | lib/hashie/version.rb | 2 |
5 files changed, 29 insertions, 19 deletions
diff --git a/lib/hashie/extensions/mash/symbolize_keys.rb b/lib/hashie/extensions/mash/symbolize_keys.rb index 91a6b14..c0f2a4d 100644 --- a/lib/hashie/extensions/mash/symbolize_keys.rb +++ b/lib/hashie/extensions/mash/symbolize_keys.rb @@ -5,7 +5,7 @@ module Hashie # # @example # class LazyResponse < Hashie::Mash - # include Hashie::Extensions::Mash::SymbolizedKeys + # include Hashie::Extensions::Mash::SymbolizeKeys # end # # response = LazyResponse.new("id" => 123, "name" => "Rey").to_h @@ -24,13 +24,13 @@ module Hashie private - # Converts a key to a symbol + # Converts a key to a symbol, if possible # # @api private - # @param [String, Symbol] key the key to convert to a symbol - # @return [void] + # @param [<K>] key the key to attempt convert to a symbol + # @return [Symbol, K] def convert_key(key) - key.to_sym + key.respond_to?(:to_sym) ? key.to_sym : key end end end diff --git a/lib/hashie/extensions/symbolize_keys.rb b/lib/hashie/extensions/symbolize_keys.rb index e4315bd..d1da9cb 100644 --- a/lib/hashie/extensions/symbolize_keys.rb +++ b/lib/hashie/extensions/symbolize_keys.rb @@ -46,7 +46,7 @@ module Hashie hash.extend(Hashie::Extensions::SymbolizeKeys) unless hash.respond_to?(:symbolize_keys!) hash.keys.each do |k| # rubocop:disable Performance/HashEachMethods symbolize_keys_recursively!(hash[k]) - hash[k.to_sym] = hash.delete(k) + hash[convert_key(k)] = hash.delete(k) end hash end @@ -61,6 +61,17 @@ module Hashie symbolize_keys!(new_hash) end end + + private + + # Converts a key to a symbol, if possible + # + # @api private + # @param [<K>] key the key to attempt convert to a symbol + # @return [Symbol, K] + def convert_key(key) + key.respond_to?(:to_sym) ? key.to_sym : key + end end class << self diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb index 7a97ef6..a2bd7ab 100644 --- a/lib/hashie/hash.rb +++ b/lib/hashie/hash.rb @@ -21,8 +21,8 @@ module Hashie assignment_key = if options[:stringify_keys] k.to_s - elsif options[:symbolize_keys] - k.to_s.to_sym + elsif options[:symbolize_keys] && k.respond_to?(:to_sym) + k.to_sym else k end diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 96b24c9..6ab6e9a 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -121,8 +121,8 @@ module Hashie alias regular_reader [] alias regular_writer []= - # Retrieves an attribute set in the Mash. Will convert - # any key passed in to a string before retrieving. + # Retrieves an attribute set in the Mash. Will convert a key passed in + # as a symbol to a string before retrieving. def custom_reader(key) default_proc.call(self, key) if default_proc && !key?(key) value = regular_reader(convert_key(key)) @@ -130,14 +130,12 @@ module Hashie value end - # Sets an attribute in the Mash. Key will be converted to - # a string before it is set, and Hashes will be converted - # into Mashes for nesting purposes. + # Sets an attribute in the Mash. Symbol keys will be converted to + # strings before being set, and Hashes will be converted into Mashes + # for nesting purposes. def custom_writer(key, value, convert = true) #:nodoc: - key_as_symbol = (key = convert_key(key)).to_sym - - log_built_in_message(key_as_symbol) if log_collision?(key_as_symbol) - regular_writer(key, convert ? convert_value(value) : value) + log_built_in_message(key) if key.respond_to?(:to_sym) && log_collision?(key.to_sym) + regular_writer(convert_key(key), convert ? convert_value(value) : value) end alias [] custom_reader @@ -371,7 +369,7 @@ module Hashie end def convert_key(key) #:nodoc: - key.to_s + key.respond_to?(:to_sym) ? key.to_s : key end def convert_value(val, duping = false) #:nodoc: @@ -406,6 +404,7 @@ module Hashie end def log_collision?(method_key) + return unless method_key.is_a?(String) || method_key.is_a?(Symbol) return unless respond_to?(method_key) _, suffix = method_name_and_suffix(method_key) diff --git a/lib/hashie/version.rb b/lib/hashie/version.rb index b97bf9b..e4ffc63 100644 --- a/lib/hashie/version.rb +++ b/lib/hashie/version.rb @@ -1,3 +1,3 @@ module Hashie - VERSION = '4.1.0'.freeze + VERSION = '5.0.0'.freeze end |