Class: Expect::SeparatedPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/carat/expect.rb

Overview

A special type of pattern that matches a list of patterns, separated by a given separator, in any order.

Example: <pre>

# matches 'foo,bar' and 'bar,foo'
p = Expect::SeparatedPattern.new(['foo', 'bar'])

e = Expect.new(nil, nil)
e.expect(p)

</pre>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, separator = ',') ⇒ SeparatedPattern

Returns a new instance of SeparatedPattern.



75
76
77
78
# File 'lib/carat/expect.rb', line 75

def initialize(message, separator = ',')
  @message = message
  @separator = separator
end

Instance Attribute Details

#separatorObject

Returns the value of attribute separator.



73
74
75
# File 'lib/carat/expect.rb', line 73

def separator
  @separator
end

Instance Method Details

#===(line) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/carat/expect.rb', line 80

def ===(line)
  message = @message.dup

  # Iterate through each field to make sure it matches.  When
  # we find a match, delete that field so we never check it
  # again.
  for field in fields do
    message.each_index do |index|
      if message[index] === field then
        message.delete_at(index)
      end
    end
  end

  # If there are no more fields, then we deleted all of them,
  # and we got a match
  return message.length == 0
end