Class: Kitchen::LazyHash

Inherits:
SimpleDelegator
  • Object
show all
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.

Author:

Instance Method Summary collapse

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_hashObject



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