Class: AWS::Record::PresenceValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/aws/record/validators/presence.rb

Constant Summary collapse

ACCEPTED_OPTIONS =
[:message, :allow_nil, :on, :if, :unless]

Instance Attribute Summary

Attributes inherited from Validator

#attribute_names, #options

Instance Method Summary collapse

Methods inherited from Validator

#initialize, #validate

Constructor Details

This class inherits a constructor from AWS::Record::Validator

Instance Method Details

#messageObject



39
40
41
# File 'lib/aws/record/validators/presence.rb', line 39

def message
  options[:message] || 'may not be blank'
end

#validate_attribute(record, attribute_name, value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aws/record/validators/presence.rb', line 24

def validate_attribute record, attribute_name, value

  blank = case
  when value.nil?                 then true
  when value.is_a?(String)        then value !~ /\S/
  when value == false             then false # defeat false.blank? == true
  when value.respond_to?(:empty?) then value.empty?
  when value.respond_to?(:blank?) then value.blank?
  else false
  end

  record.errors.add(attribute_name, message) if blank

end