Class: Foobara::Outcome
- Inherits:
-
Object
show all
- Defined in:
- foobara-0.7.3/projects/typesystem/projects/common/src/outcome.rb
Defined Under Namespace
Classes: UnsuccessfulOutcomeError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(result: nil, errors: nil, error_collection: ErrorCollection.new) ⇒ Outcome
Returns a new instance of Outcome.
50
51
52
53
54
55
56
57
58
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 50
def initialize(result: nil, errors: nil, error_collection: ErrorCollection.new)
@error_collection = error_collection
self.result = result
if errors
add_errors(errors)
end
end
|
Instance Attribute Details
#error_collection ⇒ Object
Returns the value of attribute error_collection.
48
49
50
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 48
def error_collection
@error_collection
end
|
#result ⇒ Object
Returns the value of attribute result.
47
48
49
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 47
def result
@result
end
|
Class Method Details
.error(error) ⇒ Object
38
39
40
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 38
def error(error)
errors(Util.array(error))
end
|
.errors(*errors) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 24
def errors(*errors)
errors = errors.flatten
if errors.empty?
raise "No errors given"
end
new.tap do |outcome|
outcome.add_errors(errors)
end
end
|
.raise!(errors) ⇒ Object
42
43
44
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 42
def raise!(errors)
self.errors(errors).raise! unless errors.empty?
end
|
.success(result) ⇒ Object
18
19
20
21
22
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 18
def success(result)
new.tap do |outcome|
outcome.result = result
end
end
|
Instance Method Details
#add_errors(*errors) ⇒ Object
66
67
68
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 66
def add_errors(*errors)
error_collection.add_errors(*errors)
end
|
#each_error ⇒ Object
78
79
80
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 78
def each_error(&)
error_collection.each(&)
end
|
#error_keys ⇒ Object
119
120
121
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 119
def error_keys
error_collection.keys
end
|
#errors ⇒ Object
74
75
76
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 74
def errors
error_collection
end
|
#errors_hash ⇒ Object
111
112
113
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 111
def errors_hash
error_collection.errors_hash
end
|
#errors_sentence ⇒ Object
115
116
117
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 115
def errors_sentence
error_collection.to_sentence
end
|
#fatal? ⇒ Boolean
86
87
88
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 86
def fatal?
error_collection.any?(&:fatal?)
end
|
#has_error ⇒ Object
60
61
62
63
64
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 60
def has_error
error_collection.has_error
end
|
#has_errors? ⇒ Boolean
70
71
72
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 70
def has_errors?
error_collection.has_errors?
end
|
#raise! ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 90
def raise!
return if success?
error = error_collection[0]
original_backtrace = error.backtrace_when_initialized
if error_collection.size > 1
error = UnsuccessfulOutcomeError.new(error_collection)
end
error.set_backtrace(original_backtrace)
error.backtrace_when_raised = caller
raise error
end
|
#result! ⇒ Object
106
107
108
109
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 106
def result!
raise!
result
end
|
#success? ⇒ Boolean
82
83
84
|
# File 'projects/typesystem/projects/common/src/outcome.rb', line 82
def success?
!has_errors?
end
|