class Parlour::RbiGenerator::Constant

Represents a constant definition.

Attributes

eigen_constant[R]

@return [Boolean] Whether this constant is defined on the eigenclass

of the current namespace.
value[R]

Public Class Methods

new(generator, name: '', value: '', eigen_constant: false, &block) click to toggle source

Creates a new constant definition.

@param name [String] The name of the constant. @param value [String] The value of the constant, as a Ruby code string. @param eigen_constant [Boolean] Whether this constant is defined on the

eigenclass of the current namespace.
Calls superclass method
# File lib/parlour/rbi_generator/constant.rb, line 21
def initialize(generator, name: '', value: '', eigen_constant: false, &block)
  super(generator, name)
  @value = value
  @eigen_constant = eigen_constant
  yield_self(&block) if block
end

Public Instance Methods

==(other) click to toggle source

Returns true if this instance is equal to another extend.

@param other [Object] The other instance. If this is not a {Extend} (or a

subclass of it), this will always return false.

@return [Boolean]

# File lib/parlour/rbi_generator/constant.rb, line 42
def ==(other)
  Constant === other && name == other.name && value == other.value \
    && eigen_constant == other.eigen_constant
end
describe() click to toggle source

Returns a human-readable brief string description of this code.

@return [String]

# File lib/parlour/rbi_generator/constant.rb, line 102
def describe
  "Constant (#{name} = #{value})"
end
generalize_from_rbi!() click to toggle source
# File lib/parlour/rbi_generator/constant.rb, line 107
def generalize_from_rbi!
  # There's a good change this is an untyped constant, so rescue
  # ParseError and use untyped
  @value = (TypeParser.parse_single_type(@value) if String === @value) rescue Types::Untyped.new
end
generate_rbi(indent_level, options) click to toggle source

Generates the RBI lines for this constant.

@param indent_level [Integer] The indentation level to generate the lines at. @param options [Options] The formatting options to use. @return [Array<String>] The RBI lines, formatted as specified.

# File lib/parlour/rbi_generator/constant.rb, line 58
def generate_rbi(indent_level, options)
  if String === @value
    [options.indented(indent_level, "#{name} = #{@value}")]
  else
    [options.indented(indent_level, "#{name} = T.let(nil, #{@value.generate_rbi})")]
  end
end
merge_into_self(others) click to toggle source

Given an array of {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.

@param others [Array<RbiGenerator::RbiObject>] An array of other

{Extend} instances.

@return [void]

# File lib/parlour/rbi_generator/constant.rb, line 94
def merge_into_self(others)
  # We don't need to change anything! We only merge identical constants
end
mergeable?(others) click to toggle source

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

@param others [Array<RbiGenerator::RbiObject>] An array of other

{Constant} instances.

@return [Boolean] Whether this instance may be merged with them.

# File lib/parlour/rbi_generator/constant.rb, line 77
def mergeable?(others)
  others.all? { |other| self == other }
end