Class: Parlour::RbiGenerator::Method
- Extended by:
- T::Sig
- Defined in:
- lib/parlour/rbi_generator/method.rb
Overview
Represents a method definition.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#abstract ⇒ Boolean
readonly
Whether this method is abstract.
-
#class_method ⇒ Boolean
readonly
Whether this method is a class method; that is, it it is defined using
self.
. -
#implementation ⇒ Boolean
readonly
Whether this method is an implementation of a parent abstract method.
-
#overridable ⇒ Boolean
readonly
Whether this method is overridable by subclasses.
-
#override ⇒ Boolean
readonly
Whether this method is overriding a parent overridable method.
-
#parameters ⇒ Array<Parameter>
readonly
An array of Parameter instances representing this method’s parameters.
-
#return_type ⇒ String?
readonly
A Sorbet string of what this method returns, such as “String” or “T.untyped”.
Attributes inherited from RbiObject
#comments, #generated_by, #generator, #name
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method.
-
#describe ⇒ String
Returns a human-readable brief string description of this method.
-
#generate_rbi(indent_level, options) ⇒ Array<String>
Generates the RBI lines for this method.
-
#initialize(generator, name, parameters, return_type = nil, abstract: false, implementation: false, override: false, overridable: false, class_method: false, &block) ⇒ void
constructor
Creates a new method definition.
-
#merge_into_self(others) ⇒ void
Given an array of Method instances, merges them into this one.
-
#mergeable?(others) ⇒ Boolean
Given an array of Method instances, returns true if they may be merged into this instance using #merge_into_self.
Methods inherited from RbiObject
Constructor Details
#initialize(generator, name, parameters, return_type = nil, abstract: false, implementation: false, override: false, overridable: false, class_method: false, &block) ⇒ void
You should use Namespace#create_method rather than this directly.
Creates a new method definition.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/parlour/rbi_generator/method.rb', line 42 def initialize(generator, name, parameters, return_type = nil, abstract: false, implementation: false, override: false, overridable: false, class_method: false, &block) super(generator, name) @parameters = parameters @return_type = return_type @abstract = abstract @implementation = implementation @override = override @overridable = overridable @class_method = class_method yield_self(&block) if block end |
Instance Attribute Details
#abstract ⇒ Boolean (readonly)
Whether this method is abstract.
86 87 88 |
# File 'lib/parlour/rbi_generator/method.rb', line 86 def abstract @abstract end |
#class_method ⇒ Boolean (readonly)
Whether this method is a class method; that is, it it is defined using self.
.
107 108 109 |
# File 'lib/parlour/rbi_generator/method.rb', line 107 def class_method @class_method end |
#implementation ⇒ Boolean (readonly)
Whether this method is an implementation of a parent abstract method.
91 92 93 |
# File 'lib/parlour/rbi_generator/method.rb', line 91 def implementation @implementation end |
#overridable ⇒ Boolean (readonly)
Whether this method is overridable by subclasses.
101 102 103 |
# File 'lib/parlour/rbi_generator/method.rb', line 101 def overridable @overridable end |
#override ⇒ Boolean (readonly)
Whether this method is overriding a parent overridable method.
96 97 98 |
# File 'lib/parlour/rbi_generator/method.rb', line 96 def override @override end |
#parameters ⇒ Array<Parameter> (readonly)
An array of Parameter instances representing this method’s parameters.
75 76 77 |
# File 'lib/parlour/rbi_generator/method.rb', line 75 def parameters @parameters end |
#return_type ⇒ String? (readonly)
A Sorbet string of what this method returns, such as “String” or “T.untyped”. Passing nil denotes a void return.
81 82 83 |
# File 'lib/parlour/rbi_generator/method.rb', line 81 def return_type @return_type end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/parlour/rbi_generator/method.rb', line 60 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 end |
#describe ⇒ String
Returns a human-readable brief string description of this method.
192 193 194 195 196 |
# File 'lib/parlour/rbi_generator/method.rb', line 192 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.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/parlour/rbi_generator/method.rb', line 120 def generate_rbi(indent_level, ) return_call = return_type ? "returns(#{return_type})" : 'void' sig_params = parameters.map(&:to_sig_param) sig_lines = parameters.length >= .break_params \ ? [ .indented(indent_level, 'sig do'), .indented(indent_level + 1, "#{qualifiers}params("), ] + ( parameters.empty? ? [] : sig_params.map.with_index do |x, i| .indented( indent_level + 2, # Don't include the comma on the last parameter. parameters.length == i + 1 ? "#{x}" : "#{x}," ) end ) + [ .indented(indent_level + 1, ").#{return_call}"), .indented(indent_level, 'end') ] : [.indented( indent_level, "sig { #{qualifiers}#{ parameters.empty? ? '' : "params(#{sig_params.join(', ')})" }#{ qualifiers.empty? && parameters.empty? ? '' : '.' }#{return_call} }" )] generate_comments(indent_level, ) + sig_lines + generate_definition(indent_level, ) 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.
184 185 186 |
# File 'lib/parlour/rbi_generator/method.rb', line 184 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.
167 168 169 |
# File 'lib/parlour/rbi_generator/method.rb', line 167 def mergeable?(others) others.all? { |other| self == other } end |