Class: CouchRest::Validation::ValidationErrors
- Includes:
- Enumerable
- Defined in:
- lib/couchrest/validation/validation_errors.rb
Overview
Constant Summary collapse
- @@default_error_messages =
{ :absent => '%s must be absent', :inclusion => '%s must be one of [%s]', :invalid => '%s has an invalid format', :confirmation => '%s does not match the confirmation', :accepted => "%s is not accepted", :nil => '%s must not be nil', :blank => '%s must not be blank', :length_between => '%s must be between %s and %s characters long', :too_long => '%s must be less than %s characters long', :too_short => '%s must be more than %s characters long', :wrong_length => '%s must be %s characters long', :taken => '%s is already taken', :not_a_number => '%s must be a number', :not_an_integer => '%s must be an integer', :greater_than => '%s must be greater than %s', :greater_than_or_equal_to => "%s must be greater than or equal to %s", :equal_to => "%s must be equal to %s", :less_than => '%s must be less than %s', :less_than_or_equal_to => "%s must be less than or equal to %s", :value_between => '%s must be between %s and %s', :primitive => '%s must be of type %s' }
Class Method Summary collapse
Instance Method Summary collapse
-
#add(field_name, message) ⇒ Object
Add a validation error.
-
#clear! ⇒ Object
Clear existing validation errors.
-
#count ⇒ Object
Return size of errors hash.
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#full_messages ⇒ Object
Collect all errors into a single list.
- #method_missing(meth, *args, &block) ⇒ Object
-
#on(field_name) ⇒ Object
Return validation errors for a particular field_name.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
114 115 116 |
# File 'lib/couchrest/validation/validation_errors.rb', line 114 def method_missing(meth, *args, &block) errors.send(meth, *args, &block) end |
Class Method Details
.default_error_message(key, field, *values) ⇒ Object
62 63 64 65 |
# File 'lib/couchrest/validation/validation_errors.rb', line 62 def self.(key, field, *values) field = field.to_s.humanize @@default_error_messages[key] % [field, *values].flatten end |
Instance Method Details
#add(field_name, message) ⇒ Object
Add a validation error. Use the field_name :general if the errors does not apply to a specific field of the Resource.
77 78 79 |
# File 'lib/couchrest/validation/validation_errors.rb', line 77 def add(field_name, ) (errors[field_name.to_sym] ||= []) << end |
#clear! ⇒ Object
Clear existing validation errors.
68 69 70 |
# File 'lib/couchrest/validation/validation_errors.rb', line 68 def clear! errors.clear end |
#count ⇒ Object
Return size of errors hash
Allows us to play nicely with Rails’ helpers
110 111 112 |
# File 'lib/couchrest/validation/validation_errors.rb', line 110 def count errors.size end |
#each ⇒ Object
96 97 98 99 100 101 |
# 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 |
#empty? ⇒ Boolean
103 104 105 |
# File 'lib/couchrest/validation/validation_errors.rb', line 103 def empty? entries.empty? end |
#full_messages ⇒ Object
Collect all errors into a single list.
82 83 84 85 86 |
# File 'lib/couchrest/validation/validation_errors.rb', line 82 def errors.inject([]) do |list, pair| list += pair.last end end |
#on(field_name) ⇒ Object
Return validation errors for a particular field_name.
91 92 93 94 |
# 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 |