Class: HashTools::Indifferent
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- HashTools::Indifferent
- Defined in:
- lib/hash_tools/indifferent.rb
Overview
A tiny version of HashWithIndifferentAccess. Works like a wrapper proxy around a Ruby Hash. Does not support all of the methods for a Ruby Hash object, but nevertheless can be useful for checking params and for working with parsed JSON.
Instance Method Summary collapse
-
#[](k) ⇒ Object
Get a value from the Hash, bu supplying a Symbol or a String Key presence is verified by first trying a Symbol, and then a String.
-
#[]=(k, v) ⇒ Object
Set a value in the Hash, bu supplying a Symbol or a String as a key.
-
#each(&blk) ⇒ Object
Yields each key - value pair of the indifferent.
-
#each_pair ⇒ Object
Yields each key - value pair of the indifferent.
-
#fetch(k, &blk) ⇒ Object
Fetch a value, by supplying a Symbol or a String as a key.
-
#initialize(naked_hash) ⇒ Indifferent
constructor
Create a new Indifferent by supplying a Ruby Hash object to wrap.
-
#key?(k) ⇒ Boolean
(also: #has_key?)
Checks for key presence whether the key is a String or a Symbol.
-
#keys ⇒ Array
Get the keys of the Hash.
- #map(&blk) ⇒ Object
Constructor Details
#initialize(naked_hash) ⇒ Indifferent
Create a new Indifferent by supplying a Ruby Hash object to wrap. The Hash being wrapped is not going to be altered or copied.
11 12 13 |
# File 'lib/hash_tools/indifferent.rb', line 11 def initialize(naked_hash) __setobj__(naked_hash) end |
Instance Method Details
#[](k) ⇒ Object
Get a value from the Hash, bu supplying a Symbol or a String Key presence is verified by first trying a Symbol, and then a String.
20 21 22 23 |
# File 'lib/hash_tools/indifferent.rb', line 20 def [](k) v = __getobj__[__transform_key__(k)] __rewrap__(v) end |
#[]=(k, v) ⇒ Object
Set a value in the Hash, bu supplying a Symbol or a String as a key. Key presence is verified by first trying a Symbol, and then a String.
31 32 33 |
# File 'lib/hash_tools/indifferent.rb', line 31 def []=(k, v) __getobj__[ __transform_key__(k) ] = v end |
#each(&blk) ⇒ Object
Yields each key - value pair of the indifferent. If the value is a Hash as well, that hash will be wrapped in an Indifferent before returning
62 63 64 65 66 |
# File 'lib/hash_tools/indifferent.rb', line 62 def each(&blk) __getobj__.each do |k, v| blk.call([__transform_key__(k), __rewrap__(v)]) end end |
#each_pair ⇒ Object
Yields each key - value pair of the indifferent. If the value is a Hash as well, that hash will be wrapped in an Indifferent before returning
70 71 72 73 74 75 76 |
# File 'lib/hash_tools/indifferent.rb', line 70 def each_pair o = __getobj__ keys.each do | k | value = o[__transform_key__(k)] yield(k, __rewrap__(value)) end end |
#fetch(k, &blk) ⇒ Object
Fetch a value, by supplying a Symbol or a String as a key. Key presence is verified by first trying a Symbol, and then a String.
41 42 43 44 |
# File 'lib/hash_tools/indifferent.rb', line 41 def fetch(k, &blk) v = __getobj__.fetch( __transform_key__(k) , &blk) __rewrap__(v) end |
#key?(k) ⇒ Boolean Also known as: has_key?
Checks for key presence whether the key is a String or a Symbol
56 57 58 |
# File 'lib/hash_tools/indifferent.rb', line 56 def key?(k) __getobj__.has_key?( __transform_key__(k)) end |
#keys ⇒ Array
Get the keys of the Hash. The keys are returned as-is (both Symbols and Strings).
49 50 51 |
# File 'lib/hash_tools/indifferent.rb', line 49 def keys __getobj__.keys.map{|k| __transform_key__(k) } end |
#map(&blk) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/hash_tools/indifferent.rb', line 78 def map(&blk) keys.map do |k| tk = __transform_key__(k) yield [tk, __rewrap__(__getobj__[tk])] end end |