Class: Yogurt::CodeGenerator::EnumClass

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Includes:
DefinedClass, Utils
Defined in:
lib/yogurt/code_generator/enum_class.rb

Overview

For GraphQL enum classes

Instance Method Summary collapse

Methods included from DefinedClass

#name

Methods included from Utils

#camelize, #generate_method_name, #generate_pretty_print, #indent, #possible_types_constant, #typename_method, #underscore

Instance Method Details

#dependenciesObject



16
17
18
# File 'lib/yogurt/code_generator/enum_class.rb', line 16

def dependencies
  []
end

#to_rubyObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yogurt/code_generator/enum_class.rb', line 21

def to_ruby
  existing_constants = []

  definitions = serialized_values.sort.map do |name|
    const_name = safe_constant_name(name, existing_constants)
    existing_constants << const_name
    "#{const_name} = new(#{name.inspect})"
  end

  <<~STRING
    class #{name} < T::Enum
      enums do
        #{indent(definitions.join("\n"), 2).strip}
      end
    end
  STRING
end