Class: Parlour::RbiGenerator::ClassNamespace

Inherits:
Namespace show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbi_generator/class_namespace.rb

Overview

Represents a class definition.

Direct Known Subclasses

EnumClassNamespace, StructClassNamespace

Constant Summary collapse

Child =
type_member {{ fixed: RbiObject }}

Instance Attribute Summary collapse

Attributes inherited from Namespace

#children, #final, #sealed

Attributes inherited from RbiObject

#generator

Attributes inherited from TypedObject

#comments, #generated_by, #name

Instance Method Summary collapse

Methods inherited from Namespace

#add_comment_to_next_child, #aliases, #constants, #create_arbitrary, #create_attr_accessor, #create_attr_reader, #create_attr_writer, #create_attribute, #create_class, #create_constant, #create_enum_class, #create_extend, #create_extends, #create_include, #create_includes, #create_method, #create_module, #create_struct_class, #create_type_alias, #extends, #includes, #path

Methods included from Mixin::Searchable

#children, #find, #find_all

Methods inherited from TypedObject

#add_comment, #describe, #describe_tree

Constructor Details

#initialize(generator, name, final, sealed, superclass, abstract, &block) ⇒ void

Note:

You should use Namespace#create_class rather than this directly.

Creates a new class definition.

Parameters:

  • generator (RbiGenerator)

    The current RbiGenerator.

  • name (String)

    The name of this class.

  • final (Boolean)

    Whether this namespace is final.

  • sealed (Boolean)

    Whether this namespace is sealed.

  • superclass (String, nil)

    The superclass of this class, or nil if it doesn’t have one.

  • abstract (Boolean)

    A boolean indicating whether this class is abstract.

  • block

    A block which the new instance yields itself to.



33
34
35
36
37
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 33

def initialize(generator, name, final, sealed, superclass, abstract, &block)
  super(generator, name, final, sealed, &T.cast(block, T.nilable(T.proc.params(x: Namespace).void)))
  @superclass = superclass
  @abstract = abstract
end

Instance Attribute Details

#abstractBoolean (readonly)

A boolean indicating whether this class is abstract or not.

Returns:

  • (Boolean)


70
71
72
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 70

def abstract
  @abstract
end

#superclassString? (readonly)

The superclass of this class, or nil if it doesn’t have one.

Returns:

  • (String, nil)


65
66
67
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 65

def superclass
  @superclass
end

Instance Method Details

#describe_attrsObject



116
117
118
119
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 116

def describe_attrs
  (superclass ? [:superclass] : []) \
    + [:children, :abstract, :final, :sealed]
end

#generalize_from_rbi!Object



122
123
124
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 122

def generalize_from_rbi!
  super
end

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

Generates the RBI lines for this class.

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.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 50

def generate_rbi(indent_level, options)
  class_definition = superclass.nil? \
    ? "class #{name}"
    : "class #{name} < #{superclass}"
    
  lines = generate_comments(indent_level, options)
  lines << options.indented(indent_level, class_definition)
  lines += [options.indented(indent_level + 1, "abstract!"), ""] if abstract
  lines += generate_body(indent_level + 1, options)
  lines << options.indented(indent_level, "end")
end

#merge_into_self(others) ⇒ void

This method returns an undefined value.

Given an array of Parlour::RbiGenerator::ClassNamespace instances, merges them into this one. You MUST ensure that #mergeable? is true for those instances.

Parameters:



104
105
106
107
108
109
110
111
112
113
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 104

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) ⇒ Boolean

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).

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



84
85
86
87
88
89
90
91
92
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 84

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(&:abstract).uniq.length == 1 &&
    all_classes.map(&:superclass).compact.uniq.length <= 1
end