Class: InertiaRails::Lazy
- Inherits:
-
Object
- Object
- InertiaRails::Lazy
- Defined in:
- lib/inertia_rails/lazy.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(value = nil, &block) ⇒ Lazy
constructor
A new instance of Lazy.
- #to_proc ⇒ Object
Constructor Details
#initialize(value = nil, &block) ⇒ Lazy
Returns a new instance of Lazy.
3 4 5 6 |
# File 'lib/inertia_rails/lazy.rb', line 3 def initialize(value = nil, &block) @value = value @block = block end |
Instance Method Details
#call ⇒ Object
8 9 10 |
# File 'lib/inertia_rails/lazy.rb', line 8 def call to_proc.call end |
#to_proc ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/inertia_rails/lazy.rb', line 12 def to_proc # This is called by controller.instance_exec, which changes self to the # controller instance. That makes the instance variables unavailable to the # proc via closure. Copying the instance variables to local variables before # the proc is returned keeps them in scope for the returned proc. value = @value block = @block if value.respond_to?(:call) value elsif value Proc.new { value } else block end end |