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:
|
2013-01-08 17:29:01 +00:00
|
|
|
attr_reader :failure_message_for_should
|
2010-12-15 22:34:19 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-01-08 17:29:01 +00:00
|
|
|
def failure_message_for_should_not
|
|
|
|
@failure_message_for_should_not || @failure_message_for_should
|
2010-12-15 22:34:19 +00:00
|
|
|
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)
|
2012-09-12 13:47:20 +00:00
|
|
|
allow = allow_value_matcher(value, message)
|
|
|
|
|
2010-12-15 22:34:19 +00:00
|
|
|
if allow.matches?(@subject)
|
2013-01-08 17:29:01 +00:00
|
|
|
@failure_message_for_should_not = allow.failure_message_for_should
|
2010-12-15 22:34:19 +00:00
|
|
|
true
|
|
|
|
else
|
2013-01-08 17:29:01 +00:00
|
|
|
@failure_message_for_should = allow.failure_message_for_should_not
|
2010-12-15 22:34:19 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def disallows_value_of(value, message = nil)
|
2012-09-12 13:47:20 +00:00
|
|
|
disallow = allow_value_matcher(value, message)
|
|
|
|
|
2010-12-15 22:34:19 +00:00
|
|
|
if disallow.matches?(@subject)
|
2013-01-08 17:29:01 +00:00
|
|
|
@failure_message_for_should = disallow.failure_message_for_should_not
|
2010-12-15 22:34:19 +00:00
|
|
|
false
|
|
|
|
else
|
2013-01-08 17:29:01 +00:00
|
|
|
@failure_message_for_should_not = disallow.failure_message_for_should
|
2010-12-15 22:34:19 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2012-09-12 00:53:21 +00:00
|
|
|
|
2012-09-12 13:47:20 +00:00
|
|
|
def allow_value_matcher(value, message)
|
|
|
|
matcher = AllowValueMatcher.
|
|
|
|
new(value).
|
|
|
|
for(@attribute).
|
|
|
|
with_message(message)
|
|
|
|
|
|
|
|
if strict?
|
|
|
|
matcher.strict
|
|
|
|
else
|
|
|
|
matcher
|
|
|
|
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
|