Class: StimulusPlumbers::Components::Plumber::Dispatcher::MethodCall

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(method_name, *args, **kwargs) ⇒ MethodCall

Returns a new instance of MethodCall.



12
13
14
15
16
17
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 12

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 10

def args
  @args
end

#kwargsObject (readonly)

Returns the value of attribute kwargs.



10
11
12
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 10

def kwargs
  @kwargs
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



10
11
12
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 10

def method_name
  @method_name
end

Instance Method Details

#call(target) ⇒ Object

Raises:

  • (NotImplementedError)


19
20
21
22
23
24
25
26
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 19

def call(target)
  raise NotImplementedError, "#{method_name.inspect} not implemented" unless target.respond_to?(method_name, true)

  method_call = target.method(method_name)
  accepts_args = method_call.arity.negative? ? args : args.take(method_call.arity)
  accepts_kwargs = method_call.parameters.any? { |type, _| %i[key keyreq keyrest].include?(type) }
  accepts_kwargs ? method_call.call(*accepts_args, **kwargs) : method_call.call(*accepts_args)
end