@author Guy van den Berg @since 0.9
Add a validation error. Use the field_name :general if the errors does not apply to a specific field of the Resource.
@param <Symbol> field_name the name of the field that caused the error @param <String> message the message to add
# File lib/couchrest/validation/validation_errors.rb, line 77 def add(field_name, message) (errors[field_name.to_sym] ||= []) << message end
Clear existing validation errors.
# File lib/couchrest/validation/validation_errors.rb, line 68 def clear! errors.clear end
Return size of errors hash
Allows us to play nicely with Rails’ helpers
# File lib/couchrest/validation/validation_errors.rb, line 110 def count errors.size end
# File lib/couchrest/validation/validation_errors.rb, line 96 def each errors.map.each do |k, v| next if v.blank? yield(v) end end
# File lib/couchrest/validation/validation_errors.rb, line 103 def empty? entries.empty? end
Collect all errors into a single list.
# File lib/couchrest/validation/validation_errors.rb, line 82 def full_messages errors.inject([]) do |list, pair| list += pair.last end end
# File lib/couchrest/validation/validation_errors.rb, line 114 def method_missing(meth, *args, &block) errors.send(meth, *args, &block) end
Return validation errors for a particular field_name.
@param <Symbol> field_name the name of the field you want an error for
# File lib/couchrest/validation/validation_errors.rb, line 91 def on(field_name) errors_for_field = errors[field_name.to_sym] errors_for_field.blank? ? nil : errors_for_field end
# File lib/couchrest/validation/validation_errors.rb, line 62 def self.default_error_message(key, field, *values) field = field.to_s.humanize @@default_error_messages[key] % [field, *values].flatten end