Class: Parlour::RbsGenerator::ClassNamespace

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

Overview

Represents a class definition.

Constant Summary collapse

Child =
type_member {{ fixed: RbsObject }}

Instance Attribute Summary collapse

Attributes inherited from Namespace

#children

Attributes inherited from RbsObject

#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_extend, #create_extends, #create_include, #create_includes, #create_interface, #create_method, #create_module, #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, superclass, &block) ⇒ void

Note:

You should use Namespace#create_class rather than this directly.

Creates a new class definition.

Parameters:

  • generator (RbsGenerator)

    The current RbsGenerator.

  • name (String)

    The name of this class.

  • superclass (String, nil)

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

  • block

    A block which the new instance yields itself to.



27
28
29
30
# File 'lib/parlour/rbs_generator/class_namespace.rb', line 27

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

Instance Attribute Details

#superclassTypes::TypeLike? (readonly)

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

Returns:



52
53
54
# File 'lib/parlour/rbs_generator/class_namespace.rb', line 52

def superclass
  @superclass
end

Instance Method Details

#describe_attrsObject



97
98
99
# File 'lib/parlour/rbs_generator/class_namespace.rb', line 97

def describe_attrs
  (superclass ? [:superclass] : []) + [:children]
end

#generate_rbs(indent_level, options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# 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) ⇒ void

This method returns an undefined value.

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

Parameters:



85
86
87
88
89
90
91
92
93
94
# 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) ⇒ 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.



66
67
68
69
70
71
72
73
# 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