Class: Parlour::RbiGenerator::StructClassNamespace

Inherits:
ClassNamespace show all
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

Attributes inherited from ClassNamespace

#abstract, #superclass

Attributes inherited from Namespace

#children, #final, #sealed

Attributes inherited from RbiObject

#generator

Attributes inherited from TypedObject

#comments, #generated_by, #name

Instance Method Summary collapse

Methods inherited from ClassNamespace

#generate_rbi

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

#children, #find, #find_all

Methods inherited from RbiObject

#generate_rbi

Methods inherited from TypedObject

#add_comment, #describe, #describe_tree

Constructor Details

#initialize(generator, name, final, sealed, props, abstract, &block) ⇒ void

Note:

You should use Namespace#create_struct_class rather than this directly.

Creates a new struct class definition.

Parameters:

  • generator (RbiGenerator)

    The current RbiGenerator.

  • name (String)

    The name of this class.

  • final (Boolean)

    Whether this namespace is final.

  • sealed (Boolean)

    Whether this namespace is sealed.

  • props (Array<StructProp>)

    The props of the struct.

  • abstract (Boolean)

    A boolean indicating whether this class is abstract.

  • block

    A block which the new instance yields itself to.



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, &block)
  @props = props
end

Instance Attribute Details

#propsArray<StructProp> (readonly)

The props of the struct.

Returns:



41
42
43
# File 'lib/parlour/rbi_generator/struct_class_namespace.rb', line 41

def props
  @props
end

Instance Method Details

#describe_attrsObject



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.

Parameters:

  • indent_level (Integer)

    The indentation level to generate the lines at.

  • options (Options)

    The formatting options to use.

Returns:

  • (Array<String>)

    The RBI lines for the body, formatted as specified.



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, options)
  result = []
  props.each do |prop|
    result << options.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.

Parameters:



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).

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



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