Class: Foobara::ErrorCollection
- Inherits:
-
Object
- Object
- Foobara::ErrorCollection
show all
- Defined in:
- foobara-0.0.110/projects/common/src/error_collection.rb
Overview
TODO: inherit array instead of delegating
Defined Under Namespace
Classes: ErrorAlreadySetError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ErrorCollection.
16
17
18
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 16
def initialize
@error_array = []
end
|
Instance Attribute Details
#error_array ⇒ Object
Returns the value of attribute error_array.
14
15
16
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 14
def error_array
@error_array
end
|
Class Method Details
.to_h(errors) ⇒ Object
7
8
9
10
11
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 7
def to_h(errors)
new.tap do |collection|
collection.add_errors(errors)
end.to_h
end
|
Instance Method Details
#add_error(error_or_collection_or_error_hash) ⇒ Object
48
49
50
51
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
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 48
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 ErrorCollection
return add_errors(error_or_collection_or_error_hash.errors)
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
error_array << error
end
|
#add_errors(errors) ⇒ Object
79
80
81
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 79
def add_errors(errors)
Util.array(errors).each { |error| add_error(error) }
end
|
#each_error ⇒ Object
34
35
36
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 34
def each_error(&)
error_array.each(&)
end
|
#errors ⇒ Object
30
31
32
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 30
def errors
error_array
end
|
#has_error?(error) ⇒ Boolean
38
39
40
41
42
43
44
45
46
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 38
def has_error?(error)
unless error.is_a?(Error)
raise ArgumentError, "Can only check if an Error instance is in the collection"
end
error_array.include?(error)
end
|
#has_errors? ⇒ Boolean
24
25
26
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 24
def has_errors?
!empty?
end
|
#keys ⇒ Object
93
94
95
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 93
def keys
error_array.map(&:key)
end
|
#success? ⇒ Boolean
20
21
22
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 20
def success?
empty?
end
|
#to_h ⇒ Object
83
84
85
86
87
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 83
def to_h
error_array.to_h do |error|
[error.key, error.to_h]
end
end
|
#to_sentence ⇒ Object
89
90
91
|
# File 'foobara-0.0.110/projects/common/src/error_collection.rb', line 89
def to_sentence
Util.to_sentence(error_array.map(&:message))
end
|