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-3.4.0 lib/inertia_rails/lazy.rb
inertia_rails-3.3.0 lib/inertia_rails/lazy.rb
inertiax_rails-2.0.0 lib/inertia_rails/lazy.rb
inertia_rails-3.2.0 lib/inertia_rails/lazy.rb
inertia_rails-3.1.4 lib/inertia_rails/lazy.rb
inertia_rails-3.1.3 lib/inertia_rails/lazy.rb
inertia_rails-3.1.2 lib/inertia_rails/lazy.rb
inertia_rails-3.1.1 lib/inertia_rails/lazy.rb
inertia_rails-3.1.0 lib/inertia_rails/lazy.rb
inertia_rails-3.0.0 lib/inertia_rails/lazy.rb
inertia_rails-2.0.1 lib/inertia_rails/lazy.rb
inertia_rails-2.0.0 lib/inertia_rails/lazy.rb
inertia_rails-1.12.1 lib/inertia_rails/lazy.rb
inertia_rails-1.12.0 lib/inertia_rails/lazy.rb
inertia_rails-1.11.1 lib/inertia_rails/lazy.rb
inertia_rails-1.11.0 lib/inertia_rails/lazy.rb
inertia_rails-1.10.0 lib/inertia_rails/lazy.rb
inertia_rails-1.9.2 lib/inertia_rails/lazy.rb
inertia_rails-1.9.1 lib/inertia_rails/lazy.rb
inertia_rails-1.9.0 lib/inertia_rails/lazy.rb