class InertiaRails::Lazy

Public Class Methods

new(value = nil, &block) click to toggle source
# File lib/inertia_rails/lazy.rb, line 3
def initialize(value = nil, &block)
  @value = value
  @block = block
end

Public Instance Methods

call() click to toggle source
# File lib/inertia_rails/lazy.rb, line 8
def call
  to_proc.call
end
to_proc() click to toggle source
# 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