Class: InertiaRails::Lazy

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

Instance Method Summary collapse

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

#callObject



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

def call
  to_proc.call
end

#to_procObject



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