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-12-24 11:24:27 +00:00
|
|
|
attr_reader :failure_message
|
|
|
|
|
|
|
|
alias failure_message_for_should failure_message
|
2010-12-15 22:34:19 +00:00
|
|
|
|
|
|
|
def initialize(attribute)
|
|
|
|
@attribute = attribute
|
2012-09-12 00:53:21 +00:00
|
|
|
@strict = false
|
|
|
|
end
|
|
|
|
|
2013-03-04 03:34:38 +00:00
|
|
|
def on(context)
|
|
|
|
@context = context
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-09-12 00:53:21 +00:00
|
|
|
def strict
|
|
|
|
@strict = true
|
|
|
|
self
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
def failure_message_when_negated
|
|
|
|
@failure_message_when_negated || @failure_message
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
2013-12-24 11:24:27 +00:00
|
|
|
alias failure_message_for_should_not failure_message_when_negated
|
2010-12-15 22:34:19 +00:00
|
|
|
|
|
|
|
def matches?(subject)
|
|
|
|
@subject = subject
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-04-20 14:35:18 +00:00
|
|
|
|
2013-07-24 21:46:01 +00:00
|
|
|
def allows_value_of(value, message = nil, &block)
|
2012-09-12 13:47:20 +00:00
|
|
|
allow = allow_value_matcher(value, message)
|
2013-07-24 21:46:01 +00:00
|
|
|
yield allow if block_given?
|
2012-09-12 13:47:20 +00:00
|
|
|
|
2010-12-15 22:34:19 +00:00
|
|
|
if allow.matches?(@subject)
|
2013-12-24 11:24:27 +00:00
|
|
|
@failure_message_when_negated = allow.failure_message_when_negated
|
2010-12-15 22:34:19 +00:00
|
|
|
true
|
|
|
|
else
|
2013-12-24 11:24:27 +00:00
|
|
|
@failure_message = allow.failure_message
|
2010-12-15 22:34:19 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-24 21:46:01 +00:00
|
|
|
def disallows_value_of(value, message = nil, &block)
|
2013-03-27 18:13:46 +00:00
|
|
|
disallow = disallow_value_matcher(value, message)
|
2013-07-24 21:46:01 +00:00
|
|
|
yield disallow if block_given?
|
2012-09-12 13:47:20 +00:00
|
|
|
|
2010-12-15 22:34:19 +00:00
|
|
|
if disallow.matches?(@subject)
|
2013-12-24 11:24:27 +00:00
|
|
|
@failure_message_when_negated = disallow.failure_message_when_negated
|
2010-12-15 22:34:19 +00:00
|
|
|
true
|
2013-03-27 18:13:46 +00:00
|
|
|
else
|
2013-12-24 11:24:27 +00:00
|
|
|
@failure_message = disallow.failure_message
|
2013-03-27 18:13:46 +00:00
|
|
|
false
|
2010-12-15 22:34:19 +00:00
|
|
|
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).
|
2013-07-02 02:16:32 +00:00
|
|
|
on(@context).
|
2012-09-12 13:47:20 +00:00
|
|
|
with_message(message)
|
|
|
|
|
|
|
|
if strict?
|
|
|
|
matcher.strict
|
|
|
|
else
|
|
|
|
matcher
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-27 18:13:46 +00:00
|
|
|
def disallow_value_matcher(value, message)
|
|
|
|
matcher = DisallowValueMatcher.
|
|
|
|
new(value).
|
|
|
|
for(@attribute).
|
2013-07-02 02:16:32 +00:00
|
|
|
on(@context).
|
2013-03-27 18:13:46 +00:00
|
|
|
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
|