Class: Yoda::Model::FunctionSignatures::ParameterList
- Inherits:
-
Object
- Object
- Yoda::Model::FunctionSignatures::ParameterList
- Defined in:
- lib/yoda/model/function_signatures/parameter_list.rb
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #block_parameter ⇒ String?
- #each(*args, &proc) ⇒ Enumerator<(String, String)>
-
#initialize(parameters) ⇒ ParameterList
constructor
A new instance of ParameterList.
- #keyword_rest_parameter ⇒ String?
- #optional_keyword_parameters ⇒ Array<(String, String)>
- #optional_parameters ⇒ Array<(String, String)>
- #parameter_names ⇒ Array<String>
- #parameter_options ⇒ Hash{ Symbol => Object }
- #post_parameters ⇒ Array<String>
- #required_keyword_parameters ⇒ Array<String>
- #required_parameters ⇒ Array<String>
- #rest_parameter ⇒ String?
- #to_a ⇒ Array<(String, String)>
Constructor Details
#initialize(parameters) ⇒ ParameterList
Returns a new instance of ParameterList.
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
#parameters ⇒ Array<(String, String)> (readonly)
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
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_parameter ⇒ String?
82 83 84 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 82 def block_parameter [:block_parameter] end |
#each(*args, &proc) ⇒ 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_parameter ⇒ String?
77 78 79 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 77 def keyword_rest_parameter [:keyword_rest_parameter] end |
#optional_keyword_parameters ⇒ Array<(String, String)>
67 68 69 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 67 def optional_keyword_parameters [:optional_keyword_parameters] end |
#optional_parameters ⇒ Array<(String, String)>
57 58 59 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 57 def optional_parameters [:optional_parameters] end |
#parameter_names ⇒ Array<String>
42 43 44 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 42 def parameter_names parameters.map(&:first) end |
#parameter_options ⇒ 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 ||= begin = { 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() 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_parameters ⇒ Array<String>
52 53 54 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 52 def post_parameters [:post_parameters] end |
#required_keyword_parameters ⇒ Array<String>
62 63 64 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 62 def required_keyword_parameters [:required_keyword_parameters] end |
#required_parameters ⇒ Array<String>
47 48 49 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 47 def required_parameters [:required_parameters] end |
#rest_parameter ⇒ String?
72 73 74 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 72 def rest_parameter [:rest_parameter] end |
#to_a ⇒ Array<(String, String)>
32 33 34 |
# File 'lib/yoda/model/function_signatures/parameter_list.rb', line 32 def to_a parameters end |