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

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

Defined Under Namespace

Classes: EnumValue, UnexpectedEnumValue

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/graphql/client/schema/enum_type.rb', line 53

def initialize(type)
  unless type.kind.enum?
    raise "expected type to be an Enum, but was #{type.class}"
  end

  @type = type
  @values = {}

  all_values = type.values.keys
  comparison_set = all_values.map { |s| -s.downcase }.to_set

  all_values.each do |value|
    str = EnumValue.new(-value, -value.downcase, comparison_set).freeze
    const_set(value, str) if value =~ /^[A-Z]/
    @values[str.to_s] = str
  end

  @values.freeze
end

Instance Method Details

#[](value) ⇒ Object



77
78
79
# File 'lib/graphql/client/schema/enum_type.rb', line 77

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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/graphql/client/schema/enum_type.rb', line 88

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

#define_class(definition, ast_nodes) ⇒ Object



73
74
75
# File 'lib/graphql/client/schema/enum_type.rb', line 73

def define_class(definition, ast_nodes)
  self
end