Class: Parlour::RbiGenerator::EnumClassNamespace
- Inherits:
-
ClassNamespace
- Object
- TypedObject
- RbiObject
- Namespace
- ClassNamespace
- Parlour::RbiGenerator::EnumClassNamespace
- Extended by:
- T::Sig
- Defined in:
- lib/parlour/rbi_generator/enum_class_namespace.rb
Overview
Represents an enum definition; that is, a class with an enum
call.
Constant Summary collapse
- Child =
type_member {{ fixed: RbiObject }}
Instance Attribute Summary collapse
-
#enums ⇒ Array<(String, String), String>
readonly
The values of the enumeration.
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 enum.
-
#initialize(generator, name, final, sealed, enums, abstract, &block) ⇒ void
constructor
Creates a new enum class definition.
-
#merge_into_self(others) ⇒ void
Given an array of EnumClassNamespace instances, merges them into this one.
-
#mergeable?(others) ⇒ Boolean
Given an array of EnumClassNamespace 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, enums, abstract, &block) ⇒ void
You should use Namespace#create_class rather than this directly.
Creates a new enum class definition.
32 33 34 35 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 32 def initialize(generator, name, final, sealed, enums, abstract, &block) super(generator, name, final, sealed, 'T::Enum', abstract, &T.cast(block, T.nilable(T.proc.params(x: Namespace).void))) @enums = enums end |
Instance Attribute Details
#enums ⇒ Array<(String, String), String> (readonly)
The values of the enumeration.
40 41 42 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 40 def enums @enums end |
Instance Method Details
#describe_attrs ⇒ Object
121 122 123 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 121 def describe_attrs super + [{enums: enums.inspect}] end |
#generalize_from_rbi! ⇒ Object
116 117 118 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 116 def generalize_from_rbi! super end |
#generate_body(indent_level, options) ⇒ Array<String>
Generates the RBI lines for the body of this enum. This consists of #enums, Namespace#includes, Namespace#extends and Namespace#children.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 54 def generate_body(indent_level, ) result = [.indented(indent_level, 'enums do')] enums.each do |enum_value| case enum_value when String line = "#{enum_value} = new" when Array line = "#{enum_value[0]} = new(#{enum_value[1]})" else T.absurd(enum_value) end result << .indented(indent_level + 1, line) end result << .indented(indent_level, 'end') result << '' result + super end |
#merge_into_self(others) ⇒ void
This method returns an undefined value.
Given an array of Parlour::RbiGenerator::EnumClassNamespace instances, merges them into this one. You MUST ensure that #mergeable? is true for those instances.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 104 def merge_into_self(others) super others.each do |other| next unless EnumClassNamespace === other other = T.cast(other, EnumClassNamespace) @enums = other.enums if enums.empty? end end |
#mergeable?(others) ⇒ Boolean
Given an array of Parlour::RbiGenerator::EnumClassNamespace 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).
86 87 88 89 90 91 92 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 86 def mergeable?(others) others = T.cast(others, T::Array[Namespace]) rescue (return false) all = others + [self] all_enums = T.cast(all.select { |x| EnumClassNamespace === x }, T::Array[EnumClassNamespace]) T.must(super && all_enums.map { |e| e.enums.sort }.reject(&:empty?).uniq.length <= 1) end |