module Hashie::Extensions::Mash::KeepOriginalKeys
Overrides the indifferent access of a Mash to keep keys in the original format given to the Mash.
@example
class KeepingMash < Hashie::Mash include Hashie::Extensions::Mash::KeepOriginalKeys end mash = KeepingMash.new(:symbol_key => :symbol, 'string_key' => 'string') mash.to_hash #=> { :symbol_key => :symbol, 'string_key' => 'string' } mash['string_key'] == mash[:string_key] #=> true mash[:symbol_key] == mash['symbol_key'] #=> true
Public Class Methods
included(descendant)
click to toggle source
@example Preserve symbol keys when including the extension
klass = Class.new(Hashie::Mash) do include Hashie::Extensions::Mash::KeepOriginalKeys end klass.new(name: 'Ada').keys # => [:name]
# File lib/hashie/extensions/mash/keep_original_keys.rb, line 24 def self.included(descendant) error_message = "`#{descendant}` is not a kind of Hashie::Mash" raise ArgumentError, error_message unless descendant <= Hashie::Mash end
Private Instance Methods
__convert(key)
click to toggle source
Converts symbol/string keys to their alternative formats, but leaves other keys alone.
@param [Object, String, Symbol] key the key to convert. @return [Object, String, Symbol] the converted key.
# File lib/hashie/extensions/mash/keep_original_keys.rb, line 47 def __convert(key) case key when Symbol then key.name when String then key.to_sym else key end end
convert_key(key)
click to toggle source
Converts the key when necessary to access the correct Mash key.
@param [Object, String, Symbol] key the key to access. @return [Object] the value assigned to the key.
# File lib/hashie/extensions/mash/keep_original_keys.rb, line 35 def convert_key(key) return key if regular_key?(key) converted_key = __convert(key) converted_key && regular_key?(converted_key) ? converted_key : key end