Class: GraphQL::Client::Schema::EnumType

Inherits:
Module
  • Object
show all
Includes:
BaseType
Defined in:
lib/graphql/client/schema/enum_type.rb

Instance Attribute Summary

Attributes included from BaseType

#schema_module, #type

Instance Method Summary collapse

Methods included from BaseType

#to_list_type, #to_non_null_type

Constructor Details

#initialize(type) ⇒ EnumType

Internal: Construct enum wrapper from another GraphQL::EnumType.

type - GraphQL::EnumType instance



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/client/schema/enum_type.rb', line 15

def initialize(type)
  unless type.is_a?(GraphQL::EnumType)
    raise "expected type to be a GraphQL::EnumType, but was #{type.class}"
  end

  @type = type
  @values = {}

  all_values = type.values.keys

  all_values.each do |value|
    str = value.dup
    all_values.each do |v|
      str.define_singleton_method("#{v.downcase}?") { false }
    end
    str.define_singleton_method("#{value.downcase}?") { true }
    str.freeze
    const_set(value, str) if value =~ /^[A-Z]/
    @values[str] = str
  end

  @values.freeze
end

Instance Method Details

#[](value) ⇒ Object



43
44
45
# File 'lib/graphql/client/schema/enum_type.rb', line 43

def [](value)
  @values[value]
end

#cast(value, _errors = nil) ⇒ Object

Internal: Cast JSON value to the enumeration’s corresponding constant string instance

with the convenience predicate methods.

values - JSON value errors - Errors instance

Returns String or nil.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/graphql/client/schema/enum_type.rb', line 54

def cast(value, _errors = nil)
  case value
  when String
    raise Error, "unexpected enum value #{value}" unless @values.key?(value)
    @values[value]
  when NilClass
    value
  else
    raise InvariantError, "expected value to be a String, but was #{value.class}"
  end
end

#define_class(definition, irep_node) ⇒ Object



39
40
41
# File 'lib/graphql/client/schema/enum_type.rb', line 39

def define_class(definition, irep_node)
  self
end