Class: Exocora::ValidationOfPresence

Inherits:
Validation show all
Defined in:
lib/exocora/validation.rb

Overview

ValidationOfPresence is a special validation which ensures its value is both present and non-empty.

Constant Summary collapse

DEFAULT_MESSAGE =
'must be present'

Instance Attribute Summary

Attributes inherited from Validation

#filter, #message, #negate

Instance Method Summary collapse

Methods inherited from Validation

#because, #if, #to_s, #unless

Constructor Details

#initialize(message = DEFAULT_MESSAGE) ⇒ ValidationOfPresence

Returns a new instance of ValidationOfPresence.



81
82
83
# File 'lib/exocora/validation.rb', line 81

def initialize(message=DEFAULT_MESSAGE)
  @message = message
end

Instance Method Details

#validate(value = nil) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/exocora/validation.rb', line 85

def validate(value = nil)
  if value.nil? or (value.respond_to? :empty? and value.empty?)
    raise ValidationError.new(value, @message)
  else
    value
  end
end