Module: Legion::Extensions::React::Helpers::EventMatcher

Defined in:
lib/legion/extensions/react/helpers/event_matcher.rb

Class Method Summary collapse

Class Method Details

.evaluate_condition(condition, event) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/react/helpers/event_matcher.rb', line 21

def evaluate_condition(condition, event)
  return true if condition.nil? || condition.strip.empty?

  # Parse simple conditions: "key == 'value'" or "key != 'value'"
  if condition =~ /\A(\w+)\s*(==|!=)\s*'([^']*)'\z/
    key    = Regexp.last_match(1).to_sym
    op     = Regexp.last_match(2)
    value  = Regexp.last_match(3)
    actual = event[key]&.to_s

    case op
    when '==' then actual == value
    when '!=' then actual != value
    else false
    end
  else
    false
  end
rescue StandardError
  false
end

.match?(pattern, event_name) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# File 'lib/legion/extensions/react/helpers/event_matcher.rb', line 10

def match?(pattern, event_name)
  # Use placeholder so '.**' double-star replacement isn't re-processed by single '*' gsub
  regex_str = pattern.gsub('.**', '__DS__')
                     .gsub('*', '[^.]*')
                     .gsub('__DS__', '\..*')
  regex_str = "\\A#{regex_str}\\z"
  Regexp.new(regex_str).match?(event_name)
rescue RegexpError
  false
end