Class: Parlour::RbsGenerator::Method

Inherits:
RbsObject show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbs_generator/method.rb

Overview

Represents a method definition.

Direct Known Subclasses

Attribute

Instance Attribute Summary collapse

Attributes inherited from RbsObject

#generator

Attributes inherited from TypedObject

#comments, #generated_by, #name

Instance Method Summary collapse

Methods inherited from TypedObject

#add_comment, #describe, #describe_tree

Constructor Details

#initialize(generator, name, signatures, class_method: false, &block) ⇒ void

Note:

You should use Namespace#create_method rather than this directly.

Creates a new method definition.

Parameters:

  • generator (RbsGenerator)

    The current RbsGenerator.

  • name (String)

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

  • signatures (Array<MethodSignature>)

    The signatures for each overload of this method.

  • class_method (Boolean) (defaults to: false)

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

  • block

    A block which the new instance yields itself to.



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_methodBoolean (readonly)

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

Returns:

  • (Boolean)


58
59
60
# File 'lib/parlour/rbs_generator/method.rb', line 58

def class_method
  @class_method
end

#signaturesArray<MethodSignature> (readonly)

The signatures for each overload of this method.

Returns:



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.

Parameters:

Returns:

  • (Boolean)


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_attrsObject



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.

Parameters:

  • indent_level (Integer)

    The indentation level to generate the lines at.

  • options (Options)

    The formatting options to use.

Returns:

  • (Array<String>)

    The RBS lines, formatted as specified.



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, options)
  definition = "def #{class_method ? 'self.' : ''}#{name}: "
  lines = generate_comments(indent_level, options)

  # 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 << options.indented(indent_level, definition)
    else
      this_sig_lines << options.indented(indent_level, "#{' ' * (definition.length - 2)}| ")
    end

    # Generate the signature's lines, we'll append them afterwards
    partial_sig_lines = sig.generate_rbs(options)

    # 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 + options.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.

Parameters:



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.

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



115
116
117
# File 'lib/parlour/rbs_generator/method.rb', line 115

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