Class: MiniObject::Lazy
- Inherits:
-
Delegator
- Object
- Delegator
- MiniObject::Lazy
- Defined in:
- lib/mini_object/lazy.rb
Instance Attribute Summary collapse
-
#lazy_name ⇒ Object
Returns the value of attribute lazy_name.
Instance Method Summary collapse
- #__getobj__ ⇒ Object (also: #get_obj)
- #build(&block) ⇒ Object
- #build_step(name, &block) ⇒ Object
-
#initialize(name = nil, &block) ⇒ Lazy
constructor
A new instance of Lazy.
- #inspect ⇒ Object (also: #to_s)
- #resolver_bound ⇒ Object
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_name ⇒ Object
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 |
#inspect ⇒ Object 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_bound ⇒ Object
22 23 24 |
# File 'lib/mini_object/lazy.rb', line 22 def resolver_bound @block.binding.eval('self') end |