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 14 15 |
# File 'lib/lhs/errors.rb', line 7 def initialize(response = nil) @raw = response.body if response @messages = (response) @message = (response) rescue JSON::ParserError @messages = || {} @message = 'parse error' add_error(@messages, '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
38 39 40 |
# File 'lib/lhs/errors.rb', line 38 def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end |
#[]=(attribute, error) ⇒ Object
42 43 44 |
# File 'lib/lhs/errors.rb', line 42 def []=(attribute, error) self[attribute] << error end |
#add(attribute, message = :invalid, _options = {}) ⇒ Object
23 24 25 26 |
# File 'lib/lhs/errors.rb', line 23 def add(attribute, = :invalid, = {}) self[attribute] [attribute] << end |
#count ⇒ Object
60 61 62 |
# File 'lib/lhs/errors.rb', line 60 def count to_a.size end |
#each ⇒ Object
46 47 48 49 50 |
# File 'lib/lhs/errors.rb', line 46 def each .each_key do |attribute| self[attribute].each { |error| yield attribute, error } end end |
#empty? ⇒ Boolean
64 65 66 |
# File 'lib/lhs/errors.rb', line 64 def empty? all? { |_k, v| v && v.empty? && !v.is_a?(String) } end |
#get(key) ⇒ Object
28 29 30 |
# File 'lib/lhs/errors.rb', line 28 def get(key) [key] end |
#include?(attribute) ⇒ Boolean Also known as: has_key?, key?
17 18 19 |
# File 'lib/lhs/errors.rb', line 17 def include?(attribute) [attribute].present? end |
#set(key, value) ⇒ Object
32 33 34 |
# File 'lib/lhs/errors.rb', line 32 def set(key, value) [key] = value end |
#size ⇒ Object
52 53 54 |
# File 'lib/lhs/errors.rb', line 52 def size values.flatten.size end |