Class: Parlour::RbsGenerator::ClassNamespace
- Inherits:
-
Namespace
- Object
- TypedObject
- RbsObject
- Namespace
- Parlour::RbsGenerator::ClassNamespace
- 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
-
#superclass ⇒ Types::TypeLike?
readonly
The superclass of this class, or nil if it doesn’t have one.
Attributes inherited from Namespace
Attributes inherited from RbsObject
Attributes inherited from TypedObject
#comments, #generated_by, #name
Instance Method Summary collapse
- #describe_attrs ⇒ Object
- #generate_rbs(indent_level, options) ⇒ Object
-
#initialize(generator, name, superclass, &block) ⇒ void
constructor
Creates a new class definition.
-
#merge_into_self(others) ⇒ void
Given an array of ClassNamespace instances, merges them into this one.
-
#mergeable?(others) ⇒ Boolean
Given an array of Namespace instances, returns true if they may be merged into this instance using #merge_into_self.
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
Methods inherited from TypedObject
#add_comment, #describe, #describe_tree
Constructor Details
#initialize(generator, name, superclass, &block) ⇒ void
You should use Namespace#create_class rather than this directly.
Creates a new class definition.
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
#superclass ⇒ Types::TypeLike? (readonly)
The superclass of this class, or nil if it doesn’t have one.
52 53 54 |
# File 'lib/parlour/rbs_generator/class_namespace.rb', line 52 def superclass @superclass end |
Instance Method Details
#describe_attrs ⇒ Object
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, ) class_definition = @superclass.nil? \ ? "class #{name}" : "class #{name} < #{String === @superclass ? @superclass : @superclass.generate_rbs}" lines = generate_comments(indent_level, ) lines << .indented(indent_level, class_definition) lines += generate_body(indent_level + 1, ) lines << .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.
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).
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 |