diff options
| author | Michael Herold <michael.j.herold@gmail.com> | 2017-02-19 23:31:10 -0600 |
|---|---|---|
| committer | Michael Herold <michael.j.herold@gmail.com> | 2017-02-20 07:58:24 -0600 |
| commit | 5fa3ebc13218e961455cfe3c75220a8bc064a702 (patch) | |
| tree | 5e79bf4fad585bf5e2d45b743fb05c6bb67293c1 /lib/hashie/extensions | |
| parent | cc359a22b66e910f3dfc66d6ebb24b3230b1486f (diff) | |
| download | hashie-5fa3ebc13218e961455cfe3c75220a8bc064a702.tar.gz | |
Add Hashie::Extensions::Mash::SymbolizeKeys
We often have requests to make Mash symbolize keys by default. Since
Hashie is used across so many different version of Ruby, we have been
hesitant to make this behavior the default. However, there are valid use
cases for wanting symbol keys.
To satisfy both the needs of those on older Rubies and the needs of
those who want symbol keys, this extension gives the end-user the
ability to toggle on symbolized keys in their Mash subclasses. By adding
this ability, we can wait to implement the symbol keys as a default for
a while longer.
See #341, #342 for more information. This is a half-measure toward the
implementation of #342 (which makes Mash symbolize keys by default).
Diffstat (limited to 'lib/hashie/extensions')
| -rw-r--r-- | lib/hashie/extensions/mash/symbolize_keys.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/hashie/extensions/mash/symbolize_keys.rb b/lib/hashie/extensions/mash/symbolize_keys.rb new file mode 100644 index 0000000..4e1d2be --- /dev/null +++ b/lib/hashie/extensions/mash/symbolize_keys.rb @@ -0,0 +1,38 @@ +module Hashie + module Extensions + module Mash + # Overrides Mash's default behavior of converting keys to strings + # + # @example + # class LazyResponse < Hashie::Mash + # include Hashie::Extensions::Mash::SymbolizedKeys + # end + # + # response = LazyResponse.new("id" => 123, "name" => "Rey").to_h + # #=> {id: 123, name: "Rey"} + # + # @api public + module SymbolizeKeys + # Hook for being included in a class + # + # @api private + # @return [void] + # @raise [ArgumentError] when the base class isn't a Mash + def self.included(base) + fail ArgumentError, "#{base} must descent from Hashie::Mash" unless base <= Hashie::Mash + end + + private + + # Converts a key to a symbol + # + # @api private + # @param [String, Symbol] key the key to convert to a symbol + # @return [void] + def convert_key(key) + key.to_sym + end + end + end + end +end |
