class Parlour::RbsGenerator::ClassNamespace

Represents a class definition.

Attributes

superclass[R]

The superclass of this class, or nil if it doesn't have one. @return [Types::TypeLike, nil]

Public Class Methods

new(generator, name, superclass, &block) click to toggle source

Creates a new class definition. @note You should use {Namespace#create_class} rather than this directly.

@param generator [RbsGenerator] The current RbsGenerator. @param name [String] The name of this class. @param final [Boolean] Whether this namespace is final. @param superclass [String, nil] The superclass of this class, or nil if it doesn't

have one.

@param abstract [Boolean] A boolean indicating whether this class is abstract. @param block A block which the new instance yields itself to. @return [void]

Calls superclass method
# File lib/parlour/rbs_generator/class_namespace.rb, line 27
def initialize(generator, name, superclass, &block)
  super(generator, name, &block)
  @superclass = superclass
end

Public Instance Methods

describe() click to toggle source

Returns a human-readable brief string description of this class. @return [String]

# File lib/parlour/rbs_generator/class_namespace.rb, line 99
def describe
  "Class #{name} - #{"superclass #{superclass}, " if superclass}" +
    "#{children.length} children, " +
    "#{includes.length} includes, #{extends.length} extends"
end
generate_rbs(indent_level, options) click to toggle source
# File lib/parlour/rbs_generator/class_namespace.rb, line 38
def generate_rbs(indent_level, options)
  class_definition = @superclass.nil? \
    ? "class #{name}"
    : "class #{name} < #{String === @superclass ? @superclass : @superclass.generate_rbs}"

  lines = generate_comments(indent_level, options)
  lines << options.indented(indent_level, class_definition)
  lines += generate_body(indent_level + 1, options)
  lines << options.indented(indent_level, "end")
end
merge_into_self(others) click to toggle source

Given an array of {ClassNamespace} instances, merges them into this one. You MUST ensure that {mergeable?} is true for those instances.

@param others [Array<RbsGenerator::RbsObject>] An array of other {ClassNamespace} instances. @return [void]

Calls superclass method
# File lib/parlour/rbs_generator/class_namespace.rb, line 85
def merge_into_self(others)
  super

  others.each do |other|
    next unless ClassNamespace === other
    other = T.cast(other, ClassNamespace)

    @superclass = other.superclass unless superclass
  end
end
mergeable?(others) click to toggle source

Given an array of {Namespace} instances, returns true if they may be merged into this instance using {merge_into_self}. For instances to be mergeable, they must either all be abstract or all not be abstract, and they must define the same superclass (or none at all).

@param others [Array<RbsGenerator::RbsObject>] An array of other {Namespace} instances. @return [Boolean] Whether this instance may be merged with them.

# File lib/parlour/rbs_generator/class_namespace.rb, line 66
def mergeable?(others)
  others = T.cast(others, T::Array[Namespace]) rescue (return false)
  all = others + [self]

  all_classes = T.cast(all.select { |x| ClassNamespace === x }, T::Array[ClassNamespace])

  all_classes.map(&:superclass).compact.uniq.length <= 1
end