Sha256: 08e2770ef4ee13af4e721a94940de6e158d1ba378d91b44b09cb130eda4275b6

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module GQLi
  # Base class for GraphQL type wrappers
  class Base
    attr_reader :__name, :__depth, :__nodes

    def initialize(name = nil, depth = 0, &block)
      @__name = name
      @__depth = depth
      @__nodes = []
      instance_eval(&block) unless block.nil?
    end

    # Inlines fragment nodes into current node
    def ___(fragment)
      @__nodes += __clone_nodes(fragment)
    end

    # Adds type match node
    def __on(type_name, &block)
      require_relative './node'
      @__nodes << Node.new("... on #{type_name}", [], __depth + 1, &block)
    end

    protected

    def __clone_nodes(node_container)
      require_relative './node'
      __clone(node_container.__nodes).map do |n|
        node = Node.new(n.__name, n.__params, __depth + 1)
        node.instance_variable_set(
          :@__nodes,
          node.send(:__clone_nodes, n)
        )
        node
      end
    end

    def __clone(obj)
      Marshal.load(Marshal.dump(obj))
    end

    def __params_from_args(args)
      args.empty? ? {} : args[0]
    end

    def method_missing(name, *args, &block)
      require_relative './node'
      @__nodes << Node.new(name.to_s, __params_from_args(args), __depth + 1, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gqli-0.1.0 lib/gqli/base.rb