Class: Parlour::RbiGenerator::Method

Inherits:
RbiObject
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbi_generator/method.rb

Overview

Represents a method definition.

Direct Known Subclasses

Attribute

Instance Attribute Summary collapse

Attributes inherited from RbiObject

#comments, #generated_by, #generator, #name

Instance Method Summary collapse

Methods inherited from RbiObject

#add_comment

Constructor Details

#initialize(generator, name, parameters, return_type = nil, abstract: false, implementation: false, override: false, overridable: false, class_method: false, final: false, type_parameters: nil, &block) ⇒ void

Note:

You should use Namespace#create_method rather than this directly.

Creates a new method definition.

Parameters:

  • generator (RbiGenerator)

    The current RbiGenerator.

  • name (String)

    The name of this method. You should not specify self. in this - use the class_method parameter instead.

  • parameters (Array<Parameter>)

    An array of Parameter instances representing this method’s parameters.

  • return_type (String, nil) (defaults to: nil)

    A Sorbet string of what this method returns, such as “String” or “T.untyped”. Passing nil denotes a void return.

  • abstract (Boolean) (defaults to: false)

    Whether this method is abstract.

  • implementation (Boolean) (defaults to: false)

    DEPRECATED: Whether this method is an implementation of a parent abstract method.

  • override (Boolean) (defaults to: false)

    Whether this method is overriding a parent overridable method, or implementing a parent abstract method.

  • overridable (Boolean) (defaults to: false)

    Whether this method is overridable by subclasses.

  • class_method (Boolean) (defaults to: false)

    Whether this method is a class method; that is, it it is defined using self..

  • final (Boolean) (defaults to: false)

    Whether this method is final.

  • type_parameters (Array<Symbol>, nil) (defaults to: nil)

    This method’s type parameters.

  • block

    A block which the new instance yields itself to.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/parlour/rbi_generator/method.rb', line 46

def initialize(generator, name, parameters, return_type = nil, abstract: false, implementation: false, override: false, overridable: false, class_method: false, final: false, type_parameters: nil, &block)
  super(generator, name)
  @parameters = parameters
  @return_type = return_type
  @abstract = abstract
  @implementation = implementation
  @override = override
  @overridable = overridable
  @class_method = class_method
  @final = final
  @type_parameters = type_parameters || []
  yield_self(&block) if block
end

Instance Attribute Details

#abstractBoolean (readonly)

Whether this method is abstract.

Returns:

  • (Boolean)


94
95
96
# File 'lib/parlour/rbi_generator/method.rb', line 94

def abstract
  @abstract
end

#class_methodBoolean (readonly)

Whether this method is a class method; that is, it it is defined using self..

Returns:

  • (Boolean)


119
120
121
# File 'lib/parlour/rbi_generator/method.rb', line 119

def class_method
  @class_method
end

#finalBoolean (readonly)

Whether this method is final.

Returns:

  • (Boolean)


124
125
126
# File 'lib/parlour/rbi_generator/method.rb', line 124

def final
  @final
end

#implementationBoolean (readonly)

Deprecated.

Removed from Sorbet, as #override is used for both abstract class implementations and superclass overrides. In Parlour, this will now generate override.

Whether this method is an implementation of a parent abstract method.

Returns:

  • (Boolean)


102
103
104
# File 'lib/parlour/rbi_generator/method.rb', line 102

def implementation
  @implementation
end

#overridableBoolean (readonly)

Whether this method is overridable by subclasses.

Returns:

  • (Boolean)


113
114
115
# File 'lib/parlour/rbi_generator/method.rb', line 113

def overridable
  @overridable
end

#overrideBoolean (readonly)

Whether this method is overriding a parent overridable method, or

implementing a parent abstract method.

Returns:

  • (Boolean)


108
109
110
# File 'lib/parlour/rbi_generator/method.rb', line 108

def override
  @override
end

#parametersArray<Parameter> (readonly)

An array of Parameter instances representing this method’s parameters.

