Class: Parlour::RbsGenerator::Method
- Inherits:
-
RbsObject
- Object
- TypedObject
- RbsObject
- Parlour::RbsGenerator::Method
- Extended by:
- T::Sig
- Defined in:
- lib/parlour/rbs_generator/method.rb
Overview
Represents a method definition.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#class_method ⇒ Boolean
readonly
Whether this method is a class method; that is, it it is defined using
self.
. -
#signatures ⇒ Array<MethodSignature>
readonly
The signatures for each overload of this method.
Attributes inherited from RbsObject
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
-
#generate_rbs(indent_level, options) ⇒ Array<String>
Generates the RBS lines for this method.
-
#initialize(generator, name, signatures, 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 TypedObject
#add_comment, #describe, #describe_tree
Constructor Details
#initialize(generator, name, signatures, class_method: false, &block) ⇒ void
You should use Namespace#create_method rather than this directly.
Creates a new method definition.
29 30 31 32 33 34 |
# File 'lib/parlour/rbs_generator/method.rb', line 29 def initialize(generator, name, signatures, class_method: false, &block) super(generator, name) @signatures = signatures @class_method = class_method yield_self(&block) if block end |
Instance Attribute Details
#class_method ⇒ Boolean (readonly)
Whether this method is a class method; that is, it it is defined using self.
.
58 59 60 |
# File 'lib/parlour/rbs_generator/method.rb', line 58 def class_method @class_method end |
#signatures ⇒ Array<MethodSignature> (readonly)
The signatures for each overload of this method.
52 53 54 |
# File 'lib/parlour/rbs_generator/method.rb', line 52 def signatures @signatures end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method.
42 43 44 45 46 47 |
# File 'lib/parlour/rbs_generator/method.rb', line 42 def ==(other) Method === other && name == other.name && signatures == other.signatures && class_method == other.class_method end |
#describe_attrs ⇒ Object
137 138 139 140 141 142 |
# File 'lib/parlour/rbs_generator/method.rb', line 137 def describe_attrs [ {signatures: "(#{signatures.map(&:describe_in_method).join(", ")})"}, :class_method, ] end |
#generate_rbs(indent_level, options) ⇒ Array<String>
Generates the RBS lines for this method.
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 102 |
# File 'lib/parlour/rbs_generator/method.rb', line 71 def generate_rbs(indent_level, ) definition = "def #{class_method ? 'self.' : ''}#{name}: " lines = generate_comments(indent_level, ) # Handle each signature signatures.each.with_index do |sig, i| this_sig_lines = [] # Start off the first line of the signature, either with the definition # for the first signature, or a pipe for the rest if i == 0 this_sig_lines << .indented(indent_level, definition) else this_sig_lines << .indented(indent_level, "#{' ' * (definition.length - 2)}| ") end # Generate the signature's lines, we'll append them afterwards partial_sig_lines = sig.generate_rbs() # Merge the first signature line, and indent & concat the rest first_line, *rest_lines = *partial_sig_lines this_sig_lines[0] = T.unsafe(this_sig_lines[0]) + first_line rest_lines.each do |line| this_sig_lines << ' ' * definition.length + .indented(indent_level, line) end # Add on all this signature's lines to the complete lines lines += this_sig_lines end lines end |
#merge_into_self(others) ⇒ void
This method returns an undefined value.
Given an array of Parlour::RbsGenerator::Method instances, merges them into this one. This particular implementation in fact does nothing, because Parlour::RbsGenerator::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.
132 133 134 |
# File 'lib/parlour/rbs_generator/method.rb', line 132 def merge_into_self(others) # TODO: merge signatures of different definitions end |
#mergeable?(others) ⇒ Boolean
Given an array of Parlour::RbsGenerator::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.
115 116 117 |
# File 'lib/parlour/rbs_generator/method.rb', line 115 def mergeable?(others) others.all? { |other| self == other } end |