Class: Peeky::ParameterInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/peeky/parameter_info.rb

Overview

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']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param) ⇒ ParameterInfo

Returns a new instance of ParameterInfo.


26
27
28
# File 'lib/peeky/parameter_info.rb', line 26

def initialize(param)
  map(param)
end

Instance Attribute Details

#default_valueObject

default value for positional or keyed parameters


24
25
26
# File 'lib/peeky/parameter_info.rb', line 24

def default_value
  @default_value
end

#nameObject

name of the parameter


18
19
20
# File 'lib/peeky/parameter_info.rb', line 18

def name
  @name
end

#typeObject

type of the parameter


21
22
23
# File 'lib/peeky/parameter_info.rb', line 21

def type
  @type
end

Class Method Details

.from_method(ruby_method) ⇒ Object

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


47
48
49
# 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) ⇒ Object

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']

41
42
43
# File 'lib/peeky/parameter_info.rb', line 41

def self.from_parameter(ruby_parameter)
  new(ruby_parameter)
end

Instance Method Details

#debugObject


51
52
53
54
55
56
# File 'lib/peeky/parameter_info.rb', line 51

def debug
  puts "name                          : #{name}"
  puts "type                          : #{type}"
  puts "signature_format              : #{signature_format}"
  puts "minimal_call_format           : #{minimal_call_format}"
end

#minimal_call_formatObject

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


69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/peeky/parameter_info.rb', line 69

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

#signature_formatObject

ruby code formatted for use in a method signature


59
60
61
62
63
64
65
66
# File 'lib/peeky/parameter_info.rb', line 59

def signature_format
  @_signature_format ||= begin
    method_name = "signature_format_#{@type}".to_sym

    m = method(method_name)
    m.call
  end
end