Class: NotNaughty::PresenceValidation

Inherits:
Validation show all
Defined in:
lib/not_naughty/validations/presence_validation.rb

Overview

Validates presence of obj’s attribute via the :blank? method.

Unless the validation succeeds an error hash (:attribute => :message) is added to the obj’s instance of Errors.

Options:

:message

see NotNaughty::Errors for details

:if

see NotNaughty::Validation::Condition for details

:unless

see NotNaughty::Validation::Condition for details

Example:

obj = '' # blank? => true
def obj.errors() @errors ||= NotNauthy::Errors.new end

PresenceValidation.new({}, :to_s).call obj, :to_s, ''
obj.errors.on(:to_s) # => ["To s is not present."]

Constant Summary

Constants inherited from Validation

Validation::PATTERN

Instance Attribute Summary

Attributes inherited from Validation

#attributes

Instance Method Summary collapse

Methods inherited from Validation

#call_with_conditions, #call_without_conditions, inherited, load, load_paths, new

Constructor Details

#initialize(valid, attributes) ⇒ PresenceValidation

:nodoc:



22
23
24
25
26
27
28
29
# File 'lib/not_naughty/validations/presence_validation.rb', line 22

def initialize(valid, attributes) #:nodoc:
  valid[:message] ||= '%s is not present.'

  super valid, attributes do |obj, attr, value|
    value.blank? and
    obj.errors.add attr, valid[:message]
  end
end