Class: Oval::Match

Inherits:
Base
  • Object
show all
Defined in:
lib/oval/match.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

ensure_equal, it_should, validate

Constructor Details

#initialize(re) ⇒ Match

Returns a new instance of Match.


32
33
34
# File 'lib/oval/match.rb', line 32

def initialize(re)
  self.re = re
end

Instance Attribute Details

#reObject

Returns the value of attribute re


36
37
38
# File 'lib/oval/match.rb', line 36

def re
  @re
end

Class Method Details

.[](re) ⇒ Object


28
29
30
# File 'lib/oval/match.rb', line 28

def self.[](re)
  new(re)
end

Instance Method Details

#it_shouldObject


24
25
26
# File 'lib/oval/match.rb', line 24

def it_should
  "match #{re.inspect}"
end

#validate(thing, subject = nil) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/oval/match.rb', line 5

def validate(thing, subject = nil)
  begin
    unless re.match(thing)
      raise Oval::ValueError,
        "Invalid value #{thing.inspect}#{for_subject(subject)}. " +
        "Should #{it_should}"
    end
  rescue TypeError => err
    ere = /(?:can't convert|no implicit conversion of) \S+ (?:in)?to String/
    if ere.match(err.message)
      raise Oval::ValueError,
        "Invalid value #{thing.inspect}#{for_subject(subject)}. " +
        "Should #{it_should} but it's not even convertible to String"
    else
      raise
    end
  end
end