Class: Meta::JsonSchema::ScopeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/meta/json_schema/support/scope_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_clause) ⇒ ScopeMatcher

Returns a new instance of ScopeMatcher.



8
9
10
11
12
13
# File 'lib/meta/json_schema/support/scope_matcher.rb', line 8

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

Instance Attribute Details

#defined_scopesObject (readonly)

Returns the value of attribute defined_scopes.



6
7
8
# File 'lib/meta/json_schema/support/scope_matcher.rb', line 6

def defined_scopes
  @defined_scopes
end

Instance Method Details

#match?(providing_scopes) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/meta/json_schema/support/scope_matcher.rb', line 15

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