class GraphqlRails::Model::BuildEnumType

contains info about single graphql attribute

Attributes

allowed_values[R]
description[R]
name[R]

Public Class Methods

new(name, allowed_values:, description: nil) click to toggle source
# File lib/graphql_rails/model/build_enum_type.rb, line 15
def initialize(name, allowed_values:, description: nil)
  @name = name
  @allowed_values = allowed_values
  @description = description
end

Protected Class Methods

inspect() click to toggle source
# File lib/graphql_rails/model/build_enum_type.rb, line 61
def self.inspect
  "#{GraphQL::Schema::Enum}(#{graphql_name})"
end

Public Instance Methods

call() click to toggle source
# File lib/graphql_rails/model/build_enum_type.rb, line 21
def call
  validate
  build_enum
end

Protected Instance Methods

build_enum(allowed_values: self.allowed_values, enum_name: formatted_name, enum_description: description) click to toggle source
# File lib/graphql_rails/model/build_enum_type.rb, line 53
def build_enum(allowed_values: self.allowed_values, enum_name: formatted_name, enum_description: description)
  Class.new(GraphQL::Schema::Enum) do
    allowed_values.each do |allowed_value|
      graphql_name(enum_name)
      description(enum_description) if enum_description
      value(allowed_value.to_s.underscore.upcase, value: allowed_value)
    end

    def self.inspect
      "#{GraphQL::Schema::Enum}(#{graphql_name})"
    end
  end
end
formatted_name() click to toggle source
# File lib/graphql_rails/model/build_enum_type.rb, line 49
def formatted_name
  name.to_s.camelize
end
validate() click to toggle source
# File lib/graphql_rails/model/build_enum_type.rb, line 30
def validate
  return if allowed_values.is_a?(Array) && !allowed_values.empty?

  validate_enum_type
  validate_enum_content
end
validate_enum_content() click to toggle source
# File lib/graphql_rails/model/build_enum_type.rb, line 43
def validate_enum_content
  return unless allowed_values.empty?

  raise InvalidEnum, 'At lest one enum option must be given'
end
validate_enum_type() click to toggle source
# File lib/graphql_rails/model/build_enum_type.rb, line 37
def validate_enum_type
  return if allowed_values.is_a?(Array)

  raise InvalidEnum, "Enum must be instance of Array, but instance of #{allowed_values.class} was given"
end