Class: AWS::Record::AcceptanceValidator

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

Constant Summary collapse

ACCEPTED_OPTIONS =
[:accept, :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



44
45
46
# File 'lib/aws/record/validators/acceptance.rb', line 44

def message
  options[:message] || 'must be accepted'
end

#setup(record_class) ⇒ Object



24
25
26
27
# File 'lib/aws/record/validators/acceptance.rb', line 24

def setup record_class
  set_default(:allow_nil, true)
  add_accessors(record_class, *attribute_names)
end

#validate_attribute(record, attribute_name, value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aws/record/validators/acceptance.rb', line 29

def validate_attribute record, attribute_name, value

  accepted = case value
  when '1'  then true
  when true then true
  else 
    options.has_key?(:accept) ?
      value == options[:accept] :
      false
  end

  record.errors.add(attribute_name, message) unless accepted

end