Class: InertiaRails::LazyProp

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

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, &block) ⇒ LazyProp

Returns a new instance of LazyProp.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
# File 'lib/inertia_rails/lazy_prop.rb', line 5

def initialize(value = nil, &block)
  raise ArgumentError, 'You must provide either a value or a block, not both' if value && block

  @value = value
  @block = block
end

Instance Method Details

#call(controller) ⇒ Object



12
13
14
# File 'lib/inertia_rails/lazy_prop.rb', line 12

def call(controller)
  value.respond_to?(:call) ? controller.instance_exec(&value) : value
end

#valueObject



16
17
18
# File 'lib/inertia_rails/lazy_prop.rb', line 16

def value
  @value.nil? ? @block : @value
end