Permalink
Browse files

Implement #validate and #validate!

  • Loading branch information...
1 parent f437069 commit 7fe78e1ea45ab072f28821824da72c20d6796642 @bhuga bhuga committed Nov 12, 2010
Showing with 26 additions and 8 deletions.
  1. +26 −8 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)
@@ -464,6 +460,28 @@ def errors
@errors ||= Spira::Errors.new
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
#

0 comments on commit 7fe78e1

Please sign in to comment.