Class: QueueBus::Matcher
- Inherits:
-
Object
- Object
- QueueBus::Matcher
- Defined in:
- lib/queue_bus/matcher.rb
Overview
Tests whether a field on a bus event matches a filter.
Constant Summary collapse
- SPECIAL_PREPEND =
'bus_special_value_'
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
Instance Method Summary collapse
- #encode(hash) ⇒ Object
-
#initialize(hash) ⇒ Matcher
constructor
A new instance of Matcher.
- #match?(attribute_name, attributes) ⇒ Boolean
- #matches?(attributes) ⇒ Boolean
- #to_redis ⇒ Object
Constructor Details
#initialize(hash) ⇒ Matcher
Returns a new instance of Matcher.
8 9 10 |
# File 'lib/queue_bus/matcher.rb', line 8 def initialize(hash) @filters = encode(hash) end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
7 8 9 |
# File 'lib/queue_bus/matcher.rb', line 7 def filters @filters end |
Instance Method Details
#encode(hash) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/queue_bus/matcher.rb', line 78 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
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 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/queue_bus/matcher.rb', line 16 def match?(attribute_name, attributes) mine = filters[attribute_name].to_s return false if mine.empty? given = attributes[attribute_name] case mine when "#{SPECIAL_PREPEND}key" return true if attributes.key?(attribute_name) return false when "#{SPECIAL_PREPEND}blank" return true if given.to_s.strip.empty? return false when "#{SPECIAL_PREPEND}empty" return false if given.nil? return true if given.to_s.empty? 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 unless given.to_s.strip.empty? 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 regex = if mine[0..6] == '(?-mix:' Regexp.new(mine) else Regexp.new("^#{mine}$") end return !!regex.match(given) rescue StandardError return false end end |
#matches?(attributes) ⇒ Boolean
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/queue_bus/matcher.rb', line 67 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_redis ⇒ Object
12 13 14 |
# File 'lib/queue_bus/matcher.rb', line 12 def to_redis @filters end |