Class: Kanji::Type

Inherits:
Object
  • Object
show all
Extended by:
Dry::Container::Mixin, ClassInterface
Includes:
Dry::Container::Mixin
Defined in:
lib/kanji/type.rb,
lib/kanji/type/argument.rb,
lib/kanji/type/mutation.rb,
lib/kanji/type/attribute.rb,
lib/kanji/type/class_interface.rb,
lib/kanji/type/mutation_definer.rb,
lib/kanji/type/attribute_definer.rb

Defined Under Namespace

Modules: ClassInterface Classes: Argument, Attribute, AttributeDefiner, Mutation, MutationDefiner

Instance Attribute Summary

Attributes included from ClassInterface

#_associations, #_attributes, #_description, #_name, #_repo_name

Instance Method Summary collapse

Methods included from ClassInterface

assoc, attribute, create, create_value_object, demodulized_type_name, description, destroy, finalize, get_repo_name, graphql_type, inherited, name, register_schema, update

Constructor Details

#initialize(params) ⇒ Type

Returns a new instance of Type.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kanji/type.rb', line 13

def initialize(params)
  result = self.class.resolve(:schema).call(params)

  if result.success?
    register :value, -> { self.class.resolve(:value_object).new(params) }
  else
    errors = parse_error_messages(result)
    raise ValidationError, "Schema validation failed - #{errors}"
  end

  self.class.instance_variable_get(:@_values).each do |key, value|
    register key.to_sym, value
  end

  if repo = self.class.resolve(:repo)
    register :repo, repo
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/kanji/type.rb', line 40

def method_missing(method_name, *args, &block)
  if resolve(:value).respond_to?(method_name)
    resolve(:value).send(method_name, *args)
  else
    super(method_name, *args, &block)
  end
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/kanji/type.rb', line 48

def respond_to_missing?(method_name, include_private = false)
  if resolve(:value).respond_to?(method_name)
    true
  else
    super(method_name, include_private)
  end
end

#to_hObject



32
33
34
# File 'lib/kanji/type.rb', line 32

def to_h
  resolve(:value).to_h
end

#to_jsonObject



36
37
38
# File 'lib/kanji/type.rb', line 36

def to_json
  to_h.to_json
end