Class: Parlour::RbiGenerator::EnumClassNamespace
- Inherits:
-
ClassNamespace
- Object
- 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.
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
#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 enum.
-
#initialize(generator, name, final, 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, #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, enums, abstract, &block) ⇒ void
You should use Namespace#create_class rather than this directly.
Creates a new enum class definition.
28 29 30 31 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 28 def initialize(generator, name, final, enums, abstract, &block) super(generator, name, final, 'T::Enum', abstract, &block) @enums = enums end |
Instance Attribute Details
#enums ⇒ Array<(String, String), String> (readonly)
The values of the enumeration.
36 37 38 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 36 def enums @enums end |
Instance Method Details
#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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 50 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.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 100 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).
82 83 84 85 86 87 88 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 82 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 |