Sha256: 33cd629d771a3d50041e826776c43415e46e502fa1e09b1482ebcf618d5a022d
Contents?: true
Size: 1.85 KB
Versions: 3
Compression:
Stored size: 1.85 KB
Contents
# frozen_string_literal: true module GraphQL module Define # 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. class DefinedObjectProxy # The object which will be defined by definition functions attr_reader :target def initialize(target) @target = target @dictionary = target.class.dictionary end # Provides shorthand access to GraphQL's built-in types def types GraphQL::Define::TypeDefiner.instance end # Allow `plugin` to perform complex initialization on the definition. # Calls `plugin.use(defn, **kwargs)`. # @param plugin [<#use(defn, **kwargs)>] A plugin object # @param kwargs [Hash] Any options for the plugin def use(plugin, **kwargs) # https://bugs.ruby-lang.org/issues/10708 if kwargs == {} plugin.use(self) else plugin.use(self, **kwargs) end end # Lookup a function from the dictionary and call it if it's found. def method_missing(name, *args, **kwargs, &block) definition = @dictionary[name] if definition # Avoid passing `kwargs` when it's not used. # Ruby 2.7 does fine here, but older Rubies receive too many arguments. if kwargs.any? definition.call(@target, *args, **kwargs, &block) else definition.call(@target, *args, &block) end else msg = "#{@target.class.name} can't define '#{name}'" raise NoDefinitionError, msg, caller end end def respond_to_missing?(name, include_private = false) @dictionary[name] || super end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
graphql-1.10.0 | lib/graphql/define/defined_object_proxy.rb |
graphql-1.10.0.pre4 | lib/graphql/define/defined_object_proxy.rb |
graphql-1.9.18 | lib/graphql/define/defined_object_proxy.rb |