Class: StimulusPlumbers::Plumber::Dispatcher::MethodCall
- Inherits:
-
Object
- Object
- StimulusPlumbers::Plumber::Dispatcher::MethodCall
- Defined in:
- lib/stimulus_plumbers/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.
11 12 13 14 15 16 |
# File 'lib/stimulus_plumbers/plumber/dispatcher.rb', line 11 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.
9 10 11 |
# File 'lib/stimulus_plumbers/plumber/dispatcher.rb', line 9 def args @args end |
#kwargs ⇒ Object (readonly)
Returns the value of attribute kwargs.
9 10 11 |
# File 'lib/stimulus_plumbers/plumber/dispatcher.rb', line 9 def kwargs @kwargs end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
9 10 11 |
# File 'lib/stimulus_plumbers/plumber/dispatcher.rb', line 9 def method_name @method_name end |
Instance Method Details
#call(target) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/stimulus_plumbers/plumber/dispatcher.rb', line 18 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 |