Class: Ward::Errors
Overview
Holds errors associated with a valid? call.
Class Method Summary collapse
-
.error_for(matcher, negative, key = nil) ⇒ String
Returns the unformatted error message for a matcher.
-
.format_exclusive_list(list) ⇒ String
Receives an array and formats it nicely, assuming that only one value is expected.
-
.format_inclusive_list(list) ⇒ String
Receives an array and formats it nicely, assuming that all values are expected.
-
.message(*keys) ⇒ String?
Returns a localisation message.
Instance Method Summary collapse
-
#add(attribute, message) ⇒ String
Adds an error message to the instance.
-
#each {|attribute, messages| ... } ⇒ Object
Iterates through each attribute and the errors.
-
#empty? ⇒ Boolean
Returns if there are no errors contained.
-
#initialize ⇒ Errors
constructor
Creates a new Errors instance.
-
#on(attribute) ⇒ Array
Returns an array of the errors present on an an attribute.
Constructor Details
#initialize ⇒ Errors
Creates a new Errors instance.
153 154 155 |
# File 'lib/ward/errors.rb', line 153 def initialize @errors = {} end |
Class Method Details
.error_for(matcher, negative, key = nil) ⇒ String
Returns the unformatted error message for a matcher.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ward/errors.rb', line 49 def error_for(matcher, negative, key = nil) return key if key.is_a?(String) language_key = if key.nil? "#{matcher.class.error_id}." else "#{matcher.class.error_id}.#{key}." end language_key << (negative ? 'negative' : 'positive') (language_key) || '%{context} is invalid' end |
.format_exclusive_list(list) ⇒ String
Receives an array and formats it nicely, assuming that only one value is expected.
80 81 82 |
# File 'lib/ward/errors.rb', line 80 def format_exclusive_list(list) format_list(list, ('generic.exclusive_conjunction')) end |
.format_inclusive_list(list) ⇒ String
Receives an array and formats it nicely, assuming that all values are expected.
101 102 103 |
# File 'lib/ward/errors.rb', line 101 def format_inclusive_list(list) format_list(list, ('generic.inclusive_conjunction')) end |
.message(*keys) ⇒ String?
Returns a localisation message.
31 32 33 |
# File 'lib/ward/errors.rb', line 31 def (*keys) [ keys.detect { |key| .has_key?(key) } ] end |
Instance Method Details
#add(attribute, message) ⇒ String
Support symbols for i18n.
Adds an error message to the instance.
170 171 172 173 174 175 176 177 178 |
# File 'lib/ward/errors.rb', line 170 def add(attribute, ) if attribute.kind_of?(Context) or attribute.kind_of?(ContextChain) attribute = attribute.attribute end @errors[attribute] ||= [] @errors[attribute] << end |
#each {|attribute, messages| ... } ⇒ Object
Iterates through each attribute and the errors.
200 201 202 |
# File 'lib/ward/errors.rb', line 200 def each(&block) @errors.each(&block) end |
#empty? ⇒ Boolean
Returns if there are no errors contained.
208 209 210 |
# File 'lib/ward/errors.rb', line 208 def empty? @errors.empty? end |
#on(attribute) ⇒ Array
Returns an array of the errors present on an an attribute.
188 189 190 |
# File 'lib/ward/errors.rb', line 188 def on(attribute) @errors[attribute] end |