Class: Poncho::Errors
- Inherits:
-
Object
- Object
- Poncho::Errors
- Defined in:
- lib/poncho/errors.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
When passed a symbol or a name of a method, returns an array of errors for the method.
-
#[]=(attribute, error) ⇒ Object
Adds to the supplied attribute the supplied error message.
- #add(attribute, message = nil, options = {}) ⇒ Object
-
#as_json(options = nil) ⇒ Object
Return the first error we get.
-
#clear ⇒ Object
Clear the messages.
-
#count ⇒ Object
Returns the number of error messages.
-
#delete(key) ⇒ Object
Delete messages for
key
. - #each ⇒ Object
-
#empty? ⇒ Boolean
(also: #blank?)
Returns true if no errors are found, false otherwise.
- #full_message(attribute, message) ⇒ Object
- #full_messages ⇒ Object
-
#get(key) ⇒ Object
Get messages for
key
. -
#include?(error) ⇒ Boolean
(also: #has_key?)
Do the error messages include an error with key
error
?. -
#initialize(base) ⇒ Errors
constructor
A new instance of Errors.
-
#keys ⇒ Object
Returns all message keys.
-
#set(key, value) ⇒ Object
Set messages for
key
tovalue
. -
#size ⇒ Object
Returns the number of error messages.
-
#to_a ⇒ Object
Returns an array of error messages, with the attribute name included.
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
-
#values ⇒ Object
Returns all message values.
Constructor Details
#initialize(base) ⇒ Errors
Returns a new instance of Errors.
5 6 7 8 |
# File 'lib/poncho/errors.rb', line 5 def initialize(base) @base = base @messages = {} end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
3 4 5 |
# File 'lib/poncho/errors.rb', line 3 def @messages end |
Instance Method Details
#[](attribute) ⇒ Object
When passed a symbol or a name of a method, returns an array of errors for the method.
p.errors[:name] # => ["can not be nil"]
p.errors['name'] # => ["can not be nil"]
41 42 43 |
# File 'lib/poncho/errors.rb', line 41 def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end |
#[]=(attribute, error) ⇒ Object
Adds to the supplied attribute the supplied error message.
p.errors[:name] = "must be set"
p.errors[:name] # => ['must be set']
49 50 51 |
# File 'lib/poncho/errors.rb', line 49 def []=(attribute, error) self[attribute] << error end |
#add(attribute, message = nil, options = {}) ⇒ Object
129 130 131 |
# File 'lib/poncho/errors.rb', line 129 def add(attribute, = nil, = {}) self[attribute] << end |
#as_json(options = nil) ⇒ Object
Return the first error we get
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/poncho/errors.rb', line 107 def as_json(=nil) return {} if .empty? attribute, types = .first type = types.first { :error => { :param => attribute, :type => type, :message => nil } } end |
#clear ⇒ Object
Clear the messages
11 12 13 |
# File 'lib/poncho/errors.rb', line 11 def clear .clear end |
#count ⇒ Object
Returns the number of error messages.
p.errors.add(:name, "can't be blank")
p.errors.count # => 1
p.errors.add(:name, "must be specified")
p.errors.count # => 2
95 96 97 |
# File 'lib/poncho/errors.rb', line 95 def count to_a.size end |
#delete(key) ⇒ Object
Delete messages for key
32 33 34 |
# File 'lib/poncho/errors.rb', line 32 def delete(key) .delete(key) end |
#each ⇒ Object
53 54 55 |
# File 'lib/poncho/errors.rb', line 53 def each [to_s] end |
#empty? ⇒ Boolean Also known as: blank?
Returns true if no errors are found, false otherwise. If the error message is a string it can be empty.
101 102 103 |
# File 'lib/poncho/errors.rb', line 101 def empty? .all? { |k, v| v && v == "" && !v.is_a?(String) } end |
#full_message(attribute, message) ⇒ Object
137 138 139 140 |
# File 'lib/poncho/errors.rb', line 137 def (attribute, ) return if attribute == :base "#{attribute} #{.join(', ')}" end |
#full_messages ⇒ Object
133 134 135 |
# File 'lib/poncho/errors.rb', line 133 def .map { |attribute, | (attribute, ) } end |
#get(key) ⇒ Object
Get messages for key
22 23 24 |
# File 'lib/poncho/errors.rb', line 22 def get(key) [key] end |
#include?(error) ⇒ Boolean Also known as: has_key?
Do the error messages include an error with key error
?
16 17 18 |
# File 'lib/poncho/errors.rb', line 16 def include?(error) (v = [error]) && v.any? end |
#keys ⇒ Object
Returns all message keys
73 74 75 |
# File 'lib/poncho/errors.rb', line 73 def keys .keys end |
#set(key, value) ⇒ Object
Set messages for key
to value
27 28 29 |
# File 'lib/poncho/errors.rb', line 27 def set(key, value) [key] = value end |
#size ⇒ Object
Returns the number of error messages.
p.errors.add(:name, "can't be blank")
p.errors.size # => 1
p.errors.add(:name, "must be specified")
p.errors.size # => 2
63 64 65 |
# File 'lib/poncho/errors.rb', line 63 def size values.flatten.size end |
#to_a ⇒ Object
Returns an array of error messages, with the attribute name included
p.errors.add(:name, "can't be blank")
p.errors.add(:name, "must be specified")
p.errors.to_a # => ["name can't be blank", "name must be specified"]
86 87 88 |
# File 'lib/poncho/errors.rb', line 86 def to_a end |
#to_hash ⇒ Object
125 126 127 |
# File 'lib/poncho/errors.rb', line 125 def to_hash .dup end |
#to_json ⇒ Object
121 122 123 |
# File 'lib/poncho/errors.rb', line 121 def to_json(*) as_json.to_json end |
#to_s ⇒ Object
77 78 79 |
# File 'lib/poncho/errors.rb', line 77 def to_s "Validation errors:\n " + .join(', ') end |
#values ⇒ Object
Returns all message values
68 69 70 |
# File 'lib/poncho/errors.rb', line 68 def values .values end |