Class: Parlour::RbiGenerator::StructClassNamespace
- Inherits:
-
ClassNamespace
- Object
- RbiObject
- Namespace
- ClassNamespace
- Parlour::RbiGenerator::StructClassNamespace
- Extended by:
- T::Sig
- Defined in:
- lib/parlour/rbi_generator/struct_class_namespace.rb
Overview
Represents an struct definition; that is, a class which subclasses T::Struct and declares ‘prop` members.
Instance Attribute Summary collapse
-
#props ⇒ Array<StructProp>
readonly
The props of the struct.
Attributes inherited from ClassNamespace
Attributes inherited from Namespace
Attributes inherited from RbiObject
#comments, #generated_by, #generator, #name
Instance Method Summary collapse
-
#generate_body(indent_level, options) ⇒ Array<String>
Generates the RBI lines for the body of this struct.
-
#initialize(generator, name, final, props, abstract, &block) ⇒ void
constructor
Creates a new struct class definition.
-
#merge_into_self(others) ⇒ void
Given an array of StructClassNamespace instances, merges them into this one.
-
#mergeable?(others) ⇒ Boolean
Given an array of StructClassNamespace instances, returns true if they may be merged into this instance using #merge_into_self.
Methods inherited from ClassNamespace
Methods inherited from Namespace
#add_comment_to_next_child, #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, #describe, #extends, #generate_rbi, #includes, #path
Methods inherited from RbiObject
#add_comment, #describe, #generate_rbi
Constructor Details
#initialize(generator, name, final, props, abstract, &block) ⇒ void
You should use Namespace#create_struct_class rather than this directly.
Creates a new struct class definition.
29 30 31 32 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 29 def initialize(generator, name, final, props, abstract, &block) super(generator, name, final, 'T::Struct', abstract, &block) @props = props end |
Instance Attribute Details
#props ⇒ Array<StructProp> (readonly)
The props of the struct.
37 38 39 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 37 def props @props end |
Instance Method Details
#generate_body(indent_level, options) ⇒ Array<String>
Generates the RBI lines for the body of this struct. This consists of #props, Namespace#includes, Namespace#extends and Namespace#children.
51 52 53 54 55 56 57 58 59 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 51 def generate_body(indent_level, ) result = [] props.each do |prop| result << .indented(indent_level, prop.to_prop_call) end result << '' result + super end |
#merge_into_self(others) ⇒ void
This method returns an undefined value.
Given an array of Parlour::RbiGenerator::StructClassNamespace instances, merges them into this one. You MUST ensure that #mergeable? is true for those instances.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 91 def merge_into_self(others) super others.each do |other| next unless StructClassNamespace === other other = T.cast(other, StructClassNamespace) @props = other.props if props.empty? end end |
#mergeable?(others) ⇒ Boolean
Given an array of Parlour::RbiGenerator::StructClassNamespace 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).
73 74 75 76 77 78 79 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 73 def mergeable?(others) others = T.cast(others, T::Array[Namespace]) rescue (return false) all = others + [self] all_structs = T.cast(all.select { |x| StructClassNamespace === x }, T::Array[StructClassNamespace]) T.must(super && all_structs.map { |s| s.props.map(&:to_prop_call).sort }.reject(&:empty?).uniq.length <= 1) end |