Class: GS1::Barcode::Errors
- Inherits:
-
Object
- Object
- GS1::Barcode::Errors
- Defined in:
- lib/gs1/barcode/errors.rb
Overview
Error collection handler wrapper class for a barcode.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #[](attribute_name) ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
- #full_messages ⇒ Object
-
#initialize ⇒ Errors
constructor
A new instance of Errors.
- #messages ⇒ Object
Constructor Details
#initialize ⇒ Errors
Returns a new instance of Errors.
8 9 10 |
# File 'lib/gs1/barcode/errors.rb', line 8 def initialize @errors = {} end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/gs1/barcode/errors.rb', line 6 def errors @errors end |
Instance Method Details
#[](attribute_name) ⇒ Object
12 13 14 |
# File 'lib/gs1/barcode/errors.rb', line 12 def [](attribute_name) errors[attribute_name] ||= [] end |
#clear ⇒ Object
16 17 18 19 20 |
# File 'lib/gs1/barcode/errors.rb', line 16 def clear errors.each_value do |errs| errs.select!(&:persistent?) end end |
#empty? ⇒ Boolean
22 23 24 |
# File 'lib/gs1/barcode/errors.rb', line 22 def empty? errors.values.flatten.empty? end |
#full_messages ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/gs1/barcode/errors.rb', line 32 def errors.flat_map do |(attribute_name, errors)| errors.uniq.map do |error| "#{error.} #{attribute_name.to_s.tr('_', ' ')}" end end end |
#messages ⇒ Object
26 27 28 29 30 |
# File 'lib/gs1/barcode/errors.rb', line 26 def errors.each_with_object({}) do |(attribute_name, errors), hash| hash[attribute_name] = errors.uniq.map(&:human_message) end end |