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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/client/schema/enum_type.rb', line 43

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



67
68
69
# File 'lib/graphql/client/schema/enum_type.rb', line 67

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.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/graphql/client/schema/enum_type.rb', line 78

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, ast_nodes) ⇒ Object



63
64
65
# File 'lib/graphql/client/schema/enum_type.rb', line 63

def define_class(definition, ast_nodes)
  self
end