Class: Mobb::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mobb/base.rb

Defined Under Namespace

Classes: Matched

Instance Method Summary collapse

Constructor Details

#initialize(pattern, options = {}) ⇒ Matcher

Returns a new instance of Matcher.



9
# File 'lib/mobb/base.rb', line 9

def initialize(pattern, options = {}) @pattern, @options = pattern, options; end

Instance Method Details

#cron?Boolean

Returns:

  • (Boolean)


12
# File 'lib/mobb/base.rb', line 12

def cron?; pattern.is_a?(CronParser); end

#cron_matcher(time) ⇒ Object



51
52
53
54
55
56
# File 'lib/mobb/base.rb', line 51

def cron_matcher(time)
  last = last_tick
  tick(time) if !last || time > last
  return false if time < last
  pattern.next(time) == pattern.next(last) ? false : true
end

#inspectObject



11
# File 'lib/mobb/base.rb', line 11

def inspect; "pattern: #{@pattern}, options #{@options}"; end

#last_tickObject



14
# File 'lib/mobb/base.rb', line 14

def last_tick; @options[:last_tick] end

#match?(context) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mobb/base.rb', line 16

def match?(context)
  case context
  when String
    string_matcher(context)
  when Time
    cron_matcher(context)
  when Array
    context.all? { |c| match?(c) }
  else
    false
  end
end

#patternObject



34
# File 'lib/mobb/base.rb', line 34

def pattern; @pattern; end

#regexp?Boolean

Returns:

  • (Boolean)


10
# File 'lib/mobb/base.rb', line 10

def regexp?; pattern.is_a?(Regexp); end

#string_matcher(string) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mobb/base.rb', line 36

def string_matcher(string)
  case pattern
  when Regexp
    if res = pattern.match(string)
      Matched.new(pattern, res)
    else
      false
    end
  when String
    @options[:laziness] ? string.include?(pattern) : string == pattern
  else
    false
  end
end

#tick(time = Time.now) ⇒ Object



13
# File 'lib/mobb/base.rb', line 13

def tick(time = Time.now) @options[:last_tick] = time; end