class Peeky::ParameterInfo

Parameter Info takes a ruby paramater pair (*see below) and maps it to more readily usable properties

Ruby parameter is an odd array format that with an array of 1 or 2 elements. Examples:

- [:req]
- [:req, 'aaa']
- [:opt, 'aaa']
- [:rest, 'aaa']
- [:keyreq, 'aaa']
- [:key, 'aaa']
- [:block, 'aaa']

Attributes

default_value[RW]

default value for positional or keyed parameters

name[RW]

name of the parameter

type[RW]

type of the parameter

Public Class Methods

from_method(ruby_method) click to toggle source

Gets list of parameters fro ma ruby method and maps them to an array of ParameterInfo

# File lib/peeky/parameter_info.rb, line 47
def self.from_method(ruby_method)
  ruby_method.parameters.map { |ruby_paramater| from_parameter(ruby_paramater) }
end
from_parameter(ruby_parameter) click to toggle source

Ruby parameter is an odd array format that with an array of 1 or 2 elements. Examples:

- [:req]
- [:req, 'aaa']
- [:opt, 'aaa']
- [:rest, 'aaa']
- [:keyreq, 'aaa']
- [:key, 'aaa']
- [:block, 'aaa']
# File lib/peeky/parameter_info.rb, line 41
def self.from_parameter(ruby_parameter)
  new(ruby_parameter)
end

Private Class Methods

new(param) click to toggle source
# File lib/peeky/parameter_info.rb, line 26
def initialize(param)
  map(param)
end

Public Instance Methods

default_value_type() click to toggle source

Default value type will look at the default value and try to infer the class behind it. Will default to 'Object' fi nil

# File lib/peeky/parameter_info.rb, line 84
def default_value_type
  if @default_value.nil?
    'Object'
  else
    @default_value.class
  end
end
minimal_call_format() click to toggle source

minimal required usage in a call to the method with this paramater

# File lib/peeky/parameter_info.rb, line 62
def minimal_call_format
  @_minimal_call_format ||= begin
    method_name = "minimal_call_format_#{@type}".to_sym

    if respond_to?(method_name, true)
      m = method(method_name)
      m.call
    else
      minimal_call_format_ignore
    end
  end
end
optional?() click to toggle source

@return [Boolean] true when parameter is one of the optional types?

# File lib/peeky/parameter_info.rb, line 78
def optional?
  @_optional |= (@type == :param_optional || @type == :key_optional)
end
signature_format() click to toggle source

ruby code formatted for use in a method signature

# File lib/peeky/parameter_info.rb, line 52
def signature_format
  @_signature_format ||= begin
    method_name = "signature_format_#{@type}".to_sym

    m = method(method_name)
    m.call
  end
end
wrap_default_value(value_for_nil) click to toggle source

Wrap default value in quotes if string, or no wrapping otherwise

@param value_for_nil [String] value for nil, generally '' or 'nil' (required)

# File lib/peeky/parameter_info.rb, line 95
def wrap_default_value(value_for_nil)
  if @default_value.is_a?(String)
    "'#{@default_value}'"
  else
    @default_value.nil? ? value_for_nil : @default_value
  end
end

Private Instance Methods

map(param) click to toggle source

Convert the limited information provided by ruby method.parameters to a richer structure. rubocop:disable Metrics/CyclomaticComplexity

# File lib/peeky/parameter_info.rb, line 108
def map(param)
  @name = param.length > 1 ? param[1].to_s : ''

  @default_value = nil

  case param[0]
  when :req
    @type = :param_required
  when :opt
    @type = :param_optional
  when :rest
    @type = :splat
  when :keyreq
    @type = :key_required
  when :key
    @type = :key_optional
  when :keyrest
    @type = :double_splat
  when :block
    @type = :block
  else
    raise 'unknown type'
  end
end
minimal_call_format_ignore() click to toggle source

Minimal call format *: Is used to format a call to a method with the least number of parameters needed to make it work.

# File lib/peeky/parameter_info.rb, line 168
def minimal_call_format_ignore
  ''
end
minimal_call_format_key_required() click to toggle source
# File lib/peeky/parameter_info.rb, line 176
def minimal_call_format_key_required
  "#{@name}: '#{@name}'"
end
minimal_call_format_param_required() click to toggle source
# File lib/peeky/parameter_info.rb, line 172
def minimal_call_format_param_required
  "'#{@name}'"
end
signature_format_block() click to toggle source
# File lib/peeky/parameter_info.rb, line 161
def signature_format_block
  "&#{name}"
end
signature_format_double_splat() click to toggle source
# File lib/peeky/parameter_info.rb, line 157
def signature_format_double_splat
  "**#{name}"
end
signature_format_key_optional() click to toggle source
# File lib/peeky/parameter_info.rb, line 153
def signature_format_key_optional
  "#{name}: #{wrap_default_value('')}"
end
signature_format_key_required() click to toggle source
# File lib/peeky/parameter_info.rb, line 149
def signature_format_key_required
  "#{name}:"
end
signature_format_param_optional() click to toggle source
# File lib/peeky/parameter_info.rb, line 141
def signature_format_param_optional
  "#{name} = #{wrap_default_value('nil')}" # signature format needs to be moved to a method
end
signature_format_param_required() click to toggle source

Signature format *: Is used to format a parameter when it is used inside of a method signature, eg. def my_method(p1, p2 = 'xyz', p3: :name_value)

# File lib/peeky/parameter_info.rb, line 137
def signature_format_param_required
  name.to_s
end
signature_format_splat() click to toggle source
# File lib/peeky/parameter_info.rb, line 145
def signature_format_splat
  "*#{name}"
end