Class: StimulusPlumbers::Components::Plumber::Dispatcher::InstanceExec

Inherits:
Object
  • Object
show all
Defined in:
lib/stimulus_plumbers/components/plumber/dispatcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, *args, **kwargs) ⇒ InstanceExec

Returns a new instance of InstanceExec.



40
41
42
43
44
45
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 40

def initialize(block, *args, **kwargs)
  @block = block
  @args = args
  @kwargs = kwargs
  validate!
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



38
39
40
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 38

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



38
39
40
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 38

def block
  @block
end

#kwargsObject (readonly)

Returns the value of attribute kwargs.



38
39
40
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 38

def kwargs
  @kwargs
end

Instance Method Details

#call(target) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 47

def call(target)
  accepts_args = block.arity.negative? ? args : args.take(block.arity)
  accepts_kwargs = block.parameters.any? { |type, _| %i[key keyreq keyrest].include?(type) }
  if accepts_kwargs
    target.instance_exec(
      *accepts_args,
      **kwargs,
      &block
    )
  else
    target.instance_exec(*accepts_args, &block)
  end
end