Class: DataMapper::Validations::ValidationErrors
- Includes:
- Enumerable
- Defined in:
- lib/dm-validations/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 at most %s characters long', :too_short => '%s must be at least %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', :not_equal_to => '%s must not 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' }
Instance Attribute Summary collapse
- #resource ⇒ Object readonly
Class Method Summary collapse
- .default_error_message(key, field, *values) ⇒ Object
-
.default_error_messages=(default_error_messages) ⇒ Object
Holds a hash with all the default error messages that can be replaced by your own copy or localizations.
Instance Method Summary collapse
- #[](property_name) ⇒ Object
-
#add(field_name, message) ⇒ Object
Add a validation error.
-
#clear! ⇒ Object
Clear existing validation errors.
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#full_messages ⇒ Object
Collect all errors into a single list.
-
#initialize(resource) ⇒ ValidationErrors
constructor
A new instance of ValidationErrors.
- #method_missing(meth, *args, &block) ⇒ Object
-
#on(field_name) ⇒ Array<DataMapper::Validations::Error>
Return validation errors for a particular field_name.
- #respond_to?(method) ⇒ Boolean
Constructor Details
#initialize(resource) ⇒ ValidationErrors
Returns a new instance of ValidationErrors.
47 48 49 50 |
# File 'lib/dm-validations/validation_errors.rb', line 47 def initialize(resource) @resource = resource @errors = DataMapper::Validations::OrderedHash.new { |h,k| h[k] = [] } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
115 116 117 |
# File 'lib/dm-validations/validation_errors.rb', line 115 def method_missing(meth, *args, &block) errors.send(meth, *args, &block) end |
Instance Attribute Details
#resource ⇒ Object (readonly)
45 46 47 |
# File 'lib/dm-validations/validation_errors.rb', line 45 def resource @resource end |
Class Method Details
.default_error_message(key, field, *values) ⇒ Object
40 41 42 43 |
# File 'lib/dm-validations/validation_errors.rb', line 40 def self.(key, field, *values) field = DataMapper::Inflector.humanize(field) @@default_error_messages[key] % [field, *values].flatten end |
.default_error_messages=(default_error_messages) ⇒ Object
Holds a hash with all the default error messages that can be replaced by your own copy or localizations.
36 37 38 |
# File 'lib/dm-validations/validation_errors.rb', line 36 def self.() @@default_error_messages = end |
Instance Method Details
#[](property_name) ⇒ Object
123 124 125 126 127 |
# File 'lib/dm-validations/validation_errors.rb', line 123 def [](property_name) if (property_errors = errors[property_name.to_sym]) property_errors end end |
#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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dm-validations/validation_errors.rb', line 65 def add(field_name, ) # see 6abe8fff in extlib, but don't enforce # it unless Edge version is installed if .respond_to?(:try_call) # DM resource = if (resource.respond_to?(:model) && resource.model.respond_to?(:properties)) .try_call( resource, resource.model.properties[field_name] ) else # pure Ruby object .try_call(resource) end end (errors[field_name] ||= []) << end |
#clear! ⇒ Object
Clear existing validation errors.
53 54 55 |
# File 'lib/dm-validations/validation_errors.rb', line 53 def clear! errors.clear end |
#each ⇒ Object
105 106 107 108 109 |
# File 'lib/dm-validations/validation_errors.rb', line 105 def each errors.each_value do |v| yield(v) unless DataMapper::Ext.blank?(v) end end |
#empty? ⇒ Boolean
111 112 113 |
# File 'lib/dm-validations/validation_errors.rb', line 111 def empty? @errors.all? { |property_name, errors| errors.empty? } end |
#full_messages ⇒ Object
Collect all errors into a single list.
86 87 88 89 90 |
# File 'lib/dm-validations/validation_errors.rb', line 86 def errors.inject([]) do |list, pair| list += pair.last end end |
#on(field_name) ⇒ Array<DataMapper::Validations::Error>
Return validation errors for a particular field_name.
100 101 102 103 |
# File 'lib/dm-validations/validation_errors.rb', line 100 def on(field_name) errors_for_field = errors[field_name] DataMapper::Ext.blank?(errors_for_field) ? nil : errors_for_field.uniq end |
#respond_to?(method) ⇒ Boolean
119 120 121 |
# File 'lib/dm-validations/validation_errors.rb', line 119 def respond_to?(method) super || errors.respond_to?(method) end |