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.
-
#mergeable?(others) ⇒ Boolean
Given an array of EnumClassNamespace instances, returns true if they may be merged into this instance using ClassNamespace#merge_into_self.
Methods inherited from ClassNamespace
#describe, #generate_rbi, #merge_into_self
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_type_alias, #describe, #extends, #generate_rbi, #includes, #merge_into_self, #path
Methods inherited from RbiObject
#add_comment, #describe, #generate_rbi, #merge_into_self
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 |
#mergeable?(others) ⇒ Boolean
Given an array of Parlour::RbiGenerator::EnumClassNamespace instances, returns true if they may be merged into this instance using ClassNamespace#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 |
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 82 def mergeable?(others) others = T.cast(others, T::Array[EnumClassNamespace]) rescue (return false) all = others + [self] T.must(super && all.map(&:enums).uniq.length <= 1) end |