Class: Mozaik::Service::Errors
- Inherits:
-
Object
- Object
- Mozaik::Service::Errors
- Defined in:
- lib/mozaik/service/errors.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #add(name, type) ⇒ Object
- #as_json(_options) ⇒ Object
- #empty? ⇒ Boolean
- #error?(name, type) ⇒ Boolean
- #full_details ⇒ Object
-
#initialize(service) ⇒ Errors
constructor
A new instance of Errors.
- #inspect ⇒ Object
- #merge(other) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(service) ⇒ Errors
Returns a new instance of Errors.
6 7 8 9 10 |
# File 'lib/mozaik/service/errors.rb', line 6 def initialize(service) @service_class = service.class @errors = {} @error_sources = {} end |
Instance Method Details
#==(other) ⇒ Object
29 30 31 |
# File 'lib/mozaik/service/errors.rb', line 29 def ==(other) to_h == other.to_h end |
#[](key) ⇒ Object
25 26 27 |
# File 'lib/mozaik/service/errors.rb', line 25 def [](key) @errors[key] end |
#add(name, type) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mozaik/service/errors.rb', line 12 def add(name, type) return self if error?(name, type) name = name.to_sym @error_sources[name] ||= {} @error_sources[name][type] = @service_class @errors[name] ||= [] @errors[name] << { type: type.to_sym } self end |
#as_json(_options) ⇒ Object
61 62 63 |
# File 'lib/mozaik/service/errors.rb', line 61 def as_json() to_h end |
#empty? ⇒ Boolean
37 38 39 |
# File 'lib/mozaik/service/errors.rb', line 37 def empty? @errors.empty? end |
#error?(name, type) ⇒ Boolean
41 42 43 |
# File 'lib/mozaik/service/errors.rb', line 41 def error?(name, type) (self[name.to_sym] || []).any? { |error| error.fetch(:type) == type.to_sym } end |
#full_details ⇒ Object
55 56 57 58 59 |
# File 'lib/mozaik/service/errors.rb', line 55 def full_details @errors.map do |key, errors| [key, errors.map { |error| { type: error[:type], message: translate_error(key, error[:type]) } }] end.to_h end |
#inspect ⇒ Object
45 46 47 |
# File 'lib/mozaik/service/errors.rb', line 45 def inspect "#{self.class} #{@errors}" end |
#merge(other) ⇒ Object
49 50 51 52 53 |
# File 'lib/mozaik/service/errors.rb', line 49 def merge(other) merge_errors(other.errors) merge_error_sources(other.error_sources) self end |
#to_h ⇒ Object
33 34 35 |
# File 'lib/mozaik/service/errors.rb', line 33 def to_h @errors end |