Sha256: 81a16c909eb59bf8294ef95b0805930ab9e785206e4e6fb3f6572c0ba95792e2

Contents?: true

Size: 1.2 KB

Versions: 102

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true
module GraphQL
  # @api private
  class Filter
    def initialize(only: nil, except: nil)
      @only = only
      @except = except
    end

    # Returns true if `member, ctx` passes this filter
    def call(member, ctx)
      (@only ? @only.call(member, ctx) : true) &&
      (@except ? !@except.call(member, ctx) : true)
    end

    def merge(only: nil, except: nil)
      onlies = [self].concat(Array(only))
      merged_only = MergedOnly.build(onlies)
      merged_except = MergedExcept.build(Array(except))
      self.class.new(only: merged_only, except: merged_except)
    end

    private

    class MergedOnly
      def initialize(first, second)
        @first = first
        @second = second
      end

      def call(member, ctx)
        @first.call(member, ctx) && @second.call(member, ctx)
      end

      def self.build(onlies)
        case onlies.size
        when 0
          nil
        when 1
          onlies[0]
        else
          onlies.reduce { |memo, only| self.new(memo, only) }
        end
      end
    end

    class MergedExcept < MergedOnly
      def call(member, ctx)
        @first.call(member, ctx) || @second.call(member, ctx)
      end
    end
  end
end

Version data entries

102 entries across 102 versions & 2 rubygems

Version Path
graphql-1.13.23 lib/graphql/filter.rb
graphql-1.13.22 lib/graphql/filter.rb
graphql-1.13.21 lib/graphql/filter.rb
graphql-1.13.20 lib/graphql/filter.rb
graphql-2.0.20 lib/graphql/filter.rb
graphql-2.0.17.2 lib/graphql/filter.rb
graphql-2.0.17.1 lib/graphql/filter.rb
graphql-2.0.19 lib/graphql/filter.rb
graphql-2.0.18 lib/graphql/filter.rb
graphql-2.0.17 lib/graphql/filter.rb
graphql-1.13.19 lib/graphql/filter.rb
graphql-1.13.18 lib/graphql/filter.rb
graphql-2.0.16 lib/graphql/filter.rb
graphql-1.13.17 lib/graphql/filter.rb
graphql-2.0.15 lib/graphql/filter.rb
graphql-2.0.14 lib/graphql/filter.rb
graphql-1.13.16 lib/graphql/filter.rb
graphql-2.0.13 lib/graphql/filter.rb
graphql-2.0.12 lib/graphql/filter.rb
graphql-1.13.15 lib/graphql/filter.rb