2010-12-15 22:34:19 +00:00
|
|
|
module Shoulda # :nodoc:
|
|
|
|
module Matchers
|
2011-05-06 11:51:12 +00:00
|
|
|
module ActiveModel # :nodoc:
|
2010-12-15 22:34:19 +00:00
|
|
|
class ValidationMatcher # :nodoc:
|
|
|
|
attr_reader :failure_message
|
|
|
|
|
|
|
|
def initialize(attribute)
|
|
|
|
@attribute = attribute
|
2012-09-12 00:53:21 +00:00
|
|
|
@strict = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def strict
|
|
|
|
@strict = true
|
|
|
|
self
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
|
|
|
@negative_failure_message || @failure_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(subject)
|
|
|
|
@subject = subject
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-04-20 14:35:18 +00:00
|
|
|
|
2010-12-15 22:34:19 +00:00
|
|
|
def allows_value_of(value, message = nil)
|
|
|
|
allow = AllowValueMatcher.
|
|
|
|
new(value).
|
|
|
|
for(@attribute).
|
|
|
|
with_message(message)
|
2012-09-12 00:53:21 +00:00
|
|
|
if strict?
|
|
|
|
allow = allow.strict
|
|
|
|
end
|
2010-12-15 22:34:19 +00:00
|
|
|
if allow.matches?(@subject)
|
|
|
|
@negative_failure_message = allow.failure_message
|
|
|
|
true
|
|
|
|
else
|
|
|
|
@failure_message = allow.negative_failure_message
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def disallows_value_of(value, message = nil)
|
|
|
|
disallow = AllowValueMatcher.
|
|
|
|
new(value).
|
|
|
|
for(@attribute).
|
|
|
|
with_message(message)
|
2012-09-12 00:53:21 +00:00
|
|
|
if strict?
|
|
|
|
disallow = disallow.strict
|
|
|
|
end
|
2010-12-15 22:34:19 +00:00
|
|
|
if disallow.matches?(@subject)
|
|
|
|
@failure_message = disallow.negative_failure_message
|
|
|
|
false
|
|
|
|
else
|
|
|
|
@negative_failure_message = disallow.failure_message
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2012-09-12 00:53:21 +00:00
|
|
|
|
|
|
|
def strict?
|
|
|
|
@strict
|
|
|
|
end
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-23 14:50:45 +00:00
|
|
|
end
|