Class: MiniObject::Lazy

Inherits:
Delegator
  • Object
show all
Defined in:
lib/mini_object/lazy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, &block) ⇒ Lazy

Returns a new instance of Lazy.



7
8
9
10
# File 'lib/mini_object/lazy.rb', line 7

def initialize name = nil, &block
  self.lazy_name = name
  @block = block
end

Instance Attribute Details

#lazy_nameObject

Returns the value of attribute lazy_name.



5
6
7
# File 'lib/mini_object/lazy.rb', line 5

def lazy_name
  @lazy_name
end

Instance Method Details

#__getobj__Object Also known as: get_obj



12
13
14
15
16
17
18
# File 'lib/mini_object/lazy.rb', line 12

def __getobj__
  @obj ||= @block.call.tap do |obj|
    build_steps.each do |name, block|
      block.call obj
    end
  end
end

#build(&block) ⇒ Object



30
31
32
# File 'lib/mini_object/lazy.rb', line 30

def build &block
  @block = block
end

#build_step(name, &block) ⇒ Object



26
27
28
# File 'lib/mini_object/lazy.rb', line 26

def build_step name, &block
  build_steps[name] = block
end

#inspectObject Also known as: to_s



34
35
36
37
38
# File 'lib/mini_object/lazy.rb', line 34

def inspect
  prefix = lazy_name ? "#{lazy_name}: " : ""
  steps = " " + build_steps.keys.join(", ") if build_steps.any?
  "< #{prefix}Lazy(#{formatted_block_source}#{steps}) >"
end

#resolver_boundObject



22
23
24
# File 'lib/mini_object/lazy.rb', line 22

def resolver_bound
  @block.binding.eval('self')
end