Class: GS1::Barcode::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/gs1/barcode/errors.rb

Overview

Error collection handler wrapper class for a barcode.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrors

Returns a new instance of Errors.



8
9
10
# File 'lib/gs1/barcode/errors.rb', line 8

def initialize
  @errors = {}
end

Instance Attribute Details

#errorsObject (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

#clearObject



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

Returns:

  • (Boolean)


22
23
24
# File 'lib/gs1/barcode/errors.rb', line 22

def empty?
  errors.values.flatten.empty?
end

#full_messagesObject



32
33
34
35
36
37
38
# File 'lib/gs1/barcode/errors.rb', line 32

def full_messages
  errors.flat_map do |(attribute_name, errors)|
    errors.uniq.map do |error|
      "#{error.human_message} #{attribute_name.to_s.tr('_', ' ')}"
    end
  end
end

#messagesObject



26
27
28
29
30
# File 'lib/gs1/barcode/errors.rb', line 26

def messages
  errors.each_with_object({}) do |(attribute_name, errors), hash|
    hash[attribute_name] = errors.uniq.map(&:human_message)
  end
end