Class: Kitchen::LazyHash
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Kitchen::LazyHash
- Includes:
- Enumerable
- Defined in:
- lib/kitchen/lazy_hash.rb
Overview
A modified Hash object that may contain callables as a value which must be executed in the context of another object. This allows for delayed evaluation of a hash value while still looking and largely feeling like a normal Ruby Hash.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Retrieves the rendered value object corresponding to the key object.
-
#[]=(key, value) ⇒ Object
Sets the raw value for the given key.
-
#delete_if(&block) ⇒ Hash
Returns a new Hash after deleting the key-value pairs for which the block returns true.
-
#each(&block) ⇒ Enumerator, Array
If no block provided, returns an enumerator over the keys and rendered values in the underlying object.
-
#fetch(key, default = :__undefined__, &block) ⇒ Object?
Returns a rendered value from the hash for the given key.
-
#initialize(obj, context) ⇒ LazyHash
constructor
Creates a new LazyHash using a Hash-like object to populate itself and an object that can be used as context in value-callable blocks.
-
#key?(key) ⇒ Boolean
Returns whether the given key is present, without rendering its value.
-
#keys ⇒ Array
Returns the keys of the underlying hash.
-
#select(&block) ⇒ Hash
Yields each key/value pair to the provided block.
-
#to_hash ⇒ Hash
Returns a new Hash with all keys and rendered values of the LazyHash.
Constructor Details
#initialize(obj, context) ⇒ LazyHash
Creates a new LazyHash using a Hash-like object to populate itself and an object that can be used as context in value-callable blocks. The context object can be used to compute values for keys at the time of fetching the value.
63 64 65 66 |
# File 'lib/kitchen/lazy_hash.rb', line 63 def initialize(obj, context) @context = context super(obj) end |
Instance Method Details
#[](key) ⇒ Object?
Retrieves the rendered value object corresponding to the key object. If not found, returns the default value.
74 75 76 |
# File 'lib/kitchen/lazy_hash.rb', line 74 def [](key) proc_or_val(__getobj__[key]) end |
#[]=(key, value) ⇒ Object
Sets the raw value for the given key. Values are stored unrendered, so a callable assigned here is still evaluated lazily on read.
SimpleDelegator would forward this through #method_missing; defining it here keeps the hot path off that dispatch, which every plugin walks once per default attribute while applying its defaults.
108 109 110 |
# File 'lib/kitchen/lazy_hash.rb', line 108 def []=(key, value) __getobj__[key] = value end |
#delete_if(&block) ⇒ Hash
Returns a new Hash after deleting the key-value pairs for which the block returns true.
165 166 167 |
# File 'lib/kitchen/lazy_hash.rb', line 165 def delete_if(&block) to_hash.delete_if(&block) end |
#each(&block) ⇒ Enumerator, Array
If no block provided, returns an enumerator over the keys and
rendered values in the underlying object. If a block is
provided, calls the block once for each [key, rendered_value]
pair in the underlying object.
157 158 159 |
# File 'lib/kitchen/lazy_hash.rb', line 157 def each(&block) to_hash.each(&block) end |
#fetch(key, default = :__undefined__, &block) ⇒ Object?
Returns a rendered value from the hash for the given key. If the key can't be found, there are several options: With no other arguments, it will raise a KeyError exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.
89 90 91 92 93 94 95 96 |
# File 'lib/kitchen/lazy_hash.rb', line 89 def fetch(key, default = :__undefined__, &block) case default when :__undefined__ proc_or_val(__getobj__.fetch(key, &block)) else proc_or_val(__getobj__.fetch(key, default, &block)) end end |
#key?(key) ⇒ Boolean
Returns whether the given key is present, without rendering its value.
Defined for the same reason as #[]=: to bypass SimpleDelegator's #method_missing on a hot path.
119 120 121 |
# File 'lib/kitchen/lazy_hash.rb', line 119 def key?(key) __getobj__.key?(key) end |
#keys ⇒ Array
Returns the keys of the underlying hash.
Defined for the same reason as #[]=: to bypass SimpleDelegator's #method_missing on a hot path.
129 130 131 |
# File 'lib/kitchen/lazy_hash.rb', line 129 def keys __getobj__.keys end |
#select(&block) ⇒ Hash
Yields each key/value pair to the provided block. Returns a new Hash with only the keys and rendered values for which the block returns true.
147 148 149 |
# File 'lib/kitchen/lazy_hash.rb', line 147 def select(&block) to_hash.select(&block) end |
#to_hash ⇒ Hash
Returns a new Hash with all keys and rendered values of the LazyHash.
136 137 138 139 140 |
# File 'lib/kitchen/lazy_hash.rb', line 136 def to_hash hash = {} __getobj__.each_key { |key| hash[key] = self[key] } hash end |