Class: Parlour::RbiGenerator::Method
- Inherits:
-
RbiObject
- Object
- TypedObject
- RbiObject
- 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.
. -
#final ⇒ Boolean
readonly
Whether this method is final.
-
#implementation ⇒ Boolean
readonly
deprecated
Deprecated.
Removed from Sorbet, as #override is used for both abstract class implementations and superclass overrides. In Parlour, this will now generate
override
. -
#overridable ⇒ Boolean
readonly
Whether this method is overridable by subclasses.
-
#override ⇒ Boolean
readonly
Whether this method is overriding a parent overridable method, or implementing a parent abstract method.
-
#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.
Attributes inherited from RbiObject
Attributes inherited from TypedObject
#comments, #generated_by, #name
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method.
- #describe_attrs ⇒ Object
- #generalize_from_rbi! ⇒ Object
-
#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, final: false, type_parameters: nil, &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 TypedObject
#add_comment, #describe, #describe_tree
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
You should use Namespace#create_method rather than this directly.
Creates a new method definition.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/parlour/rbi_generator/method.rb', line 45 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
#abstract ⇒ Boolean (readonly)
Whether this method is abstract.
92 93 94 |
# File 'lib/parlour/rbi_generator/method.rb', line 92 def abstract @abstract end |
#class_method ⇒ Boolean (readonly)
Whether this method is a class method; that is, it it is defined using self.
.
117 118 119 |
# File 'lib/parlour/rbi_generator/method.rb', line 117 def class_method @class_method end |
#final ⇒ Boolean (readonly)
Whether this method is final.
122 123 124 |
# File 'lib/parlour/rbi_generator/method.rb', line 122 def final @final end |
#implementation ⇒ Boolean (readonly)
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.
100 101 102 |
# File 'lib/parlour/rbi_generator/method.rb', line 100 def implementation @implementation end |
#overridable ⇒ Boolean (readonly)
Whether this method is overridable by subclasses.
111 112 113 |
# File 'lib/parlour/rbi_generator/method.rb', line 111 def overridable @overridable end |
#override ⇒ Boolean (readonly)
Whether this method is overriding a parent overridable method, or
implementing a parent abstract method.
106 107 108 |
# File 'lib/parlour/rbi_generator/method.rb', line 106 def override @override end |
#parameters ⇒ Array<Parameter> (readonly)
An array of Parameter instances representing this method’s parameters.
82 83 84 |
# File 'lib/parlour/rbi_generator/method.rb', line 82 def parameters @parameters end |
#return_type ⇒ Types::TypeLike? (readonly)
What this method returns. Passing nil denotes a void return.
87 88 89 |
# File 'lib/parlour/rbi_generator/method.rb', line 87 def return_type @return_type end |
#type_parameters ⇒ Array<Symbol> (readonly)
This method’s type parameters.
127 128 129 |
# File 'lib/parlour/rbi_generator/method.rb', line 127 def type_parameters @type_parameters end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/parlour/rbi_generator/method.rb', line 65 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 |
#describe_attrs ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/parlour/rbi_generator/method.rb', line 217 def describe_attrs (type_parameters.any? ? [{ type_parameters: type_parameters.join(", ")}] : []) \ + [ {parameters: "(#{parameters.map(&:describe_in_method).join(", ")})"}, {return_type: return_type || '(void)'}, # avoid quotes :class_method, :abstract, :implementation, :override, :overridable, :final, ] end |
#generalize_from_rbi! ⇒ Object
210 211 212 213 214 |
# File 'lib/parlour/rbi_generator/method.rb', line 210 def generalize_from_rbi! @return_type = TypeParser.parse_single_type(@return_type) if String === @return_type parameters.each(&:generalize_from_rbi!) end |
#generate_rbi(indent_level, options) ⇒ Array<String>
Generates the RBI lines for this method.
140 141 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 |
# File 'lib/parlour/rbi_generator/method.rb', line 140 def generate_rbi(indent_level, ) return_call = @return_type ? "returns(#{String === @return_type ? @return_type : @return_type.generate_rbi})" : 'void' sig_args = final ? '(:final)' : '' sig_params = parameters.map(&:to_sig_param) sig_lines = parameters.length >= .break_params \ ? [ .indented(indent_level, "sig#{sig_args} 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#{sig_args} { #{parameters.empty? ? qualifiers[0...-1] : 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.
205 206 207 |
# File 'lib/parlour/rbi_generator/method.rb', line 205 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.
188 189 190 |
# File 'lib/parlour/rbi_generator/method.rb', line 188 def mergeable?(others) others.all? { |other| self == other } end |