Class: Kitchen::LazyHash
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Kitchen::LazyHash
- Defined in:
- lib/kitchen/lazy_hash.rb
Overview
A modifed Hash object that may contain procs as a value which must be executed in the context of another object.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(obj, context) ⇒ LazyHash
constructor
A new instance of LazyHash.
- #to_hash ⇒ Object
Constructor Details
#initialize(obj, context) ⇒ LazyHash
Returns a new instance of LazyHash.
29 30 31 32 |
# File 'lib/kitchen/lazy_hash.rb', line 29 def initialize(obj, context) @context = context super(obj) end |
Instance Method Details
#[](key) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/kitchen/lazy_hash.rb', line 34 def [](key) proc_or_val = __getobj__[key] if proc_or_val.respond_to?(:call) proc_or_val.call(@context) else proc_or_val end end |
#to_hash ⇒ Object
44 45 46 47 48 |
# File 'lib/kitchen/lazy_hash.rb', line 44 def to_hash hash = Hash.new __getobj__.keys.each { |key| hash[key] = self[key] } hash end |