Commit 2690332c authored by Mario de la Ossa's avatar Mario de la Ossa

Make GraphQL enums a little bit nicer

parent a658e852
...@@ -2,5 +2,18 @@ ...@@ -2,5 +2,18 @@
module Types module Types
class BaseEnum < GraphQL::Schema::Enum class BaseEnum < GraphQL::Schema::Enum
class << self
def value(*args, **kwargs, &block)
enum[args[0].downcase] = kwargs[:value] || args[0]
super(*args, **kwargs, &block)
end
# Returns an indifferent access hash with the key being the downcased name of the attribute
# and the value being the Ruby value (either the explicit `value` passed or the same as the value attr).
def enum
@enum_values ||= {}.with_indifferent_access
end
end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
describe Types::BaseEnum do
describe '#enum' do
let(:enum) do
Class.new(described_class) do
value 'TEST', value: 3
value 'other'
value 'NORMAL'
end
end
it 'adds all enum values to #enum' do
expect(enum.enum.keys).to contain_exactly('test', 'other', 'normal')
expect(enum.enum.values).to contain_exactly(3, 'other', 'NORMAL')
end
it 'is a HashWithIndefferentAccess' do
expect(enum.enum).to be_a(HashWithIndifferentAccess)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment