Class: QueueBus::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/queue_bus/matcher.rb

Constant Summary collapse

SPECIAL_PREPEND =
"bus_special_value_"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Matcher

Returns a new instance of Matcher.



5
6
7
# File 'lib/queue_bus/matcher.rb', line 5

def initialize(hash)
  @filters = encode(hash)
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



4
5
6
# File 'lib/queue_bus/matcher.rb', line 4

def filters
  @filters
end

Instance Method Details

#encode(hash) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/queue_bus/matcher.rb', line 68

def encode(hash)
  out = {}
  hash.each do |key, value|
    case value
    when :key, :blank, :nil, :present, :empty, :value
      value = "#{SPECIAL_PREPEND}#{value}"
    end
    out[key.to_s] = value.to_s
  end      
  out
end

#match?(attribute_name, attributes) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/queue_bus/matcher.rb', line 13

def match?(attribute_name, attributes)
  mine = filters[attribute_name].to_s
  return false if mine.size == 0
  
  given = attributes[attribute_name]
  case mine
  when "#{SPECIAL_PREPEND}key"
    return true if attributes.has_key?(attribute_name)
    return false
  when "#{SPECIAL_PREPEND}blank"
    return true if given.to_s.strip.size == 0
    return false
  when "#{SPECIAL_PREPEND}empty"
    return false if given == nil
    return true if given.to_s.size == 0
    return false
  when "#{SPECIAL_PREPEND}nil"
    return true if given == nil
    return false
  when "#{SPECIAL_PREPEND}value"
    return false if given == nil
    return true
  when "#{SPECIAL_PREPEND}present"
    return true if given.to_s.strip.size > 0
    return false
  end
  
  given = given.to_s
  
  return true if mine == given
  begin
    # if it's already a regex, don't mess with it
    # otherwise, it should have start and end line situation
    if mine[0..6] == "(?-mix:"
      regex = Regexp.new(mine)
    else
      regex = Regexp.new("^#{mine}$")
    end
    return !!regex.match(given)
  rescue
    return false
  end
end

#matches?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
# File 'lib/queue_bus/matcher.rb', line 57

def matches?(attributes)
  return false if filters.empty?
  return false if attributes == nil
  
  filters.keys.each do |key|
    return false unless match?(key, attributes)
  end
  
  true
end

#to_redisObject



9
10
11
# File 'lib/queue_bus/matcher.rb', line 9

def to_redis
  @filters
end