Module: Evoke::Parameters

Included in:
Task
Defined in:
lib/evoke/parameters.rb

Overview

Extendable module for providing access to method parameters during runtime.

Instance Method Summary collapse

Instance Method Details

#optional_parameters(method) ⇒ Array

Gets all the optional parameters for a method.

Parameters:

  • method (UnboundMethod)

    The method to scan.

Returns:

  • (Array)

    An array of the method names.



28
29
30
# File 'lib/evoke/parameters.rb', line 28

def optional_parameters(method)
  select_parameters_by_type(method, :opt)
end

#parameter_names(method) ⇒ Array

Note:

Key and &block parameters are not supported.

Parses the parameters for the supplied method into parameters that can be understood by humans. The method names are capitalized. Optional parameters are surrounded in brackets.

Parameters:

  • method (UnboundMethod)

    The method to scan.

Returns:

  • (Array)

    The human-readable method names.

Raises:

  • (ArgumentError)

    if an unsupported parameter type is detected.



64
65
66
# File 'lib/evoke/parameters.rb', line 64

def parameter_names(method)
  method.parameters.map { |type, name| parameter_name(method, type, name) }
end

#parameter_size(method) ⇒ Array

Finds the minimum and maximum amount of parameters the supplied method supports.

Parameters:

  • method (UnboundMethod)

    The method to check.

Returns:

  • (Array)

    The first item is the minimum size, second is the maximum.



9
10
11
12
13
14
# File 'lib/evoke/parameters.rb', line 9

def parameter_size(method)
  req_size = required_parameters(method).size
  opt_size = optional_parameters(method).size

  [req_size, req_size + opt_size]
end

#required_parameters(method) ⇒ Array

Gets all the required parameters for a method.

Parameters:

  • method (UnboundMethod)

    The method to scan.

Returns:

  • (Array)

    An array of the method names.



20
21
22
# File 'lib/evoke/parameters.rb', line 20

def required_parameters(method)
  select_parameters_by_type(method, :req)
end

#select_parameters_by_type(method, type) ⇒ Array

Finds all the parameters of a given type for the supplied method.

Examples:

Get the key parameters for a method.


class Example
  extend Evoke::Parameters

  def hello(to: "world")
    puts "Hello #{to}"
  end
end

method = Example.instance_method(:hello)
key_params = Example.select_parameters_by_type(method, :key)

Parameters:

  • method (UnboundMethod)

    The method to scan.

  • type (Symbol)

    The type of method.

Returns:

  • (Array)

    An array of the method names.



50
51
52
53
# File 'lib/evoke/parameters.rb', line 50

def select_parameters_by_type(method, type)
  args = method.parameters.select { |param| param[0] == type }
  args.map { |arg| arg[1] }
end