Sha256: bb3fb26453812ac858a0c3c18abda6c938b6d1db87e6f06b37c64edbc2a8ee37

Contents?: true

Size: 1.66 KB

Versions: 77

Compression:

Stored size: 1.66 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
      extend GraphQL::Ruby2Keywords
      # 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, &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
      ruby2_keywords :method_missing

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

Version data entries

77 entries across 77 versions & 2 rubygems

Version Path
graphql-1.11.12 lib/graphql/define/defined_object_proxy.rb
graphql-1.11.11 lib/graphql/define/defined_object_proxy.rb
graphql-1.12.25 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.24 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.23 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.22 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.21 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.20 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.19 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.18 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.17 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.16 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.15 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.14 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.13 lib/graphql/define/defined_object_proxy.rb
graphql_cody-1.13.0 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.12 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.11 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.10 lib/graphql/define/defined_object_proxy.rb
graphql-1.13.9 lib/graphql/define/defined_object_proxy.rb