Class: Flowy::Success
- Inherits:
-
Object
show all
- Includes:
- Result
- Defined in:
- lib/flowy/success.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Result
_collect_results, _deep_merge, failure, success, wrap
Constructor Details
#initialize(data: {}, warnings: []) ⇒ Success
Returns a new instance of Success.
7
8
9
10
|
# File 'lib/flowy/success.rb', line 7
def initialize(data: {}, warnings: [])
@data = data
@warnings = warnings
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5
6
7
|
# File 'lib/flowy/success.rb', line 5
def data
@data
end
|
#warnings ⇒ Object
Returns the value of attribute warnings.
5
6
7
|
# File 'lib/flowy/success.rb', line 5
def warnings
@warnings
end
|
Instance Method Details
#+(other) ⇒ Object
12
13
14
|
# File 'lib/flowy/success.rb', line 12
def +(other)
self.class.new(data: Flowy::Result._deep_merge(data, other.data))
end
|
#and_then ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'lib/flowy/success.rb', line 41
def and_then
result = yield self
unless result.is_a?(Flowy::Success) || result.is_a?(Flowy::Failure)
raise TypeError, "and_then block must return a Flowy::Success or Flowy::Failure, got #{result.class}"
end
result
end
|
#failure? ⇒ Boolean
28
29
30
|
# File 'lib/flowy/success.rb', line 28
def failure?
false
end
|
#map_failure ⇒ Object
54
55
56
|
# File 'lib/flowy/success.rb', line 54
def map_failure(**)
self
end
|
#merge_data(extra = nil) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/flowy/success.rb', line 67
def merge_data( = nil)
= block_given? ? yield(data) :
raise ArgumentError, 'merge_data requires a Hash' unless .is_a?(Hash)
self.class.new(data: Flowy::Result._deep_merge(data, ), warnings: warnings)
end
|
#on_failure ⇒ Object
37
38
39
|
# File 'lib/flowy/success.rb', line 37
def on_failure
self
end
|
#on_success {|_self| ... } ⇒ Object
32
33
34
35
|
# File 'lib/flowy/success.rb', line 32
def on_success
yield self
self
end
|
#or_else ⇒ Object
50
51
52
|
# File 'lib/flowy/success.rb', line 50
def or_else
self
end
|
#raise! ⇒ Object
58
59
60
|
# File 'lib/flowy/success.rb', line 58
def raise!
self
end
|
#success? ⇒ Boolean
24
25
26
|
# File 'lib/flowy/success.rb', line 24
def success?
true
end
|
#tap {|_self| ... } ⇒ Object
62
63
64
65
|
# File 'lib/flowy/success.rb', line 62
def tap
yield self
self
end
|
#to_hash ⇒ Object
16
17
18
19
20
21
22
|
# File 'lib/flowy/success.rb', line 16
def to_hash
{
success: true,
data: data,
warnings: warnings
}
end
|