Sha256: 29b132c97a65cd45c13014131bd93ffe578e6b9d540ad0fb79a162c4038b8928

Contents?: true

Size: 927 Bytes

Versions: 2

Compression:

Stored size: 927 Bytes

Contents

# frozen_string_literal: true

module Meta
  module JsonSchema
    class ScopeMatcher
      attr_reader :defined_scopes

      def initialize(query_clause)
        query_clause = [query_clause] if query_clause.is_a?(String) || query_clause.is_a?(Symbol)
        query_clause = { all_of: query_clause } if query_clause.is_a?(Array)

        @match_type, @defined_scopes = query_clause.first
      end

      def match?(providing_scopes)
        # 目前认为空数组就是不做 scope 筛选
        return false if providing_scopes.empty?

        case @match_type
        when :some_of
          # 只要相交就可以
          (@defined_scopes & providing_scopes).any?
        when :all_of
          # @defined_scopes 一定要被包含在 providing_scopes 内
          (@defined_scopes - providing_scopes).empty?
        else
          raise "Unknown match type: #{@match_type}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
meta-api-0.2.0 lib//meta/json_schema/support/scope_matcher.rb
meta-api-0.1.2 lib//meta/json_schema/support/scope_matcher.rb