Class: Parlour::RbiGenerator::ClassNamespace
- Inherits:
-
Namespace
- Object
- TypedObject
- RbiObject
- Namespace
- Parlour::RbiGenerator::ClassNamespace
- Extended by:
- T::Sig
- Defined in:
- lib/parlour/rbi_generator/class_namespace.rb
Overview
Represents a class definition.
Direct Known Subclasses
Constant Summary collapse
- Child =
type_member {{ fixed: RbiObject }}
Instance Attribute Summary collapse
-
#abstract ⇒ Boolean
readonly
A boolean indicating whether this class is abstract or not.
-
#superclass ⇒ String?
readonly
The superclass of this class, or nil if it doesn’t have one.
Attributes inherited from Namespace
Attributes inherited from RbiObject
Attributes inherited from TypedObject
#comments, #generated_by, #name
Instance Method Summary collapse
- #describe_attrs ⇒ Object
- #generalize_from_rbi! ⇒ Object
-
#generate_rbi(indent_level, options) ⇒ Array<String>
Generates the RBI lines for this class.
-
#initialize(generator, name, final, sealed, superclass, abstract, &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_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
Methods inherited from TypedObject
#add_comment, #describe, #describe_tree
Constructor Details
#initialize(generator, name, final, sealed, superclass, abstract, &block) ⇒ void
You should use Namespace#create_class rather than this directly.
Creates a new class definition.
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
#abstract ⇒ Boolean (readonly)
A boolean indicating whether this class is abstract or not.
70 71 72 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 70 def abstract @abstract end |
#superclass ⇒ String? (readonly)
The superclass of this class, or nil if it doesn’t have one.
65 66 67 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 65 def superclass @superclass end |
Instance Method Details
#describe_attrs ⇒ Object
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.
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, ) class_definition = superclass.nil? \ ? "class #{name}" : "class #{name} < #{superclass}" lines = generate_comments(indent_level, ) lines << .indented(indent_level, class_definition) lines += [.indented(indent_level + 1, "abstract!"), ""] if abstract 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::RbiGenerator::ClassNamespace instances, merges them into this one. You MUST ensure that #mergeable? is true for those instances.
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).
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 |