Class: Yoda::Model::FunctionSignatures::ParameterList

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/model/function_signatures/parameter_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ ParameterList

Returns a new instance of ParameterList.

Parameters:

  • parameters (Array<(String, String)>)


26
27
28
29
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 26

def initialize(parameters)
  fail ArgumentError, parameters unless parameters.all? { |param| param.is_a?(Array) }
  @parameters = parameters
end

Instance Attribute Details

#parametersArray<(String, String)> (readonly)

Returns:

  • (Array<(String, String)>)


6
7
8
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 6

def parameters
  @parameters
end

Class Method Details

.from_rbs_method_type(type) ⇒ Object

Parameters:

  • type (RBS::MethodType)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 9

def self.from_rbs_method_type(type)
  func = type.type

  parameters = []
  parameters += func.required_positionals.map { |param| [param.name, ""] }
  parameters += func.optional_positionals.map { |param| [param.name, ""] }
  parameters += ["*#{type.rest_positionals.name}", ""] if func.rest_positionals
  parameters += func.trailing_positionals.map { |param| [param.name, ""] }
  parameters += func.required_keywords.map { |param| ["#{param.name}:", ""] }
  parameters += func.optional_keywords.map { |param| ["#{param.name}:", ""] }
  parameters += ["**#{func.rest_keywords.name}", ""] if func.rest_keywords
  parameters += ["&block", ""] if type.block

  new(parameters)
end

Instance Method Details

#block_parameterString?

Returns:

  • (String, nil)


82
83
84
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 82

def block_parameter
  parameter_options[:block_parameter]
end

#each(*args, &proc) ⇒ Enumerator<(String, String)>

Returns:

  • (Enumerator<(String, String)>)


37
38
39
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 37

def each(*args, &proc)
  to_a.each(*args, &proc)
end

#keyword_rest_parameterString?

Returns:

  • (String, nil)


77
78
79
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 77

def keyword_rest_parameter
  parameter_options[:keyword_rest_parameter]
end

#optional_keyword_parametersArray<(String, String)>

Returns:

  • (Array<(String, String)>)


67
68
69
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 67

def optional_keyword_parameters
  parameter_options[:optional_keyword_parameters]
end

#optional_parametersArray<(String, String)>

Returns:

  • (Array<(String, String)>)


57
58
59
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 57

def optional_parameters
  parameter_options[:optional_parameters]
end

#parameter_namesArray<String>

Returns:

  • (Array<String>)


42
43
44
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 42

def parameter_names
  parameters.map(&:first)
end

#parameter_optionsHash{ Symbol => Object }

Returns:

  • (Hash{ Symbol => Object })


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 87

def parameter_options
  @parameter_options ||= begin
    options = {
      required_parameters: [],
      optional_parameters: [],
      rest_parameter: nil,
      post_parameters: [],
      required_keyword_parameters: [],
      optional_keyword_parameters: [],
      keyword_rest_parameter: nil,
      block_parameter: nil,
    }
    parameters.each_with_object(options) do |(name, default), obj|
      if name.to_s.start_with?('**')
        obj[:keyword_rest_parameter] ||= name.to_s.gsub(/\A\*\*/, '')
      elsif name.to_s.start_with?('*')
        obj[:rest_parameter] ||= name.to_s.gsub(/\A\*/, '')
      elsif name.to_s.start_with?('&')
        obj[:block_parameter] ||= name.to_s.gsub(/\A\&/, '')
      elsif name.to_s.end_with?(':')
        if default && !default.empty?
          obj[:optional_keyword_parameters].push([name.to_s.gsub(/:\Z/, ''), default])
        else
          obj[:required_keyword_parameters].push(name.to_s.gsub(/:\Z/, ''))
        end
      elsif default && !default.empty?
        obj[:optional_parameters].push([name, default])
      elsif obj[:rest_parameter]
        obj[:post_parameters].push(name.to_s)
      else
        obj[:required_parameters].push(name.to_s)
      end
    end
  end
end

#post_parametersArray<String>

Returns:

  • (Array<String>)


52
53
54
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 52

def post_parameters
  parameter_options[:post_parameters]
end

#required_keyword_parametersArray<String>

Returns:

  • (Array<String>)


62
63
64
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 62

def required_keyword_parameters
  parameter_options[:required_keyword_parameters]
end

#required_parametersArray<String>

Returns:

  • (Array<String>)


47
48
49
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 47

def required_parameters
  parameter_options[:required_parameters]
end

#rest_parameterString?

Returns:

  • (String, nil)


72
73
74
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 72

def rest_parameter
  parameter_options[:rest_parameter]
end

#to_aArray<(String, String)>

Returns:

  • (Array<(String, String)>)


32
33
34
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 32

def to_a
  parameters
end