Class: LHS::Errors
Overview
Like ActiveModel::Errors
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, error) ⇒ Object
- #add(attribute, message = :invalid, _options = {}) ⇒ Object
- #count ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #get(key) ⇒ Object
- #include?(attribute) ⇒ Boolean (also: #has_key?, #key?)
-
#initialize(response = nil) ⇒ Errors
constructor
A new instance of Errors.
- #set(key, value) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(response = nil) ⇒ Errors
Returns a new instance of Errors.
7 8 9 10 11 12 13 |
# File 'lib/lhs/errors.rb', line 7 def initialize(response = nil) @messages = (response) @message = (response) @raw = response.body if response rescue JSON::ParserError add_error(, 'body', 'parse error') end |
Instance Attribute Details
#message ⇒ Object (readonly)
Returns the value of attribute message.
5 6 7 |
# File 'lib/lhs/errors.rb', line 5 def @message end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
5 6 7 |
# File 'lib/lhs/errors.rb', line 5 def @messages end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
5 6 7 |
# File 'lib/lhs/errors.rb', line 5 def raw @raw end |
Instance Method Details
#[](attribute) ⇒ Object
36 37 38 |
# File 'lib/lhs/errors.rb', line 36 def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end |
#[]=(attribute, error) ⇒ Object
40 41 42 |
# File 'lib/lhs/errors.rb', line 40 def []=(attribute, error) self[attribute] << error end |
#add(attribute, message = :invalid, _options = {}) ⇒ Object
21 22 23 24 |
# File 'lib/lhs/errors.rb', line 21 def add(attribute, = :invalid, = {}) self[attribute] [attribute] << end |
#count ⇒ Object
58 59 60 |
# File 'lib/lhs/errors.rb', line 58 def count to_a.size end |
#each ⇒ Object
44 45 46 47 48 |
# File 'lib/lhs/errors.rb', line 44 def each .each_key do |attribute| self[attribute].each { |error| yield attribute, error } end end |
#empty? ⇒ Boolean
62 63 64 |
# File 'lib/lhs/errors.rb', line 62 def empty? all? { |_k, v| v && v.empty? && !v.is_a?(String) } end |
#get(key) ⇒ Object
26 27 28 |
# File 'lib/lhs/errors.rb', line 26 def get(key) [key] end |
#include?(attribute) ⇒ Boolean Also known as: has_key?, key?
15 16 17 |
# File 'lib/lhs/errors.rb', line 15 def include?(attribute) [attribute].present? end |
#set(key, value) ⇒ Object
30 31 32 |
# File 'lib/lhs/errors.rb', line 30 def set(key, value) [key] = value end |
#size ⇒ Object
50 51 52 |
# File 'lib/lhs/errors.rb', line 50 def size values.flatten.size end |