module Hashie::Extensions::IndifferentAccess
IndifferentAccess gives you the ability to not care whether your hash has string or symbol keys. Made famous in Rails for accessing query and POST parameters, this is a handy tool for making sure your hash has maximum utility.
One unique feature of this mixin is that it will recursively inject itself into sub-hash instances without modifying the actual class of the sub-hash.
@example
class MyHash < Hash include Hashie::Extensions::MergeInitializer include Hashie::Extensions::IndifferentAccess end h = MyHash.new(:foo => 'bar', 'baz' => 'blip') h['foo'] # => 'bar' h[:foo] # => 'bar' h[:baz] # => 'blip' h['baz'] # => 'blip'
Public Class Methods
# File lib/hashie/extensions/indifferent_access.rb, line 52 def [](*) super.convert! end
@api private
# File lib/hashie/extensions/indifferent_access.rb, line 29 def self.convert_key(key) return key.name if key.is_a?(Symbol) key.to_s end
# File lib/hashie/extensions/indifferent_access.rb, line 35 def self.included(base) Hashie::Extensions::Dash::IndifferentAccess.maybe_extend(base) base.class_eval do alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) alias_method :[]=, :indifferent_writer alias_method :store, :indifferent_writer %w[default update replace fetch delete key? values_at].each do |m| alias_method "regular_#{m}", m unless method_defined?("regular_#{m}") alias_method m, "indifferent_#{m}" end %w[include? member? has_key?].each do |key_alias| alias_method key_alias, :indifferent_key? end class << self def [](*) super.convert! end def try_convert(*) (hash = super) && self[hash] end end end end
Injects indifferent access into a duplicate of the hash provided. See inject!
@example Leave the original hash's top-level keys unchanged
original = { name: 'Ada' } hash = Hashie::Extensions::IndifferentAccess.inject(original) hash['name'] # => 'Ada' original # => { name: 'Ada' }
# File lib/hashie/extensions/indifferent_access.rb, line 85 def self.inject(hash) inject!(hash.dup) end
This will inject indifferent access into an instance of a hash without modifying the actual class. This is what allows IndifferentAccess to spread to sub-hashes.
@example Convert an existing hash in place
hash = { user: { name: 'Ada' } } Hashie::Extensions::IndifferentAccess.inject!(hash) hash['user'][:name] # => 'Ada' hash.class # => Hash
# File lib/hashie/extensions/indifferent_access.rb, line 72 def self.inject!(hash) (class << hash; self; end).send :include, IndifferentAccess hash.convert! end
# File lib/hashie/extensions/indifferent_access.rb, line 56 def try_convert(*) (hash = super) && self[hash] end
Public Instance Methods
Iterates through the keys and values, reconverting them to their proper indifferent state. Used when IndifferentAccess is injecting itself into member hashes.
@example Normalize keys inserted through the original writer
hash = Hashie::Extensions::IndifferentAccess.inject({}) hash.regular_writer(:name, 'Ada') hash.convert! hash.keys # => ['name'] hash[:name] # => 'Ada'
# File lib/hashie/extensions/indifferent_access.rb, line 106 def convert! keys.each do |k| # rubocop:disable Performance/HashEachMethods indifferent_writer k, regular_delete(k) end self end
@example Normalize symbol keys
hash = Hashie::Extensions::IndifferentAccess.inject({}) hash.convert_key(:name) # => 'name'
# File lib/hashie/extensions/indifferent_access.rb, line 92 def convert_key(key) IndifferentAccess.convert_key(key) end
@example Exclude keys using either representation
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada', age: 42 }) hash.except(:age) # => { 'name' => 'Ada' }
# File lib/hashie/extensions/indifferent_access.rb, line 258 def except(*keys) string_keys = keys.map { |key| convert_key(key) } super(*string_keys) end
@example Identify an indifferent hash
hash = Hashie::Extensions::IndifferentAccess.inject({}) hash.indifferent_access? # => true
# File lib/hashie/extensions/indifferent_access.rb, line 192 def indifferent_access? true end
@example Return an existing value or the configured default
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada' }) hash.default = 'unknown' hash.indifferent_default(:name) # => 'Ada' hash.indifferent_default(:missing) # => 'unknown'
# File lib/hashie/extensions/indifferent_access.rb, line 132 def indifferent_default(key = nil) return self[convert_key(key)] if key?(key) regular_default(key) end
@example Delete a string key through a symbol
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada' }) hash.indifferent_delete(:name) # => 'Ada' hash # => {}
# File lib/hashie/extensions/indifferent_access.rb, line 170 def indifferent_delete(key) regular_delete convert_key(key) end
@example Fetch with either key type and support fallbacks
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada' }) hash.indifferent_fetch(:name) # => 'Ada' hash.indifferent_fetch(:missing, 'unknown') # => 'unknown'
# File lib/hashie/extensions/indifferent_access.rb, line 162 def indifferent_fetch(key, ...) regular_fetch(convert_key(key), ...) end
@example Recognize both key representations
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada' }) hash.indifferent_key?(:name) # => true hash.indifferent_key?('name') # => true
# File lib/hashie/extensions/indifferent_access.rb, line 178 def indifferent_key?(key) regular_key? convert_key(key) end
@example Replace existing data and convert incoming keys
hash = Hashie::Extensions::IndifferentAccess.inject({ old: true }) hash.indifferent_replace(name: 'Ada') hash # => { 'name' => 'Ada' }
# File lib/hashie/extensions/indifferent_access.rb, line 200 def indifferent_replace(other_hash) (keys - other_hash.keys).each { |key| delete(key) } other_hash.each { |key, value| self[key] = value } self end
@example Normalize incoming keys and nested values
hash = Hashie::Extensions::IndifferentAccess.inject({}) hash.indifferent_update(user: { name: 'Ada' }) hash['user']['name'] # => 'Ada'
# File lib/hashie/extensions/indifferent_access.rb, line 142 def indifferent_update(other_hash) return regular_update(other_hash) if hash_with_indifference?(other_hash) other_hash.each_pair do |k, v| self[k] = v end end
@example Make hashes nested in arrays indifferent
hash = Hashie::Extensions::IndifferentAccess.inject({}) value = hash.indifferent_value([{ name: 'Ada' }]) value.first['name'] # => 'Ada'
# File lib/hashie/extensions/indifferent_access.rb, line 117 def indifferent_value(value) if hash_lacking_indifference?(value) IndifferentAccess.inject!(value) elsif value.is_a?(::Array) value.replace(value.map { |e| indifferent_value(e) }) else value end end
@example Retrieve values using mixed key types
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada', age: 42 }) hash.indifferent_values_at(:name, 'age', :missing) # => ['Ada', 42, nil]
# File lib/hashie/extensions/indifferent_access.rb, line 185 def indifferent_values_at(*indices) indices.map { |i| self[i] } end
@example Store a symbol key as a string
hash = Hashie::Extensions::IndifferentAccess.inject({}) hash.indifferent_writer(:name, 'Ada') hash # => { 'name' => 'Ada' }
# File lib/hashie/extensions/indifferent_access.rb, line 154 def indifferent_writer(key, value) regular_writer convert_key(key), indifferent_value(value) end
@example Merge without changing the original
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada' }) merged = hash.merge(age: 42) merged[:age] # => 42 merged.indifferent_access? # => true hash.key?(:age) # => false
# File lib/hashie/extensions/indifferent_access.rb, line 212 def merge(*args) result = super return IndifferentAccess.inject!(result) if hash_lacking_indifference?(result) result.convert! end
@example Merge and normalize in place
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada' }) hash.merge!(age: 42) hash['age'] # => 42 hash.keys.sort # => ['age', 'name']
# File lib/hashie/extensions/indifferent_access.rb, line 224 def merge!(*) super.convert! end
@example Select keys using either representation
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada', age: 42 }) hash.slice(:name) # => { 'name' => 'Ada' }
# File lib/hashie/extensions/indifferent_access.rb, line 250 def slice(*keys) string_keys = keys.map { |key| convert_key(key) } super(*string_keys) end
@example Export a regular hash, preserving its default
hash = Hashie::Extensions::IndifferentAccess.inject({ name: 'Ada' }) hash.default = 'unknown' plain = hash.to_hash plain # => { 'name' => 'Ada' } plain[:name] # => 'unknown' plain.respond_to?(:indifferent_access?) # => false
# File lib/hashie/extensions/indifferent_access.rb, line 235 def to_hash {}.tap do |result| each_pair { |key, value| result[key] = value } if default_proc result.default_proc = default_proc else result.default = default end end end
Protected Instance Methods
# File lib/hashie/extensions/indifferent_access.rb, line 265 def hash_lacking_indifference?(other) other.is_a?(::Hash) && !(other.respond_to?(:indifferent_access?) && other.indifferent_access?) end
# File lib/hashie/extensions/indifferent_access.rb, line 271 def hash_with_indifference?(other) other.is_a?(::Hash) && other.respond_to?(:indifferent_access?) && other.indifferent_access? end