Class: Appfuel::Errors
- Inherits:
-
Object
- Object
- Appfuel::Errors
- Includes:
- Enumerable
- Defined in:
- lib/appfuel/errors.rb
Overview
Feature handler, action handler, command handler all use this class. Presenters and validators will have there errors tranformed into this. Errors are a basic hash structure where each key has an array of strings that represent error messages.
Example
messages: {
name: [
'must be present',
'can not be blank',
'can not be Bob'
]
}
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#add(key, msg) ⇒ Object
Add an error message to a given key.
- #clear ⇒ Object
- #delete(key) ⇒ Object
-
#each ⇒ Object
Defined to use Enumerable so that we can treat errors as an iterator.
- #empty? ⇒ Boolean
-
#format(msg_separator = "\n", list_separator = "\n") ⇒ Object
Formats the list of messages for each key.
-
#initialize(messages = {}) ⇒ Errors
constructor
A new instance of Errors.
- #keys ⇒ Object
- #size ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(messages = {}) ⇒ Errors
Returns a new instance of Errors.
19 20 21 22 |
# File 'lib/appfuel/errors.rb', line 19 def initialize( = {}) @messages = || {} @messages.stringify_keys! unless @messages.empty? end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
17 18 19 |
# File 'lib/appfuel/errors.rb', line 17 def @messages end |
Instance Method Details
#[](key) ⇒ Object
72 73 74 |
# File 'lib/appfuel/errors.rb', line 72 def [](key) [key.to_s] end |
#add(key, msg) ⇒ Object
Add an error message to a given key
36 37 38 39 40 41 |
# File 'lib/appfuel/errors.rb', line 36 def add(key, msg) key = key.to_s msg = msg.to_s [key] = [] unless .key?(key) [key] << msg unless [key].include?(msg) end |
#clear ⇒ Object
88 89 90 |
# File 'lib/appfuel/errors.rb', line 88 def clear .clear end |
#delete(key) ⇒ Object
68 69 70 |
# File 'lib/appfuel/errors.rb', line 68 def delete(key) .delete(key.to_s) end |
#each ⇒ Object
Defined to use Enumerable so that we can treat errors as an iterator
26 27 28 29 30 |
# File 'lib/appfuel/errors.rb', line 26 def each .each do|key, msgs| yield key, msgs end end |
#empty? ⇒ Boolean
92 93 94 |
# File 'lib/appfuel/errors.rb', line 92 def empty? .empty? end |
#format(msg_separator = "\n", list_separator = "\n") ⇒ Object
Formats the list of messages for each key
Example
messages: {
name: [
' must be present ',
' can not be blank ',
' can not be Bob '
]
}
note: spaces are used only for readability name: must be present n can not be blank n can not be Bob n n
60 61 62 63 64 65 66 |
# File 'lib/appfuel/errors.rb', line 60 def format(msg_separator = "\n", list_separator = "\n") msg = '' each do |key, list| msg << "#{key}: #{list.join(msg_separator)}#{list_separator}" end msg end |
#keys ⇒ Object
84 85 86 |
# File 'lib/appfuel/errors.rb', line 84 def keys .keys end |
#size ⇒ Object
76 77 78 |
# File 'lib/appfuel/errors.rb', line 76 def size .length end |
#to_h ⇒ Object
96 97 98 |
# File 'lib/appfuel/errors.rb', line 96 def to_h {errors: } end |
#to_s ⇒ Object
100 101 102 |
# File 'lib/appfuel/errors.rb', line 100 def to_s format end |
#values ⇒ Object
80 81 82 |
# File 'lib/appfuel/errors.rb', line 80 def values .values end |