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.
Constant Summary collapse
- Child =
type_member {{ fixed: RbiObject }}
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
- #describe_attrs ⇒ Object
- #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, #extends, #generate_rbi, #includes, #path
Methods included from Mixin::Searchable
Methods inherited from RbiObject
Methods inherited from TypedObject
#add_comment, #describe, #describe_tree
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.
33 34 35 36 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 33 def initialize(generator, name, final, sealed, props, abstract, &block) super(generator, name, final, sealed, 'T::Struct', abstract, &T.cast(block, T.nilable(T.proc.params(x: Namespace).void))) @props = props end |
Instance Attribute Details
#props ⇒ Array<StructProp> (readonly)
The props of the struct.
41 42 43 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 41 def props @props end |
Instance Method Details
#describe_attrs ⇒ Object
114 115 116 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 114 def describe_attrs super + [{props: "(#{props.map(&:name)})"}] end |
#generalize_from_rbi! ⇒ Object
107 108 109 110 111 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 107 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.
55 56 57 58 59 60 61 62 63 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 55 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.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 95 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).
77 78 79 80 81 82 83 |
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 77 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 |