Class: Parlour::RbiGenerator::StructClassNamespace
- Inherits:
-
ClassNamespace
- Object
- TypedObject
- 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
Attributes inherited from TypedObject
#comments, #generated_by, #name
Instance Method Summary collapse
- #generalize_from_rbi! ⇒ Object
-
#generate_body(indent_level, options) ⇒ Array<String>
Generates the RBI lines for the body of this struct.
-
#initialize(generator, name, final, sealed, 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, #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, #describe, #extends, #generate_rbi, #includes, #path
Methods inherited from RbiObject
Methods inherited from TypedObject
Constructor Details
#initialize(generator, name, final, sealed, props, abstract, &block) ⇒ void
You should use Namespace#create_struct_class rather than this directly.
Creates a new struct class definition.
31 32 33 34 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 31 def initialize(generator, name, final, sealed, props, abstract, &block) super(generator, name, final, sealed, 'T::Struct', abstract, &block) @props = props end |
Instance Attribute Details
#props ⇒ Array<StructProp> (readonly)
The props of the struct.
39 40 41 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 39 def props @props end |
Instance Method Details
#generalize_from_rbi! ⇒ Object
105 106 107 108 109 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 105 def generalize_from_rbi! super props.each(&:generalize_from_rbi!) end |
#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.
53 54 55 56 57 58 59 60 61 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 53 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.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 93 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).
75 76 77 78 79 80 81 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 75 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 |