Module: SimplyStored::SimpleDB::Validations
- Included in:
- SimplyStored::Simple
- Defined in:
- lib/simply_stored/simpledb/validations.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #add_error(attr, desc) ⇒ Object
- #clear_errors ⇒ Object
- #ensure_formats ⇒ Object
- #ensure_inclusions ⇒ Object
- #ensure_no_foreign_attributes ⇒ Object
- #ensure_required_attributes_present ⇒ Object
- #validate ⇒ Object
Class Method Details
.included(base) ⇒ Object
8 9 10 |
# File 'lib/simply_stored/simpledb/validations.rb', line 8 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#add_error(attr, desc) ⇒ Object
59 60 61 |
# File 'lib/simply_stored/simpledb/validations.rb', line 59 def add_error(attr, desc) errors << [attr, desc] end |
#clear_errors ⇒ Object
63 64 65 |
# File 'lib/simply_stored/simpledb/validations.rb', line 63 def clear_errors @errors = [] end |
#ensure_formats ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/simply_stored/simpledb/validations.rb', line 37 def ensure_formats list_of_format_checks = self.class.instance_variable_get("@_required_formats") || {} list_of_format_checks.each do |attr, | valid_regex, = add_error(attr, 'is of invalid format') unless valid_regex.match(self.send(attr)) || (self.send(attr).blank? && [:allow_blank]) end end |
#ensure_inclusions ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/simply_stored/simpledb/validations.rb', line 45 def ensure_inclusions list_of_inclusion_checks = self.class.instance_variable_get("@_required_inclusions") || {} list_of_inclusion_checks.each do |attr, | valid_set, = if self.send(attr) && self.send(attr).is_a?(Array) self.send(attr).all? do |item| add_error(attr, 'is not included in the valid set') unless (valid_set.include?(item) rescue false) || (self.send(attr).blank? && [:allow_blank]) end else add_error(attr, 'is not included in the valid set') unless (valid_set.include?(self.send(attr)) rescue false) || (self.send(attr).blank? && [:allow_blank]) end end end |
#ensure_no_foreign_attributes ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/simply_stored/simpledb/validations.rb', line 67 def ensure_no_foreign_attributes not_allowed_attributes = self.attributes.keys.sort - (self.class.instance_variable_get("@_defined_attributes") || []) not_allowed_attributes = not_allowed_attributes.delete_if{|attr| attr.to_s == 'id'} # the ID is an implicit attribute not_allowed_attributes.each do |attr| add_error(attr, 'is unknown and should not be set') end end |
#ensure_required_attributes_present ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/simply_stored/simpledb/validations.rb', line 75 def ensure_required_attributes_present list_of_required_attributes = self.class.instance_variable_get("@_required_attributes") || [] list_of_required_attributes.all? do |attr| if self.send(attr).blank? add_error(attr, 'is missing') false else true end end end |
#validate ⇒ Object
4 5 6 |
# File 'lib/simply_stored/simpledb/validations.rb', line 4 def validate # abstract - implement in subclass and add checks that add to errors with add_error end |