Module: RakeCommander::Patcher::Helpers
- Included in:
- RakeCommander::Patcher
- Defined in:
- lib/rake-commander/patcher/helpers.rb
Overview
Helpers to patch
Instance Method Summary collapse
-
#change_method_argument(arg_name, method:, args:) ⇒ Array
Its usage only makes sense if you extended an existing method you are patching.
-
#method_argument_idx(meth, arg_name) ⇒ Integer, NilClass
For a given method
meth
it gives the index of the parameterarg_name
.
Instance Method Details
#change_method_argument(arg_name, method:, args:) ⇒ Array
Note:
although the signature of a method can change through different versions the name of the parameters is generally preserved (specially when they are core parameters).
Its usage only makes sense if you extended an existing method you are patching.
Therefore it is expected that super
exists, so the original parameters definition
of the method can be accessed.
34 35 36 37 38 39 40 41 |
# File 'lib/rake-commander/patcher/helpers.rb', line 34 def change_method_argument(arg_name, method:, args:) raise ArgumentError, 'Expecting block' unless block_given? raise ArgumentError, "Expecting Method. Given #{method.class}" unless method.is_a?(Method) if idx = method_argument_idx(method.super_method, arg_name.to_sym) args[idx] = yield(args[idx]) end args end |
#method_argument_idx(meth, arg_name) ⇒ Integer, NilClass
For a given method meth
it gives the index of the parameter arg_name
7 8 9 10 11 12 |
# File 'lib/rake-commander/patcher/helpers.rb', line 7 def method_argument_idx(meth, arg_name) arg_name = arg_name.to_sym meth.parameters.each_with_index do |(_type, name), i| return i if name == arg_name end end |