Module: Philiprehberger::EventEmitter::Pattern

Defined in:
lib/philiprehberger/event_emitter/pattern.rb

Overview

Glob-style pattern matcher for wildcard event subscriptions. Segments are separated by ‘.`. `*` matches exactly one segment. `**` matches zero or more segments.

Class Method Summary collapse

Class Method Details

.match?(pattern, event_name) ⇒ Boolean

Match an event name against a glob-style pattern.

Parameters:

  • pattern (String)

    the glob pattern (e.g. “user.*” or “app.**”)

  • event_name (String)

    the actual event name (e.g. “user.created”)

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/philiprehberger/event_emitter/pattern.rb', line 25

def match?(pattern, event_name)
  pattern_segments = pattern.to_s.split('.')
  event_segments = event_name.to_s.split('.')
  segments_match?(pattern_segments, 0, event_segments, 0)
end

.wildcard?(pattern) ⇒ Boolean

Check if a string contains wildcard characters.

Parameters:

  • pattern (String)

    the pattern to check

Returns:

  • (Boolean)


16
17
18
# File 'lib/philiprehberger/event_emitter/pattern.rb', line 16

def wildcard?(pattern)
  pattern.is_a?(String) && pattern.include?('*')
end