Class: InertiaRails::LazyProp

Inherits:
IgnoreOnFirstLoadProp 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
11
12
13
14
# 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

  InertiaRails.deprecator.warn(
    '`lazy` is deprecated and will be removed in InertiaRails 4.0, use `optional` instead.'
  )

  @value = value
  @block = block
end

Instance Method Details

#call(controller) ⇒ Object



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

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

#valueObject



20
21
22
# File 'lib/inertia_rails/lazy_prop.rb', line 20

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