Module: RakeCommander::Patcher::Helpers

Included in:
RakeCommander::Patcher
Defined in:
lib/rake-commander/patcher/helpers.rb

Overview

Helpers to patch

Instance Method Summary collapse

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.

Examples:

module Rake
  class Application
    def init(*args)
      args = RakeCommander::Patcher.change_method_argument(:argv, method: method(__method__), args: args) do |value|
        RakeCommander.argv_rake_native_arguments(value)
      end
      super(*args)
    end
  end
end

Parameters:

  • arg_name (Symbol, String)

    the name of the parameter as it reads in the original method.

  • method (Method)

    the extended method (not its super method)

Returns:

  • (Array)

    the original arguments where arg_name has been changed.

Raises:

  • (ArgumentError)


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

Returns:

  • (Integer, NilClass)

    the position of arg_name in parameters.



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