Module: Hashie::Extensions::IndifferentAccess
- Included in:
- DataBindings::IndifferentHash
- Defined in:
- lib/ext/hashie.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
- .included(base) ⇒ Object
-
.inject(hash) ⇒ Object
Injects indifferent access into a duplicate of the hash provided.
-
.inject!(hash) ⇒ Object
This will inject indifferent access into an instance of a hash without modifying the actual class.
Instance Method Summary collapse
-
#convert! ⇒ Object
Iterates through the keys and values, reconverting them to their proper indifferent state.
- #convert_key(key) ⇒ Object
- #convert_value(value) ⇒ Object
- #indifferent_access? ⇒ Boolean
- #indifferent_default(key = nil) ⇒ Object
- #indifferent_delete(key) ⇒ Object
- #indifferent_fetch(key, *args) ⇒ Object
- #indifferent_key?(key) ⇒ Boolean
- #indifferent_update(other_hash) ⇒ Object
- #indifferent_values_at(*indices) ⇒ Object
- #indifferent_writer(key, value) ⇒ Object
Class Method Details
.included(base) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ext/hashie.rb', line 26 def self.included(base) base.class_eval do alias_method :regular_writer, :[]= alias_method :[]=, :indifferent_writer %w(default update fetch delete key? values_at).each do |m| alias_method "regular_#{m}", m alias_method m, "indifferent_#{m}" end end end |
.inject(hash) ⇒ Object
Injects indifferent access into a duplicate of the hash provided. See #inject!
47 48 49 |
# File 'lib/ext/hashie.rb', line 47 def self.inject(hash) inject!(hash.dup) end |
.inject!(hash) ⇒ Object
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.
40 41 42 43 |
# File 'lib/ext/hashie.rb', line 40 def self.inject!(hash) (class << hash; self; end).send :include, Hashie::Extensions::IndifferentAccess hash.convert! end |
Instance Method Details
#convert! ⇒ Object
Iterates through the keys and values, reconverting them to their proper indifferent state. Used when IndifferentAccess is injecting itself into member hashes.
58 59 60 61 62 63 |
# File 'lib/ext/hashie.rb', line 58 def convert! keys.each do |k| regular_writer convert_key(k), convert_value(self.regular_delete(k)) end self end |
#convert_key(key) ⇒ Object
51 52 53 |
# File 'lib/ext/hashie.rb', line 51 def convert_key(key) key.to_s end |
#convert_value(value) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/ext/hashie.rb', line 65 def convert_value(value) if hash_lacking_indifference?(value) Hashie::Extensions::IndifferentAccess.inject(value.dup) elsif value.is_a?(::Array) value.dup.replace(value.map { |e| convert_value(e) }) else value end end |
#indifferent_access? ⇒ Boolean
93 |
# File 'lib/ext/hashie.rb', line 93 def indifferent_access?; true end |
#indifferent_default(key = nil) ⇒ Object
75 76 77 78 |
# File 'lib/ext/hashie.rb', line 75 def indifferent_default(key = nil) return self[convert_key(key)] if key?(key) regular_default(key) end |
#indifferent_delete(key) ⇒ Object
89 |
# File 'lib/ext/hashie.rb', line 89 def indifferent_delete(key); regular_delete convert_key(key) end |
#indifferent_fetch(key, *args) ⇒ Object
88 |
# File 'lib/ext/hashie.rb', line 88 def indifferent_fetch(key, *args); regular_fetch convert_key(key), *args end |
#indifferent_key?(key) ⇒ Boolean
90 |
# File 'lib/ext/hashie.rb', line 90 def indifferent_key?(key); regular_key? convert_key(key) end |
#indifferent_update(other_hash) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/ext/hashie.rb', line 80 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 |
#indifferent_values_at(*indices) ⇒ Object
91 |
# File 'lib/ext/hashie.rb', line 91 def indifferent_values_at(*indices); indices.map{|i| self[i] } end |
#indifferent_writer(key, value) ⇒ Object
87 |
# File 'lib/ext/hashie.rb', line 87 def indifferent_writer(key, value); regular_writer convert_key(key), convert_value(value) end |