Class: Foobara::ErrorCollection
- Inherits:
-
Array
- Object
- Array
- Foobara::ErrorCollection
show all
- Defined in:
- foobara-0.1.7/projects/common/src/error_collection.rb
Defined Under Namespace
Classes: ErrorAlreadySetError
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.to_h(errors) ⇒ Object
6
7
8
9
10
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 6
def to_h(errors)
new.tap do |collection|
collection.add_errors(errors)
end.errors_hash
end
|
Instance Method Details
#add_error(error_or_collection_or_error_hash) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 52
def add_error(error_or_collection_or_error_hash)
error = case error_or_collection_or_error_hash
when Error
error_or_collection_or_error_hash
when Hash
if error_or_collection_or_error_hash.key?(:symbol) &&
error_or_collection_or_error_hash.key?(:message)
Error.new(**error_or_collection_or_error_hash)
else
raise ArgumentError,
"if passing a hash of error args it must include symbol and message at least"
end
else
raise ArgumentError, "Not sure how to convert #{error_or_collection_or_error_hash.inspect} " \
"into an error. Can handle a hash of error " \
"args or 3 arguments for symbol, message, and context, or, of course, an Error"
end
if has_error?(error)
raise ErrorAlreadySetError, "cannot set #{error} more than once"
end
self << error
end
|
#add_errors(errors) ⇒ Object
81
82
83
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 81
def add_errors(errors)
Util.array(errors).each { |error| add_error(error) }
end
|
#each_error ⇒ Object
35
36
37
38
39
40
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 35
def each_error(&)
warn "DEPRECATED: This method will be deprecated in the coming version"
each(&)
end
|
#error_array ⇒ Object
28
29
30
31
32
33
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 28
def error_array
warn "DEPRECATED: Do not call ErrorCollection#error_array instead just use the collection directly."
self
end
|
#errors ⇒ Object
21
22
23
24
25
26
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 21
def errors
warn "DEPRECATED: Do not call ErrorCollection#errors instead just use the collection directly."
self
end
|
#errors_hash ⇒ Object
85
86
87
88
89
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 85
def errors_hash
each_with_object({}) do |error, hash|
hash[error.key] = error.to_h
end
end
|
#has_error?(error) ⇒ Boolean
42
43
44
45
46
47
48
49
50
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 42
def has_error?(error)
unless error.is_a?(Error)
raise ArgumentError, "Can only check if an Error instance is in the collection"
end
include?(error)
end
|
#has_errors? ⇒ Boolean
17
18
19
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 17
def has_errors?
!empty?
end
|
#keys ⇒ Object
95
96
97
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 95
def keys
map(&:key)
end
|
#success? ⇒ Boolean
13
14
15
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 13
def success?
empty?
end
|
#to_h ⇒ Object
99
100
101
102
103
104
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 99
def to_h
warn "DEPRECATED: Use #errors_hash instead"
errors_hash
end
|
#to_sentence ⇒ Object
91
92
93
|
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 91
def to_sentence
Util.to_sentence(map(&:message))
end
|