Module: Hashie::Extensions::IndifferentAccess
- Defined in:
- lib/hashie/extensions/indifferent_access.rb
Overview
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.
Class Method Summary (collapse)
- + (Object) included(base)
-
+ (Object) inject(hash)
Injects indifferent access into a duplicate of the hash provided.
-
+ (Object) inject!(hash)
This will inject indifferent access into an instance of a hash without modifying the actual class.
Instance Method Summary (collapse)
-
- (Object) convert!
Iterates through the keys and values, reconverting them to their proper indifferent state.
- - (Object) convert_key(key)
- - (Object) convert_value(value)
- - (Boolean) indifferent_access?
- - (Object) indifferent_default(key = nil)
- - (Object) indifferent_delete(key)
- - (Object) indifferent_fetch(key, *args)
- - (Boolean) indifferent_key?(key)
- - (Object) indifferent_replace(other_hash)
- - (Object) indifferent_update(other_hash)
- - (Object) indifferent_values_at(*indices)
- - (Object) indifferent_writer(key, value)
Class Method Details
+ (Object) included(base)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 26 def self.included(base) base.class_eval do alias_method :regular_writer, :[]= alias_method :[]=, :indifferent_writer %w(default update replace fetch delete key? values_at).each do |m| alias_method "regular_#{m}", m alias_method m, "indifferent_#{m}" end %w(include? member? has_key?).each do |key_alias| alias_method key_alias, :indifferent_key? end end end |
+ (Object) inject(hash)
Injects indifferent access into a duplicate of the hash provided. See #inject!
51 52 53 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 51 def self.inject(hash) inject!(hash.dup) end |
+ (Object) inject!(hash)
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.
44 45 46 47 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 44 def self.inject!(hash) (class << hash; self; end).send :include, IndifferentAccess hash.convert! end |
Instance Method Details
- (Object) convert!
Iterates through the keys and values, reconverting them to their proper indifferent state. Used when IndifferentAccess is injecting itself into member hashes.
62 63 64 65 66 67 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 62 def convert! keys.each do |k| regular_writer convert_key(k), convert_value(self.regular_delete(k)) end self end |
- (Object) convert_key(key)
55 56 57 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 55 def convert_key(key) key.to_s end |
- (Object) convert_value(value)
69 70 71 72 73 74 75 76 77 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 69 def convert_value(value) if hash_lacking_indifference?(value) IndifferentAccess.inject(value.dup) elsif value.is_a?(::Array) value.dup.replace(value.map { |e| convert_value(e) }) else value end end |
- (Boolean) indifferent_access?
97 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 97 def indifferent_access?; true end |
- (Object) indifferent_default(key = nil)
79 80 81 82 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 79 def indifferent_default(key = nil) return self[convert_key(key)] if key?(key) regular_default(key) end |
- (Object) indifferent_delete(key)
93 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 93 def indifferent_delete(key); regular_delete convert_key(key) end |
- (Object) indifferent_fetch(key, *args)
92 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 92 def indifferent_fetch(key, *args); regular_fetch convert_key(key), *args end |
- (Boolean) indifferent_key?(key)
94 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 94 def indifferent_key?(key); regular_key? convert_key(key) end |
- (Object) indifferent_replace(other_hash)
99 100 101 102 103 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 99 def indifferent_replace(other_hash) (keys - other_hash.keys).each { |key| delete(key) } other_hash.each { |key, value| self[key] = value } self end |
- (Object) indifferent_update(other_hash)
84 85 86 87 88 89 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 84 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 |
- (Object) indifferent_values_at(*indices)
95 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 95 def indifferent_values_at(*indices); indices.map{|i| self[i] } end |
- (Object) indifferent_writer(key, value)
91 |
# File 'lib/hashie/extensions/indifferent_access.rb', line 91 def indifferent_writer(key, value); regular_writer convert_key(key), convert_value(value) end |