class Shoulda::Matchers::ActiveModel::ValidationMatcher

@private

Attributes

attribute[R]
context[R]
last_submatcher_run[R]
subject[R]

Public Class Methods

new(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 8
def initialize(attribute)
  super
  @attribute = attribute
  @expects_strict = false
  @subject = nil
  @last_submatcher_run = nil
  @expected_message = nil
  @expects_custom_validation_message = false
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 18
def description
  ValidationMatcher::BuildDescription.call(self, simple_description)
end
expects_custom_validation_message?() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 45
def expects_custom_validation_message?
  @expects_custom_validation_message
end
expects_strict?() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 32
def expects_strict?
  @expects_strict
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 54
def failure_message
  overall_failure_message.dup.tap do |message|
    if failure_reason.present?
      message << "\n"
      message << Shoulda::Matchers.word_wrap(
        failure_reason,
        indent: 2
      )
    end
  end
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 66
def failure_message_when_negated
  overall_failure_message_when_negated.dup.tap do |message|
    if failure_reason_when_negated.present?
      message << "\n"
      message << Shoulda::Matchers.word_wrap(
        failure_reason_when_negated,
        indent: 2
      )
    end
  end
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 49
def matches?(subject)
  @subject = subject
  false
end
on(context) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 22
def on(context)
  @context = context
  self
end
strict() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 27
def strict
  @expects_strict = true
  self
end
with_message(expected_message) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 36
def with_message(expected_message)
  if expected_message
    @expects_custom_validation_message = true
    @expected_message = expected_message
  end

  self
end

Protected Instance Methods

allow_value_matcher(value, message = nil, &block) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 96
def allow_value_matcher(value, message = nil, &block)
  build_allow_or_disallow_value_matcher(
    matcher_class: AllowValueMatcher,
    value: value,
    message: message,
    &block
  )
end
allows_value_of(value, message = nil, &block) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 86
def allows_value_of(value, message = nil, &block)
  matcher = allow_value_matcher(value, message, &block)
  run_allow_or_disallow_matcher(matcher)
end
disallow_value_matcher(value, message = nil, &block) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 105
def disallow_value_matcher(value, message = nil, &block)
  build_allow_or_disallow_value_matcher(
    matcher_class: DisallowValueMatcher,
    value: value,
    message: message,
    &block
  )
end
disallows_value_of(value, message = nil, &block) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 91
def disallows_value_of(value, message = nil, &block)
  matcher = disallow_value_matcher(value, message, &block)
  run_allow_or_disallow_matcher(matcher)
end
model() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 82
def model
  subject.class
end

Private Instance Methods

build_allow_or_disallow_value_matcher(args) { |matcher| ... } click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 136
def build_allow_or_disallow_value_matcher(args)
  matcher_class = args.fetch(:matcher_class)
  value = args.fetch(:value)
  message = args[:message]

  matcher = matcher_class.new(value).
    for(attribute).
    with_message(message).
    on(context).
    strict(expects_strict?).
    ignoring_interference_by_writer(ignore_interference_by_writer)

  yield matcher if block_given?

  matcher
end
failure_reason() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 128
def failure_reason
  last_submatcher_run.try(:failure_message)
end
failure_reason_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 132
def failure_reason_when_negated
  last_submatcher_run.try(:failure_message_when_negated)
end
overall_failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 116
def overall_failure_message
  Shoulda::Matchers.word_wrap(
    "#{model.name} did not properly #{description}."
  )
end
overall_failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 122
def overall_failure_message_when_negated
  Shoulda::Matchers.word_wrap(
    "Expected #{model.name} not to #{description}, but it did."
  )
end
run_allow_or_disallow_matcher(matcher) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 153
def run_allow_or_disallow_matcher(matcher)
  @last_submatcher_run = matcher
  matcher.matches?(subject)
end