Class: GraphQL::Define::DefinedObjectProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/define/defined_object_proxy.rb

Overview

This object delegates most methods to a dictionary of functions, @dictionary. @target is passed to the specified function, along with any arguments and block. This allows a method-based DSL without adding methods to the defined class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ DefinedObjectProxy

Returns a new instance of DefinedObjectProxy


11
12
13
14
# File 'lib/graphql/define/defined_object_proxy.rb', line 11

def initialize(target)
  @target = target
  @dictionary = target.class.dictionary
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Lookup a function from the dictionary and call it if it's found.


35
36
37
38
39
40
41
42
43
# File 'lib/graphql/define/defined_object_proxy.rb', line 35

def method_missing(name, *args, &block)
  definition = @dictionary[name]
  if definition
    definition.call(@target, *args, &block)
  else
    msg = "#{@target.class.name} can't define '#{name}'"
    raise NoDefinitionError, msg, caller
  end
end

Instance Attribute Details

#targetObject (readonly)

The object which will be defined by definition functions


9
10
11
# File 'lib/graphql/define/defined_object_proxy.rb', line 9

def target
  @target
end

Instance Method Details

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

Returns:

  • (Boolean)

45
46
47
# File 'lib/graphql/define/defined_object_proxy.rb', line 45

def respond_to_missing?(name, include_private = false)
  @dictionary[name] || super
end

#typesObject

Provides shorthand access to GraphQL's built-in types


17
18
19
# File 'lib/graphql/define/defined_object_proxy.rb', line 17

def types
  GraphQL::Define::TypeDefiner.instance
end

#use(plugin, **kwargs) ⇒ Object

Allow plugin to perform complex initialization on the definition. Calls plugin.use(defn, **kwargs).

Parameters:

  • plugin (<#use(defn, **kwargs)>)

    A plugin object

  • kwargs (Hash)

    Any options for the plugin


25
26
27
28
29
30
31
32
# File 'lib/graphql/define/defined_object_proxy.rb', line 25

def use(plugin, **kwargs)
  # https://bugs.ruby-lang.org/issues/10708
  if kwargs == {}
    plugin.use(self)
  else
    plugin.use(self, **kwargs)
  end
end