Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement #validate and #validate!

  • Loading branch information...
commit 7fe78e1ea45ab072f28821824da72c20d6796642 1 parent f437069
@bhuga bhuga authored
Showing with 26 additions and 8 deletions.
  1. +26 −8 lib/spira/resource/instance_methods.rb
View
34 lib/spira/resource/instance_methods.rb
@@ -162,16 +162,12 @@ def save!
existed = (self.respond_to?(:before_create) || self.respond_to?(:after_create)) && !self.type.nil? && exists?
before_create if self.respond_to?(:before_create) && !self.type.nil? && !existed
before_save if self.respond_to?(:before_save)
- unless self.class.validators.empty?
- errors.clear
- self.class.validators.each do | validator | self.send(validator) end
- if errors.empty?
+ # we use the non-raising validate and check it to make a slightly different error message. worth it?...
+ case validate
+ when true
_update!
- else
+ when false
raise(ValidationError, "Could not save #{self.inspect} due to validation errors: " + errors.each.join(';'))
- end
- else
- _update!
end
after_create if self.respond_to?(:after_create) && !self.type.nil? && !existed
after_save if self.respond_to?(:after_save)
@@ -465,6 +461,28 @@ def errors
end
##
+ # Run any model validations and populate the errors object accordingly.
+ # Returns true if the model is valid, false otherwise
+ #
+ # @return [True, False]
+ def validate
+ unless self.class.validators.empty?
+ errors.clear
+ self.class.validators.each do | validator | self.send(validator) end
+ end
+ errors.empty?
+ end
+
+ ##
+ # Run validations on this model and raise a Spira::ValidationError if the validations fail.
+ #
+ # @see #validate
+ # @return true
+ def validate!
+ validate || raise(ValidationError, "Failed to validate #{self.inspect}: " + errors.each.join(';'))
+ end
+
+ ##
# Returns true if any data exists for this subject in the backing RDF store
#
# @return [Boolean]
Please sign in to comment.
Something went wrong with that request. Please try again.