Class: HashTools::Indifferent
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- HashTools::Indifferent
- Defined in:
- lib/hash_tools/indifferent.rb
Instance Method Summary collapse
-
#[](key) ⇒ 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.
-
#[]=(key, value) ⇒ 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(key, &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?(key) ⇒ 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 ⇒ Object
Maps over keys and values of the Hash.
- #method_missing(method_name, *args) ⇒ Object
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
- #to_hash ⇒ Object
-
#to_json(*serializer_state) ⇒ Object
There is a quirk whereby the delegate library will not pass ‘to_json` to the contained Hash, and the Indifferent would the JSON-serialize as a String.
-
#value_present?(key) ⇒ Boolean
Checks if the value at the given key is non-empty.
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.
14 15 16 |
# File 'lib/hash_tools/indifferent.rb', line 14 def initialize(naked_hash) __setobj__(naked_hash) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
113 114 115 116 117 |
# File 'lib/hash_tools/indifferent.rb', line 113 def method_missing(method_name, *args) return self[method_name] if key?(method_name) && args.empty? super end |
Instance Method Details
#[](key) ⇒ 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.
23 24 25 26 |
# File 'lib/hash_tools/indifferent.rb', line 23 def [](key) value = __getobj__[__transform_key__(key)] __rewrap__(value) end |
#[]=(key, value) ⇒ 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.
34 35 36 |
# File 'lib/hash_tools/indifferent.rb', line 34 def []=(key, value) __getobj__[__transform_key__(key)] = value 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
77 78 79 80 81 |
# File 'lib/hash_tools/indifferent.rb', line 77 def each(&blk) __getobj__.each do |key, value| blk.call([__transform_key__(key), __rewrap__(value)]) 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
85 86 87 88 89 90 91 |
# File 'lib/hash_tools/indifferent.rb', line 85 def each_pair object = __getobj__ keys.each do |key| value = object[__transform_key__(key)] yield(key, __rewrap__(value)) end end |
#fetch(key, &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.
44 45 46 47 |
# File 'lib/hash_tools/indifferent.rb', line 44 def fetch(key, &blk) value = __getobj__.fetch(__transform_key__(key), &blk) __rewrap__(value) end |
#key?(key) ⇒ Boolean Also known as: has_key?
Checks for key presence whether the key is a String or a Symbol
59 60 61 |
# File 'lib/hash_tools/indifferent.rb', line 59 def key?(key) __getobj__.key?(__transform_key__(key)) end |
#keys ⇒ Array
Get the keys of the Hash. The keys are returned as-is (both Symbols and Strings).
52 53 54 |
# File 'lib/hash_tools/indifferent.rb', line 52 def keys __getobj__.keys.map { |key| __transform_key__(key) } end |
#map ⇒ Object
Maps over keys and values of the Hash. The key class will be preserved (i.e. within the block the keys will be either Strings or Symbols depending on what is used in the underlying Hash).
96 97 98 99 100 101 |
# File 'lib/hash_tools/indifferent.rb', line 96 def map keys.map do |key| transform_key = __transform_key__(key) yield [transform_key, __rewrap__(__getobj__[transform_key])] end end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
119 120 121 |
# File 'lib/hash_tools/indifferent.rb', line 119 def respond_to_missing?(method_name, _include_private = false) key?(method_name) end |
#to_hash ⇒ Object
123 124 125 |
# File 'lib/hash_tools/indifferent.rb', line 123 def to_hash __getobj__.to_hash end |
#to_json(*serializer_state) ⇒ Object
There is a quirk whereby the delegate library will not pass ‘to_json` to the contained Hash, and the Indifferent would the JSON-serialize as a String. We have to forward this method explicitly.
In general, the method will never be called by the user directly but will instead be excercised by ‘JSON.dump()` and friends.
109 110 111 |
# File 'lib/hash_tools/indifferent.rb', line 109 def to_json(*serializer_state) to_h.to_json(*serializer_state) end |
#value_present?(key) ⇒ Boolean
Checks if the value at the given key is non-empty
66 67 68 69 70 71 72 73 |
# File 'lib/hash_tools/indifferent.rb', line 66 def value_present?(key) return false unless key?(key) value = self[key] return false unless value !value.to_s.empty? end |