Returns:



83
84
85
# File 'lib/parlour/rbi_generator/method.rb', line 83

def parameters
  @parameters
end

#return_typeString? (readonly)

A Sorbet string of what this method returns, such as “String” or “T.untyped”. Passing nil denotes a void return.

Returns:

  • (String, nil)


89
90
91
# File 'lib/parlour/rbi_generator/method.rb', line 89

def return_type
  @return_type
end

#type_parametersArray<Symbol> (readonly)

This method’s type parameters.

Returns:

  • (Array<Symbol>)


129
130
131
# File 'lib/parlour/rbi_generator/method.rb', line 129

def type_parameters
  @type_parameters
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another method.

Parameters:

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/parlour/rbi_generator/method.rb', line 66

def ==(other)
  Method === other &&
    name            == other.name && 
    parameters      == other.parameters &&
    return_type     == other.return_type &&
    abstract        == other.abstract &&
    implementation  == other.implementation &&
    override        == other.override &&
    overridable     == other.overridable &&
    class_method    == other.class_method &&
    final           == other.final &&
    type_parameters == other.type_parameters
end

#describeString

Returns a human-readable brief string description of this method.

Returns:

  • (String)


215
216
217
218
219
# File 'lib/parlour/rbi_generator/method.rb', line 215

def describe
  # TODO: more info
  "Method #{name} - #{parameters.length} parameters, " +
    " returns #{return_type}"
end

#generate_rbi(indent_level, options) ⇒ Array<String>

Generates the RBI lines for this method.

Parameters:

  • indent_level (Integer)

    The indentation level to generate the lines at.

  • options (Options)

    The formatting options to use.

Returns:

  • (Array<String>)

    The RBI lines, formatted as specified.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/parlour/rbi_generator/method.rb', line 142

def generate_rbi(indent_level, options)
  return_call = return_type ? "returns(#{return_type})" : 'void'
  sig_args = final ? '(:final)' : ''

  sig_params = parameters.map(&:to_sig_param)
  sig_lines = parameters.length >= options.break_params \
    ? [
        options.indented(indent_level, "sig#{sig_args} do"),
        options.indented(indent_level + 1, "#{qualifiers}params("),
      ] +
      (
        parameters.empty? ? [] : sig_params.map.with_index do |x, i|
          options.indented(
            indent_level + 2,
            # Don't include the comma on the last parameter.
            parameters.length == i + 1 ? "#{x}" : "#{x},"
          )
        end
      ) +
      [
        options.indented(indent_level + 1, ").#{return_call}"),
        options.indented(indent_level, 'end')
      ]

    : [options.indented(
        indent_level,
        "sig#{sig_args} { #{parameters.empty? ? qualifiers[0...-1] : qualifiers}#{
          parameters.empty? ? '' : "params(#{sig_params.join(', ')})"
        }#{
          qualifiers.empty? && parameters.empty? ? '' : '.'
        }#{return_call} }"
      )]        

  generate_comments(indent_level, options) + sig_lines +
    generate_definition(indent_level, options)
end

#merge_into_self(others) ⇒ void

This method returns an undefined value.

Given an array of Parlour::RbiGenerator::Method instances, merges them into this one. This particular implementation in fact does nothing, because Parlour::RbiGenerator::Method instances are only mergeable if they are identical, so nothing needs to be changed. You MUST ensure that #mergeable? is true for those instances.

Parameters:



207
208
209
# File 'lib/parlour/rbi_generator/method.rb', line 207

def merge_into_self(others)
  # We don't need to change anything! We only merge identical methods
end

#mergeable?(others) ⇒ Boolean

Given an array of Parlour::RbiGenerator::Method instances, returns true if they may be merged into this instance using #merge_into_self. For instances to be mergeable, their signatures and definitions must be identical.

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



190
191
192
# File 'lib/parlour/rbi_generator/method.rb', line 190

def mergeable?(others)
  others.all? { |other| self == other }
end