Class: NotNaughty::PresenceValidation

Inherits:
Validation
  • Object
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::BASEDIR, 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, new

Constructor Details

#initialize(opts, attributes) ⇒ PresenceValidation

:nodoc:



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

def initialize(opts, attributes) #:nodoc:
  __message = opts[:message] || '#{"%s".humanize} is not present.'
  
  super opts, attributes do |o, a, v|
    o.errors.add a, __message if v.blank?
  end
end