Sha256: 20e04170ff7021eba0e42170f6a7e4f97f64955aa79c797684b97a28d7cb1a80

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 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?(sym)
          client.parse(heredoc).tap do |fragment|
            namespace.const_set(sym, fragment)
            ::Fragment.const_set(sym, fragment) unless namespace == ::Fragment
          end
        end

        private

        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

6 entries across 6 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.3.5 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.3 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.3.1 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.2.3 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.2.2 lib/ecoportal/api/graphql/fragment.rb
ecoportal-api-graphql-0.2.1 lib/ecoportal/api/graphql/fragment.rb