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.



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

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

Instance Method Details

#inspectObject



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

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

#invoke(time = Time.now) ⇒ Object



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

def invoke(time = Time.now) @options[:last_invoked] = time; end

#match?(context) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mobb/base.rb', line 11

def match?(context)
  case context
  when String
    string_matcher(context)
  when Time
    # TODO: do something
    false
  when Array
    context.all? { |c| match?(c) }
  else
    false
  end
end

#patternObject



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

def pattern; @pattern; end

#regexp?Boolean

Returns:

  • (Boolean)


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

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

#string_matcher(string) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mobb/base.rb', line 32

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