Class: RSMP::StatusMatcher

Inherits:
Matcher
  • Object
show all
Defined in:
lib/rsmp/collect/status_matcher.rb

Overview

Match a specific status

Instance Attribute Summary

Attributes inherited from Matcher

#got, #message, #want

Instance Method Summary collapse

Methods inherited from Matcher

#done?, #forget, #initialize, #keep, #perform_match

Constructor Details

This class inherits a constructor from RSMP::Matcher

Instance Method Details

#match(item) ⇒ Object



33
34
35
36
37
# File 'lib/rsmp/collect/status_matcher.rb', line 33

def match(item)
  return nil unless match_code(item)

  match_value?(item)
end

#match_code(item) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/rsmp/collect/status_matcher.rb', line 4

def match_code(item)
  return nil if @want['sCI'] && @want['sCI'] != item['sCI']
  return nil if @want['cO'] && @want['cO'] != item['cO']
  return nil if @want['n'] && @want['n'] != item['n']

  true
end

#match_value?(item) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rsmp/collect/status_matcher.rb', line 12

def match_value?(item)
  return false if @want['q'] && @want['q'] != item['q']
  return true unless @want.key?('s')

  want = @want['s']
  got = item['s']
  if want.is_a? Regexp
    return false unless regex_match?(got, want)
  elsif got != want
    return false
  end
  true
end

#regex_match?(got, want) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/rsmp/collect/status_matcher.rb', line 26

def regex_match?(got, want)
  return got =~ want if got.is_a?(String)
  return got.any? { |item| item.is_a?(String) && item =~ want } if got.is_a?(Array)

  false
end