module Shoulda # :nodoc:
module Matchers
module ActiveRecord # :nodoc:
# Ensures that the model is not valid if the given attribute is not
# formatted correctly.
#
# Options:
# * with_message - value the test expects to find in
# errors.on(:attribute). Regexp or String.
# Defaults to the translation for :blank.
# * with(string to test against)
# * not_with(string to test against)
#
# Examples:
# it { should validate_format_of(:name).
# with('12345').
# with_message(/is not optional/) }
# it { should validate_format_of(:name).
# not_with('12D45').
# with_message(/is not optional/) }
#
def validate_format_of(attr)
ValidateFormatOfMatcher.new(attr)
end
class ValidateFormatOfMatcher < ValidationMatcher # :nodoc:
def initialize(attribute)
super
end
def with_message(message)
@expected_message = message if message
self
end
def with(value)
raise "You may not call both with and not_with" if @value_to_fail
@value_to_pass = value
self
end
def not_with(value)
raise "You may not call both with and not_with" if @value_to_pass
@value_to_fail = value
self
end
def matches?(subject)
super(subject)
@expected_message ||= :blank
return disallows_value_of(@value_to_fail, @expected_message) if @value_to_fail
allows_value_of(@value_to_pass, @expected_message) if @value_to_pass
end
def description
"#{@attribute} have a valid format"
end
end
end
end
end