Class: Parlour::RbsGenerator::MethodSignature
- Inherits:
-
Object
- Object
- Parlour::RbsGenerator::MethodSignature
- Extended by:
- T::Sig
- Defined in:
- lib/parlour/rbs_generator/method_signature.rb
Overview
Represents one signature in a method definition. (This is not an RbsObject because it doesn’t generate a full line.)
Instance Attribute Summary collapse
-
#block ⇒ Block?
readonly
The block this method accepts.
-
#parameters ⇒ Array<Parameter>
readonly
An array of Parameter instances representing this method’s parameters.
-
#return_type ⇒ Types::TypeLike?
readonly
What this method returns.
-
#type_parameters ⇒ Array<Symbol>
readonly
This method’s type parameters.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method signature.
- #describe_in_method ⇒ Object
-
#generate_rbs(options) ⇒ Array<String>
Generates the RBS string for this signature.
-
#initialize(parameters, return_type = nil, block: nil, type_parameters: nil) ⇒ void
constructor
Creates a new method signature.
Constructor Details
#initialize(parameters, return_type = nil, block: nil, type_parameters: nil) ⇒ void
Creates a new method signature.
25 26 27 28 29 30 |
# File 'lib/parlour/rbs_generator/method_signature.rb', line 25 def initialize(parameters, return_type = nil, block: nil, type_parameters: nil) @parameters = parameters @return_type = return_type @block = block @type_parameters = type_parameters || [] end |
Instance Attribute Details
#block ⇒ Block? (readonly)
The block this method accepts.
59 60 61 |
# File 'lib/parlour/rbs_generator/method_signature.rb', line 59 def block @block end |
#parameters ⇒ Array<Parameter> (readonly)
An array of Parameter instances representing this method’s parameters.
49 50 51 |
# File 'lib/parlour/rbs_generator/method_signature.rb', line 49 def parameters @parameters end |
#return_type ⇒ Types::TypeLike? (readonly)
What this method returns. Passing nil denotes a void return.
54 55 56 |
# File 'lib/parlour/rbs_generator/method_signature.rb', line 54 def return_type @return_type end |
#type_parameters ⇒ Array<Symbol> (readonly)
This method’s type parameters.
64 65 66 |
# File 'lib/parlour/rbs_generator/method_signature.rb', line 64 def type_parameters @type_parameters end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method signature.
38 39 40 41 42 43 44 |
# File 'lib/parlour/rbs_generator/method_signature.rb', line 38 def ==(other) MethodSignature === other && parameters == other.parameters && return_type == other.return_type && block == other.block && type_parameters == other.type_parameters end |
#describe_in_method ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/parlour/rbs_generator/method_signature.rb', line 104 def describe_in_method # RBS is terse enough that just describing using the RBS is probably # fine. (Unfortunately, this doesn't allow any differentiation between # string types and Parlour::Types types.) # (#describe is supposed to be one line, but this will break if you # have than 10000 parameters. Honestly, if you do have more than 10000 # parameters, you deserve this...) generate_rbs(Parlour::Options.new( break_params: 10000, tab_size: 2, sort_namespaces: false )).join("\n") end |
#generate_rbs(options) ⇒ Array<String>
Generates the RBS string for this signature.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/parlour/rbs_generator/method_signature.rb', line 71 def generate_rbs() block_type = @block&.generate_rbs() rbs_params = parameters.reject { |x| x.kind == :block }.map(&:to_rbs_param) rbs_return_type = String === @return_type ? @return_type : @return_type&.generate_rbs generated_params = parameters.length >= .break_params \ ? ["("] + ( parameters.empty? ? [] : rbs_params.map.with_index do |x, i| .indented( 1, # Don't include the comma on the last parameter. parameters.length == i + 1 ? "#{x}" : "#{x}," ) end ) + [")"] : ["(#{rbs_params.join(', ')})"] generated_params[0] = "#{ type_parameters.any? ? "[#{type_parameters.join(', ')}] " : '' }" + T.must(generated_params[0]) generated_params[-1] = T.must(generated_params[-1]) + "#{ (block_type && block_type.first != 'untyped') ? " #{block_type.first}" : '' # TODO: doesn't support multi-line block types } -> #{rbs_return_type || 'void'}" generated_params end |