class Shoulda::Matchers::ActiveModel::ValidationMatcher

@private

Attributes

attribute[R]
context[R]
last_submatcher_run[R]
options[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

allow_blank() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 27
def allow_blank
  options[:allow_blank] = true
  self
end
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
does_not_match?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 59
def does_not_match?(subject)
  @subject = subject
  true
end
expects_custom_validation_message?() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 50
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 37
def expects_strict?
  @expects_strict
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 64
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 76
def failure_message_when_negated
  overall_failure_message_when_negated.dup.tap do |message|
    if failure_reason.present?
      message << "\n"
      message << Shoulda::Matchers.word_wrap(
        failure_reason,
        indent: 2,
      )
    end
  end
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 54
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 32
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 41
def with_message(expected_message)
  if expected_message
    @expects_custom_validation_message = true
    @expected_message = expected_message
  end

  self
end

Protected Instance Methods

allow_blank_does_not_match?() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 129
def allow_blank_does_not_match?
  expects_to_allow_blank? &&
    blank_values.all? { |value| disallows_value_of(value) }
end
allow_blank_matches?() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 124
def allow_blank_matches?
  !expects_to_allow_blank? ||
    blank_values.all? { |value| allows_value_of(value) }
end
allow_value_matcher(value, message = nil, &block) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 106
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 96
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 115
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 101
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 92
def model
  subject.class
end

Private Instance Methods

array_column?() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 190
def array_column?
  @subject.class.respond_to?(:columns_hash) &&
    @subject.class.columns_hash[@attribute.to_s].respond_to?(:array) &&
    @subject.class.columns_hash[@attribute.to_s].array
end
blank_values() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 186
def blank_values
  ['', ' ', "\n", "\r", "\t", "\f"]
end
build_allow_or_disallow_value_matcher(args) { |matcher| ... } click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 160
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
expects_to_allow_blank?() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 182
def expects_to_allow_blank?
  options[:allow_blank]
end
failure_reason() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 152
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 156
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 138
def overall_failure_message
  Shoulda::Matchers.word_wrap(
    "Expected #{model.name} to #{description}, but this could not be "\
    'proved.',
  )
end
overall_failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 145
def overall_failure_message_when_negated
  Shoulda::Matchers.word_wrap(
    "Expected #{model.name} not to #{description}, but this could "\
    'not be proved.',
  )
end
run_allow_or_disallow_matcher(matcher) click to toggle source
# File lib/shoulda/matchers/active_model/validation_matcher.rb, line 177
def run_allow_or_disallow_matcher(matcher)
  @last_submatcher_run = matcher
  matcher.matches?(subject)
end