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.

Instance Attribute Summary collapse

Attributes inherited from ClassNamespace

#abstract, #superclass

Attributes inherited from Namespace

#children, #final

Attributes inherited from RbiObject

#comments, #generated_by, #generator, #name

Instance Method Summary collapse

Methods inherited from ClassNamespace

#describe, #generate_rbi

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

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.

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



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

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

The values of the enumeration.

Returns:

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


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.

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.



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, 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:



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

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



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