class Oval::Match

Attributes

re[R]

Public Class Methods

[](re) click to toggle source
# File lib/oval/match.rb, line 28
def self.[](re)
  new(re)
end
new(re) click to toggle source
# File lib/oval/match.rb, line 32
def initialize(re)
  self.re = re
end

Private Class Methods

validate_re(re) click to toggle source
# File lib/oval/match.rb, line 45
def self.validate_re(re)
  unless re.is_a?(Regexp)
    raise Oval::DeclError, "Invalid regular expression #{re.inspect}. " +
      "Should be an instance of Regexp"
  end
end

Public Instance Methods

it_should() click to toggle source
# File lib/oval/match.rb, line 24
def it_should
  "match #{re.inspect}"
end
validate(thing, subject = nil) click to toggle source
# 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

Private Instance Methods

re=(re) click to toggle source
# File lib/oval/match.rb, line 40
def re=(re)
  self.class.validate_re(re)
  @re = re
end