Class: Parlour::RbiGenerator::ClassNamespace
- Extended by:
- T::Sig
- Defined in:
- lib/parlour/rbi_generator/class_namespace.rb
Overview
Represents a class definition.
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
#children, #constants, #extends, #includes
Attributes inherited from RbiObject
#comments, #generated_by, #generator, #name
Instance Method Summary collapse
-
#describe ⇒ String
Returns a human-readable brief string description of this class.
-
#generate_rbi(indent_level, options) ⇒ Array<String>
Generates the RBI lines for this class.
-
#initialize(generator, name, 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 ClassNamespace instances, returns true if they may be merged into this instance using #merge_into_self.
Methods inherited from Namespace
#add_comment_to_next_child, #add_constant, #add_extend, #add_include, #create_attr_accessor, #create_attr_reader, #create_attr_writer, #create_attribute, #create_class, #create_method, #create_module
Methods inherited from RbiObject
Constructor Details
#initialize(generator, name, superclass, abstract, &block) ⇒ void
You should use Namespace#create_class rather than this directly.
Creates a new class definition.
27 28 29 30 31 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 27 def initialize(generator, name, superclass, abstract, &block) super(generator, name, &block) @superclass = superclass @abstract = abstract end |
Instance Attribute Details
#abstract ⇒ Boolean (readonly)
A boolean indicating whether this class is abstract or not.
64 65 66 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 64 def abstract @abstract end |
#superclass ⇒ String? (readonly)
The superclass of this class, or nil if it doesn’t have one.
59 60 61 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 59 def superclass @superclass end |
Instance Method Details
#describe ⇒ String
Returns a human-readable brief string description of this class.
109 110 111 112 113 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 109 def describe "Class #{name} - #{"superclass #{superclass}, " if superclass}" + "#{"abstract, " if abstract}#{children.length} children, " + "#{includes.length} includes, #{extends.length} extends" end |
#generate_rbi(indent_level, options) ⇒ Array<String>
Generates the RBI lines for this class.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 44 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.
96 97 98 99 100 101 102 103 104 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 96 def merge_into_self(others) super others.each do |other| other = T.cast(other, ClassNamespace) @superclass = other.superclass unless superclass end end |
#mergeable?(others) ⇒ Boolean
Given an array of Parlour::RbiGenerator::ClassNamespace 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).
78 79 80 81 82 83 84 |
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 78 def mergeable?(others) others = T.cast(others, T::Array[ClassNamespace]) rescue (return false) all = others + [self] all.map(&:abstract).uniq.length == 1 && all.map(&:superclass).compact.uniq.length <= 1 end |