2014-01-23 18:07:36 +00:00
|
|
|
module Shoulda
|
2010-12-15 22:34:19 +00:00
|
|
|
module Matchers
|
2014-01-23 18:07:36 +00:00
|
|
|
module ActiveModel
|
|
|
|
# @private
|
|
|
|
class ValidationMatcher
|
2013-12-24 11:24:27 +00:00
|
|
|
attr_reader :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
|
2014-06-27 22:39:39 +00:00
|
|
|
@failure_message = nil
|
|
|
|
@failure_message_when_negated = nil
|
2012-09-12 00:53:21 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
2014-06-27 22:39:39 +00:00
|
|
|
matcher = AllowValueMatcher.new(value).for(@attribute).
|
2012-09-12 13:47:20 +00:00
|
|
|
with_message(message)
|
|
|
|
|
2014-06-27 22:39:39 +00:00
|
|
|
if defined?(@context)
|
|
|
|
matcher.on(@context)
|
|
|
|
end
|
|
|
|
|
2012-09-12 13:47:20 +00:00
|
|
|
if strict?
|
|
|
|
matcher.strict
|
|
|
|
end
|
2014-06-27 22:39:39 +00:00
|
|
|
|
|
|
|
matcher
|
2012-09-12 13:47:20 +00:00
|
|
|
end
|
|
|
|
|
2013-03-27 18:13:46 +00:00
|
|
|
def disallow_value_matcher(value, message)
|
2014-06-27 22:39:39 +00:00
|
|
|
matcher = DisallowValueMatcher.new(value).for(@attribute).
|
2013-03-27 18:13:46 +00:00
|
|
|
with_message(message)
|
|
|
|
|
2014-06-27 22:39:39 +00:00
|
|
|
if defined?(@context)
|
|
|
|
matcher.on(@context)
|
|
|
|
end
|
|
|
|
|
2013-03-27 18:13:46 +00:00
|
|
|
if strict?
|
|
|
|
matcher.strict
|
|
|
|
end
|
2014-06-27 22:39:39 +00:00
|
|
|
|
|
|
|
matcher
|
2013-03-27 18:13:46 +00:00
|
|
|
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
|