Class: Lotus::Utils::Hash
- Inherits:
-
Object
- Object
- Lotus::Utils::Hash
- Defined in:
- lib/lotus/utils/hash.rb
Overview
Hash on steroids
Constant Summary collapse
- DUPLICATE_LOGIC =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Proc.new do |value| case value when Hash value.deep_dup when ::Hash Hash.new(value).deep_dup.to_h end end.freeze
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
(also: #eql?)
Equality.
-
#[](key) ⇒ Object?
Retrieves the value object corresponding to the key object.
-
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key.
-
#deep_dup ⇒ Hash
Return a deep copy of the current Lotus::Utils::Hash.
-
#delete(key) ⇒ Object?
Deletes the key-value pair and returns the value from hsh whose key is equal to key.
-
#hash ⇒ Fixnum
Returns the hash of the internal @hash.
-
#initialize(hash = {}, &blk) ⇒ Lotus::Utils::Hash
constructor
Initialize the hash.
-
#inspect ⇒ String
Returns a string describing the internal @hash.
-
#keys ⇒ Array
Returns a new array populated with the keys from this hash.
-
#method_missing(m, *args, &blk) ⇒ Object
private
Override Ruby’s method_missing in order to provide ::Hash interface.
-
#respond_to_missing?(m, include_private = false) ⇒ Boolean
private
Override Ruby’s respond_to_missing? in order to support ::Hash interface.
-
#stringify! ⇒ Hash
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
-
#symbolize! ⇒ Hash
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
-
#to_a ⇒ ::Array
Converts into a nested array of [ key, value ] arrays.
-
#to_h ⇒ ::Hash
(also: #to_hash)
Returns a Ruby Hash as duplicated version of self.
Constructor Details
#initialize(hash = {}, &blk) ⇒ Lotus::Utils::Hash
Initialize the hash
46 47 48 49 |
# File 'lib/lotus/utils/hash.rb', line 46 def initialize(hash = {}, &blk) @hash = hash.to_h @hash.default_proc = blk end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &blk) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Override Ruby’s method_missing in order to provide ::Hash interface
280 281 282 283 284 285 286 287 288 |
# File 'lib/lotus/utils/hash.rb', line 280 def method_missing(m, *args, &blk) if respond_to?(m) h = @hash.__send__(m, *args, &blk) h = self.class.new(h) if h.is_a?(::Hash) h else raise NoMethodError.new(%(undefined method `#{ m }' for #{ @hash }:#{ self.class })) end end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass Also known as: eql?
Equality
250 251 252 |
# File 'lib/lotus/utils/hash.rb', line 250 def ==(other) @hash == other.to_h end |
#[](key) ⇒ Object?
Retrieves the value object corresponding to the key object.
202 203 204 |
# File 'lib/lotus/utils/hash.rb', line 202 def [](key) @hash[key] end |
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key.
214 215 216 |
# File 'lib/lotus/utils/hash.rb', line 214 def []=(key, value) @hash[key] = value end |
#deep_dup ⇒ Hash
Return a deep copy of the current Lotus::Utils::Hash
162 163 164 165 166 |
# File 'lib/lotus/utils/hash.rb', line 162 def deep_dup Hash.new.tap do |result| @hash.each {|k, v| result[k] = Duplicable.dup(v, &DUPLICATE_LOGIC) } end end |
#delete(key) ⇒ Object?
Deletes the key-value pair and returns the value from hsh whose key is equal to key.
189 190 191 |
# File 'lib/lotus/utils/hash.rb', line 189 def delete(key) @hash.delete(key) end |
#hash ⇒ Fixnum
Returns the hash of the internal @hash
261 262 263 |
# File 'lib/lotus/utils/hash.rb', line 261 def hash @hash.hash end |
#inspect ⇒ String
Returns a string describing the internal @hash
270 271 272 |
# File 'lib/lotus/utils/hash.rb', line 270 def inspect @hash.inspect end |
#keys ⇒ Array
Returns a new array populated with the keys from this hash
175 176 177 |
# File 'lib/lotus/utils/hash.rb', line 175 def keys @hash.keys end |
#respond_to_missing?(m, include_private = false) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Override Ruby’s respond_to_missing? in order to support ::Hash interface
294 295 296 |
# File 'lib/lotus/utils/hash.rb', line 294 def respond_to_missing?(m, include_private=false) @hash.respond_to?(m, include_private) end |
#stringify! ⇒ Hash
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lotus/utils/hash.rb', line 90 def stringify! keys.each do |k| v = delete(k) v = Hash.new(v).stringify! if v.is_a?(::Hash) self[k.to_s] = v end self end |
#symbolize! ⇒ Hash
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/lotus/utils/hash.rb', line 65 def symbolize! keys.each do |k| v = delete(k) v = Hash.new(v).symbolize! if v.is_a?(::Hash) self[k.to_sym] = v end self end |
#to_a ⇒ ::Array
Converts into a nested array of [ key, value ] arrays.
241 242 243 |
# File 'lib/lotus/utils/hash.rb', line 241 def to_a @hash.to_a end |
#to_h ⇒ ::Hash Also known as: to_hash
Returns a Ruby Hash as duplicated version of self
225 226 227 228 229 230 |
# File 'lib/lotus/utils/hash.rb', line 225 def to_h @hash.each_with_object({}) do |(k, v), result| v = v.to_h if v.is_a?(self.class) result[k] = v end end |