Class: Parlour::RbiGenerator::EnumClassNamespace

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

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, enums, abstract, &block) ⇒ void

Note:

You should use Namespace#create_class rather than this directly.

Creates a new enum 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.

  • enums (Array<(String, String), String>)

    The values of the enumeration.

  • abstract (Boolean)

    A boolean indicating whether this class is abstract.

  • block

    A block which the new instance yields itself to.



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

Instance Attribute Details

#enumsArray<(String, String), String> (readonly)

The values of the enumeration.

Returns:

  • (Array<(String, String), String>)


40
41
42
# File 'lib/parlour/rbi_generator/enum_class_namespace.rb', line 40

def enums
  @enums
end

Instance Method Details

#describe_attrsObject



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.

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.



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, options)
  result = [options.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 << options.indented(indent_level + 1, line)
  end
  result << options.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.

Parameters:



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

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



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