Class: LHS::Errors::Base
- Inherits:
-
Object
- Object
- LHS::Errors::Base
- Includes:
- Enumerable
- Defined in:
- lib/lhs/errors/base.rb
Direct Known Subclasses
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.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, error) ⇒ Object
- #add(attribute, message = :invalid, options = {}) ⇒ Object
- #clear ⇒ Object
- #count ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #get(key) ⇒ Object
- #include?(attribute) ⇒ Boolean (also: #has_key?, #key?)
-
#initialize(response = nil, record = nil) ⇒ Base
constructor
A new instance of Base.
- #set(key, value) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(response = nil, record = nil) ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/lhs/errors/base.rb', line 8 def initialize(response = nil, record = nil) @raw = response.body if response @record = record @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.
6 7 8 |
# File 'lib/lhs/errors/base.rb', line 6 def @message end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
6 7 8 |
# File 'lib/lhs/errors/base.rb', line 6 def @messages end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
6 7 8 |
# File 'lib/lhs/errors/base.rb', line 6 def raw @raw end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
6 7 8 |
# File 'lib/lhs/errors/base.rb', line 6 def record @record end |
Instance Method Details
#[](attribute) ⇒ Object
40 41 42 |
# File 'lib/lhs/errors/base.rb', line 40 def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end |
#[]=(attribute, error) ⇒ Object
44 45 46 |
# File 'lib/lhs/errors/base.rb', line 44 def []=(attribute, error) self[attribute] << (attribute, error) end |
#add(attribute, message = :invalid, options = {}) ⇒ Object
25 26 27 28 |
# File 'lib/lhs/errors/base.rb', line 25 def add(attribute, = :invalid, = {}) self[attribute] [attribute] << (attribute, , ) end |
#clear ⇒ Object
58 59 60 61 |
# File 'lib/lhs/errors/base.rb', line 58 def clear @raw = nil @messages.clear end |
#count ⇒ Object
67 68 69 |
# File 'lib/lhs/errors/base.rb', line 67 def count to_a.size end |
#each ⇒ Object
48 49 50 51 52 |
# File 'lib/lhs/errors/base.rb', line 48 def each .each_key do |attribute| self[attribute].each { |error| yield attribute, error } end end |
#empty? ⇒ Boolean
71 72 73 |
# File 'lib/lhs/errors/base.rb', line 71 def empty? all? { |_k, v| v && v.empty? && !v.is_a?(String) } end |
#get(key) ⇒ Object
30 31 32 |
# File 'lib/lhs/errors/base.rb', line 30 def get(key) [key] end |
#include?(attribute) ⇒ Boolean Also known as: has_key?, key?
19 20 21 |
# File 'lib/lhs/errors/base.rb', line 19 def include?(attribute) [attribute].present? end |
#set(key, value) ⇒ Object
34 35 36 |
# File 'lib/lhs/errors/base.rb', line 34 def set(key, value) [key] = (key, value) end |
#size ⇒ Object
54 55 56 |
# File 'lib/lhs/errors/base.rb', line 54 def size values.flatten.size end |