Class: Parlour::RbiGenerator::Constant

Inherits:
RbiObject show all
Defined in:
lib/parlour/rbi_generator/constant.rb

Overview

Represents a constant definition.

Instance Attribute Summary collapse

Attributes inherited from RbiObject

#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: '', value: '', eigen_constant: false, heredocs: nil, &block) ⇒ Constant

Creates a new constant definition.

Parameters:

  • name (String) (defaults to: '')

    The name of the constant.

  • value (String) (defaults to: '')

    The value of the constant, as a Ruby code string.

  • eigen_constant (Boolean) (defaults to: false)

    Whether this constant is defined on the eigenclass of the current namespace.

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

    Definitions of the heredocs used in the value, if any



23
24
25
26
27
28
29
# File 'lib/parlour/rbi_generator/constant.rb', line 23

def initialize(generator, name: '', value: '', eigen_constant: false, heredocs: nil, &block)
  super(generator, name)
  @value = value
  @heredocs = heredocs
  @eigen_constant = eigen_constant
  yield_self(&block) if block
end

Instance Attribute Details

#eigen_constantBoolean (readonly)

Returns Whether this constant is defined on the eigenclass of the current namespace.

Returns:

  • (Boolean)

    Whether this constant is defined on the eigenclass of the current namespace.



37
38
39
# File 'lib/parlour/rbi_generator/constant.rb', line 37

def eigen_constant
  @eigen_constant
end

#heredocsString? (readonly)

Returns Definitions of the heredocs used in the value, if any.

Returns:

  • (String, nil)

    Definitions of the heredocs used in the value, if any



40
41
42
# File 'lib/parlour/rbi_generator/constant.rb', line 40

def heredocs
  @heredocs
end

#valueObject (readonly)

Returns the value of attribute value.



33
34
35
# File 'lib/parlour/rbi_generator/constant.rb', line 33

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another extend.

Parameters:

  • other (Object)

    The other instance. If this is not a Extend (or a subclass of it), this will always return false.

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/parlour/rbi_generator/constant.rb', line 48

def ==(other)
  Constant === other && name == other.name && value == other.value \
    && eigen_constant == other.eigen_constant && heredocs == other.heredocs
end

#describe_attrsObject



109
110
111
# File 'lib/parlour/rbi_generator/constant.rb', line 109

def describe_attrs
  [:value, :eigen_constant, :heredocs]
end

#generalize_from_rbi!Object



114
115
116
117
118
119
120
# File 'lib/parlour/rbi_generator/constant.rb', line 114

def generalize_from_rbi!
  if @value.is_a?(String)
    # There's a good chance this is an untyped constant, so rescue
    # ParseError and use untyped
    @value = TypeParser.parse_single_type(@value) rescue Types::Untyped.new
  end
end

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

Generates the RBI lines for this constant.

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.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/parlour/rbi_generator/constant.rb', line 64

def generate_rbi(indent_level, options)
  if String === @value
    [
      options.indented(indent_level, "#{name} = #{@value}"),
    ] + [heredocs].compact
  else
    [
      options.indented(indent_level, "#{name} = T.let(nil, #{@value.generate_rbi})"),
    ] + [heredocs].compact
  end
end

#merge_into_self(others) ⇒ void

This method returns an undefined value.

Given an array of Parlour::RbiGenerator::Constant instances, merges them into this one. This particular implementation will simply do nothing, as instances are only mergeable if they are indentical. You MUST ensure that #mergeable? is true for those instances.

Parameters:



104
105
106
# File 'lib/parlour/rbi_generator/constant.rb', line 104

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

#mergeable?(others) ⇒ Boolean

Given an array of Parlour::RbiGenerator::Constant instances, returns true if they may be merged into this instance using #merge_into_self. This is always false.

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



87
88
89
# File 'lib/parlour/rbi_generator/constant.rb', line 87

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