Sha256: c522905191556c425d4a62954cd988afc5fe3a3fd24cf55cc4be1f364860a4ca

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

# Namespace to create custom fragments from client scripts
module Fragment
  include Ecoportal::API::Common::GraphQL::ClassHelpers
end
# Class to define/parse fragments
module Ecoportal
  module API
    class GraphQL
      class Fragment
        include Ecoportal::API::Common::GraphQL::ClassHelpers

        class << self
          def fragment(sym, heredoc)
            fragments[sym] = heredoc
          end

          def fragments
            @fragments ||= {}
          end
        end

        attr_reader :client, :fragments

        def initialize(client)
          @client = client
          parse
        end

        def define(sym, heredoc, namespace: ::Fragment)
          namespace.send(:remove_const, sym) if namespace.const_defined?(sym)
          client.parse(heredoc).tap do |fragment|
            without_warnings do
              namespace.const_set(sym, fragment)
              ::Fragment.const_set(sym, fragment) unless namespace == ::Fragment
            end
          end
        end

        private

        def without_warnings
          original_verbose, $VERBOSE = $VERBOSE, nil
          yield if block_given?
        ensure
          $VERBOSE = original_verbose
        end

        def parse
          fragments = self.class.fragments.each_with_object({}) do |(sym, heredoc), out|
            out[sym] = define(sym, heredoc, namespace: self.class)
          end
        end
      end
    end
  end
end

require 'ecoportal/api/graphql/fragment/pagination'
require 'ecoportal/api/graphql/fragment/location_node'
require 'ecoportal/api/graphql/fragment/action'
require 'ecoportal/api/graphql/fragment/contractor_entity'

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.3.16 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.15 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.14 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.13 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.12 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.11 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.10 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.9 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.8 lib/ecoportal/api/graphql/fragment.rb