Class: StimulusPlumbers::Components::Plumber::Dispatcher::MethodCall
- Inherits:
-
Object
- Object
- StimulusPlumbers::Components::Plumber::Dispatcher::MethodCall
- Defined in:
- lib/stimulus_plumbers/components/plumber/dispatcher.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#kwargs ⇒ Object
readonly
Returns the value of attribute kwargs.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
Instance Method Summary collapse
- #call(target) ⇒ Object
-
#initialize(method_name, *args, **kwargs) ⇒ MethodCall
constructor
A new instance of MethodCall.
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
10 11 12 |
# File 'lib/stimulus_plumbers/components/plumber/dispatcher.rb', line 10 def args @args end |
#kwargs ⇒ Object (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_name ⇒ Object (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
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 |