@@ -10,62 +10,96 @@ module Obligation
10
10
include Dereferenceable
11
11
12
12
# Has the obligation been fulfilled?
13
+ #
13
14
# @return [Boolean]
14
15
def fulfilled?
15
16
state == :fulfilled
16
17
end
17
18
alias_method :realized? , :fulfilled?
18
19
19
20
# Has the obligation been rejected?
21
+ #
20
22
# @return [Boolean]
21
23
def rejected?
22
24
state == :rejected
23
25
end
24
26
25
27
# Is obligation completion still pending?
28
+ #
26
29
# @return [Boolean]
27
30
def pending?
28
31
state == :pending
29
32
end
30
33
31
34
# Is the obligation still unscheduled?
35
+ #
32
36
# @return [Boolean]
33
37
def unscheduled?
34
38
state == :unscheduled
35
39
end
36
40
37
- def completed?
41
+ # Has the obligation completed processing?
42
+ #
43
+ # @return [Boolean]
44
+ def complete?
38
45
[ :fulfilled , :rejected ] . include? state
39
46
end
40
47
48
+ # Has the obligation completed processing?
49
+ #
50
+ # @return [Boolean]
51
+ #
52
+ # @deprecated
53
+ def completed?
54
+ warn '[DEPRECATED] Use #complete? instead'
55
+ complete?
56
+ end
57
+
58
+ # Is the obligation still awaiting completion of processing?
59
+ #
60
+ # @return [Boolean]
41
61
def incomplete?
42
62
[ :unscheduled , :pending ] . include? state
43
63
end
44
64
65
+ # The current value of the obligation. Will be `nil` while the state is
66
+ # pending or the operation has been rejected.
67
+ #
68
+ # @param [Numeric] timeout the maximum time in seconds to wait.
45
69
# @return [Object] see Dereferenceable#deref
46
70
def value ( timeout = nil )
47
71
wait timeout
48
72
deref
49
73
end
50
74
51
- # wait until Obligation is #complete?
52
- # @param [Numeric] timeout the maximum time in second to wait.
75
+ # Wait until obligation is complete or the timeout has been reached.
76
+ #
77
+ # @param [Numeric] timeout the maximum time in seconds to wait.
53
78
# @return [Obligation] self
54
79
def wait ( timeout = nil )
55
80
event . wait ( timeout ) if timeout != 0 && incomplete?
56
81
self
57
82
end
58
83
59
- # wait until Obligation is #complete?
60
- # @param [Numeric] timeout the maximum time in second to wait.
84
+ # Wait until obligation is complete or the timeout is reached. Will re-raise
85
+ # any exceptions raised during processing (but will not raise an exception
86
+ # on timeout).
87
+ #
88
+ # @param [Numeric] timeout the maximum time in seconds to wait.
61
89
# @return [Obligation] self
62
- # @raise [Exception] when #rejected? it raises #reason
63
- def no_error !( timeout = nil )
90
+ # @raise [Exception] raises the reason when rejected
91
+ def wait !( timeout = nil )
64
92
wait ( timeout ) . tap { raise self if rejected? }
65
93
end
94
+ alias_method :no_error! , :wait!
66
95
67
- # @raise [Exception] when #rejected? it raises #reason
96
+ # The current value of the obligation. Will be `nil` while the state is
97
+ # pending or the operation has been rejected. Will re-raise any exceptions
98
+ # raised during processing (but will not raise an exception on timeout).
99
+ #
100
+ # @param [Numeric] timeout the maximum time in seconds to wait.
68
101
# @return [Object] see Dereferenceable#deref
102
+ # @raise [Exception] raises the reason when rejected
69
103
def value! ( timeout = nil )
70
104
wait ( timeout )
71
105
if rejected?
@@ -75,13 +109,21 @@ def value!(timeout = nil)
75
109
end
76
110
end
77
111
112
+ # The current state of the obligation.
113
+ #
114
+ # @return [Symbol] the current state
78
115
def state
79
116
mutex . lock
80
117
@state
81
118
ensure
82
119
mutex . unlock
83
120
end
84
121
122
+ # If an exception was raised during processing this will return the
123
+ # exception object. Will return `nil` when the state is pending or if
124
+ # the obligation has been successfully fulfilled.
125
+ #
126
+ # @return [Exception] the exception raised during processing or `nil`
85
127
def reason
86
128
mutex . lock
87
129
@reason
@@ -134,8 +176,8 @@ def state=(value) # :nodoc:
134
176
mutex . unlock
135
177
end
136
178
137
- # atomic compare and set operation
138
- # state is set to next_state only if current state is == expected_current
179
+ # Atomic compare and set operation
180
+ # State is set to ` next_state` only if ` current state == expected_current`.
139
181
#
140
182
# @param [Symbol] next_state
141
183
# @param [Symbol] expected_current
0 commit comments