module Kanji::Type::ClassInterface

Attributes

_associations[R]
_attributes[R]
_description[R]
_name[R]
_repo_name[R]

Public Instance Methods

assoc(name, type = nil, description = nil, **kwargs, &block) click to toggle source
# File lib/kanji/type/class_interface.rb, line 73
def assoc(name, type = nil, description = nil, **kwargs, &block)
  if @_associations.map(&:name).include?(name)
    fail AttributeError, "Association #{name} is already defined"
  else
    @_associations <<
    AttributeDefiner.new(name, type, description, kwargs, &block).call
  end
end
attribute(name, type = nil, description = nil, **kwargs, &block) click to toggle source
# File lib/kanji/type/class_interface.rb, line 64
def attribute(name, type = nil, description = nil, **kwargs, &block)
  if @_attributes.map(&:name).include?(name)
    fail AttributeError, "Attribute #{name} is already defined"
  else
    @_attributes <<
      AttributeDefiner.new(name, type, description, kwargs, &block).call
  end
end
create(&block) click to toggle source
# File lib/kanji/type/class_interface.rb, line 86
def create(&block)
  register :create_mutation do
    Kanji::Graph::RegisterMutation.new(
      return_type: resolve(:graphql_type),
      attributes: @_attributes.reject { |attr| attr.name == :id },
      name: "Create#{demodulized_type_name}Mutation",
      description: "Create a new #{demodulized_type_name}.",
      resolve: block
    ).call
  end
end
create_value_object() click to toggle source
# File lib/kanji/type/class_interface.rb, line 138
def create_value_object
  builder = Dry::Core::ClassBuilder.new(
    name: "#{instance_variable_get(:@_name)}Value",
    parent: Dry::Struct::Value
  )
  klass = builder.call

  klass.constructor_type(:schema)

  instance_variable_get(:@_attributes).each do |attribute|
    klass.attribute(attribute.name, attribute.type)
  end

  klass
end
demodulized_type_name() click to toggle source
# File lib/kanji/type/class_interface.rb, line 82
def demodulized_type_name
  @_demodulized_type_name ||= Dry::Core::Inflector.demodulize(self.to_s)
end
description(description) click to toggle source
# File lib/kanji/type/class_interface.rb, line 60
def description(description)
  @_description = description
end
destroy(&block) click to toggle source
# File lib/kanji/type/class_interface.rb, line 110
def destroy(&block)
  register :destroy_mutation do
    Kanji::Graph::RegisterMutation.new(
      return_type: resolve(:graphql_type),
      attributes: [@_attributes.find { |attr| attr.name == :id }],
      name: "Destroy#{demodulized_type_name}Mutation",
      description: "Destroy a #{demodulized_type_name}.",
      resolve: block
    ).call
  end
end
finalize(klass) click to toggle source
# File lib/kanji/type/class_interface.rb, line 44
def finalize(klass)
  klass.register :graphql_type, graphql_type(klass)
  klass.register :schema, -> { klass.register_schema }
  klass.register :value_object, -> { klass.create_value_object }
  klass.instance_variable_set(:@_repo_name, get_repo_name(klass))
end
get_repo_name(klass) click to toggle source
# File lib/kanji/type/class_interface.rb, line 51
def get_repo_name(klass)
  name = Dry::Core::Inflector.underscore(klass._name)
  Dry::Core::Inflector.pluralize(name).to_sym
end
graphql_type(klass) click to toggle source
# File lib/kanji/type/class_interface.rb, line 19
def graphql_type(klass)
  Kanji::Graph::RegisterObject.new(
    attributes: klass._attributes + klass._associations,
    name: klass._name,
    description: klass._description
  ).call
end
inherited(klass) click to toggle source
Calls superclass method
# File lib/kanji/type/class_interface.rb, line 27
def inherited(klass)
  super

  klass.instance_variable_set(:@_attributes, [])
  klass.instance_variable_set(:@_values, {})
  klass.instance_variable_set(:@_associations, [])

  klass.attribute(:id, Kanji::Types::Int, "The primary key")

  TracePoint.trace(:end) do |t|
    if klass == t.self
      self.finalize(klass)
      t.disable
    end
  end
end
name(name) click to toggle source
# File lib/kanji/type/class_interface.rb, line 56
def name(name)
  @_name = name
end
register_schema() click to toggle source
# File lib/kanji/type/class_interface.rb, line 122
def register_schema
  attributes = _attributes

  Dry::Validation.JSON do
    configure { config.type_specs = true }

    attributes.each do |attribute|
      if attribute.options[:required]
        required(attribute.name, attribute.type).filled
      else
        optional(attribute.name, attribute.type).maybe
      end
    end
  end
end
update(&block) click to toggle source
# File lib/kanji/type/class_interface.rb, line 98
def update(&block)
  register :update_mutation do
    Kanji::Graph::RegisterMutation.new(
      return_type: resolve(:graphql_type),
      attributes: @_attributes,
      name: "Update#{demodulized_type_name}Mutation",
      description: "Update an instance of #{demodulized_type_name}.",
      resolve: block
    ).call
  end
end