class DailyAffirmation::Validator

Attributes

attribute[RW]
object[RW]
opts[RW]
value[RW]

Public Class Methods

new(object, attribute, opts = {}) click to toggle source

Initializes a new validator to validate the given object/attribute combination.

@param object [Object] the object to validate. @param attribute [Symbol] the attribute to validate. The object must

`respond_to?` a method with the name `attribute` with no arguments.

@param opts [Hash] any special options related to the affirmation. @option opts [Proc] :if evaluated before affirmation to ensure we should

run validation.

@option opts [true, false] :allow_nil determines if we skip validation of

`nil` valued attributes.

@option opts [true, false] :allow_blank determines if we skip validation

of `""` valued attributes.

@return [self]

# File lib/daily_affirmation/validator.rb, line 18
def initialize(object, attribute, opts = {})
  self.object = object
  self.attribute = attribute
  self.value = object.send(attribute)
  self.opts = opts
end

Public Instance Methods

affirm() click to toggle source

Returns an array of length 2 telling you if the object passes this affirmation along with an error message if it doesn’t.

@return [Array(Boolean, [nil, String])] Array of length 2 containing

validation results.
# File lib/daily_affirmation/validator.rb, line 30
def affirm
  @affirm ||= [valid?, valid? ? nil : error_message]
end
error_message() click to toggle source

Returns the error message related to this validation.

@note This method will always return the associated error message, even if the object passes validation.

@return [String]

# File lib/daily_affirmation/validator.rb, line 50
def error_message
  i18n_error_message(:none)
end
valid?() click to toggle source

Tells you if the object is valid based on this affirmation.

@note Subclasses of DailyAffirmation::Validator must implement this method.

@return [true, false]

# File lib/daily_affirmation/validator.rb, line 40
def valid?
  raise StandardError, "must implement #valid?"
end

Private Instance Methods

i18n_error_message(type, default: " click to toggle source
# File lib/daily_affirmation/validator.rb, line 58
def i18n_error_message(type, default: "#{attribute} failed validation")
  if defined?(I18n)
    args = opts.merge(
      {
        :default => [
          :"daily_affirmation.errors.messages.#{type}.default", default
        ],
        :attribute => attribute,
        :value => value
      }
    )
    I18n.t(:"daily_affirmation.errors.messages.#{type}.#{attribute}", args)
  else
    default
  end
end