Class: ProcessQueryLanguage::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/process-query-language/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Matcher

pattern: { a => { $gt => 100 }, b => { $in => [ 1,2,3 ] } }



9
10
11
# File 'lib/process-query-language/matcher.rb', line 9

def initialize(pattern)
  @pattern = pattern
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



6
7
8
# File 'lib/process-query-language/matcher.rb', line 6

def pattern
  @pattern
end

Instance Method Details

#apply(op, value, pattern) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/process-query-language/matcher.rb', line 13

def apply(op, value, pattern)
  case op
  when "$gt"
    return value > pattern
  when "$gte"
    return value >= pattern
  when "$lt"
    return value < pattern
  when "$lte"
    return value <= pattern
  when "$in"
    return pattern.include?(value)
  when "$nin"
    return !pattern.include?(value)
  else
    raise Exception.new("Unknown operator: #{op}")
  end
end

#match(object) ⇒ Object

object: { a => 200, b => 2 }



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/process-query-language/matcher.rb', line 33

def match(object)
  pattern.each do |key, value|
    return false if object[key].nil?

    case value
    when Regexp
      return false if !value.match(object[key])
    when Hash
      value.each do |op, value|
        return false if !apply(op, object[key], value)
      end
    when object[key]
      return false if object[key] != value
    else
      return false
    end
  end

  return true
end