class Shoulda::Matchers::ActiveModel::Validator

@private

Attributes

attribute[RW]
captured_range_error[R]
context[RW]
record[RW]
strict[R]

Public Class Methods

new() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 10
def initialize
  reset
end

Public Instance Methods

allow_description(allowed_values) click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 31
def allow_description(allowed_values)
  "allow #{attribute} to be set to #{allowed_values}"
end
capture_range_error(exception) click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 26
def capture_range_error(exception)
  @captured_range_error = exception
  extend ValidatorWithCapturedRangeError
end
captured_range_error?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 67
def captured_range_error?
  !!captured_range_error
end
expected_message_from(attribute_message) click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 35
def expected_message_from(attribute_message)
  attribute_message
end
expected_messages_description(expected_message) click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 59
def expected_messages_description(expected_message)
  if expected_message
    "errors to include #{expected_message.inspect}"
  else
    'errors'
  end
end
formatted_messages() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 43
def formatted_messages
  messages
end
has_messages?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 47
def has_messages?
  messages.any?
end
messages() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 39
def messages
  @messages ||= collect_messages
end
messages_description() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 51
def messages_description
  if has_messages?
    " errors:\n#{pretty_error_messages(record)}"
  else
    ' no errors'
  end
end
reset() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 14
def reset
  @messages = nil
end
strict=(strict) click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 18
def strict=(strict)
  @strict = strict

  if strict
    extend StrictValidator
  end
end

Protected Instance Methods

collect_messages() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 76
def collect_messages
  validation_errors
end

Private Instance Methods

collect_errors_or_exceptions() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 86
def collect_errors_or_exceptions
  collect_messages
rescue RangeError => exception
  capture_range_error(exception)
  []
end
human_attribute_name() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 107
def human_attribute_name
  record.class.human_attribute_name(attribute)
end
strict?() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 82
def strict?
  !!@strict
end
validation_errors() click to toggle source
# File lib/shoulda/matchers/active_model/validator.rb, line 93
def validation_errors
  if context
    record.valid?(context)
  else
    record.valid?
  end

  if record.errors.respond_to?(:[])
    record.errors[attribute]
  else
    record.errors.on(attribute)
  end
end