Sha256: 137ff63dddc191643d7d4071479634576f7ddf36a9053b4ebf194dac236257ef

Contents?: true

Size: 684 Bytes

Versions: 22

Compression:

Stored size: 684 Bytes

Contents

module InertiaRails
  class Lazy
    def initialize(value = nil, &block)
      @value = value
      @block = block
    end

    def call
      to_proc.call
    end

    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
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
inertia_rails-1.8.0 lib/inertia_rails/lazy.rb
inertia_rails-1.7.1 lib/inertia_rails/lazy.rb