Module: Operation::Deferrable

Included in:
Defer
Defined in:
lib/operation/deferrable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



3
4
5
# File 'lib/operation/deferrable.rb', line 3

def arguments
  @arguments
end

Instance Method Details

#call_finishables!Object



150
151
152
153
154
155
156
157
# File 'lib/operation/deferrable.rb', line 150

def call_finishables!
  return unless finishables
  return if finishables.empty?

  while callback = finishables.pop
    callback.call
  end
end

#call_startables!Object



141
142
143
144
145
146
147
148
# File 'lib/operation/deferrable.rb', line 141

def call_startables!
  return unless startables
  return if startables.empty?

  while callback = startables.pop
    callback.call
  end
end

#fail!(*args) ⇒ Object



113
114
115
# File 'lib/operation/deferrable.rb', line 113

def fail!(*args)
  set_status :failed, *args
end

#failablesObject



169
170
171
# File 'lib/operation/deferrable.rb', line 169

def failables
  @failables ||= []
end

#failed?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/operation/deferrable.rb', line 72

def failed?
  status == :failed
end

#finishablesObject



177
178
179
# File 'lib/operation/deferrable.rb', line 177

def finishables
  @finishables ||= []
end

#on_failure(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/operation/deferrable.rb', line 35

def on_failure(&block)
  return unless block

  if failed?
    block.call(*arguments)
  elsif !succeeded?
    failables.unshift block
  end
  self
end

#on_finish(&block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/operation/deferrable.rb', line 46

def on_finish(&block)
  return unless block

  if succeeded? || failed?
    block.call
  else
    finishables.unshift block
  end
  self
end

#on_progress(&block) ⇒ Object



18
19
20
21
22
# File 'lib/operation/deferrable.rb', line 18

def on_progress(&block)
  return unless block
  progressables.unshift block
  self
end

#on_start(&block) ⇒ Object

Public Callbacks



7
8
9
10
11
12
13
14
15
16
# File 'lib/operation/deferrable.rb', line 7

def on_start(&block)
  return unless block

  if started?
    block.call
  else
    startables.unshift block
  end
  self
end

#on_success(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/operation/deferrable.rb', line 24

def on_success(&block)
  return unless block

  if succeeded?
    block.call(*arguments)
  elsif !failed?
    successables.unshift block
  end
  self
end

#percentObject



63
64
65
66
# File 'lib/operation/deferrable.rb', line 63

def percent
  return 100 if succeeded?
  @percent ||= 0
end

#progress!(new_percent, code = nil) ⇒ Object

Public State Setters



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/operation/deferrable.rb', line 82

def progress!(new_percent, code = nil)
  return if succeeded? || failed?
  @percent = new_percent.to_i
  start! unless started?
  @status = code unless [:succeeded, :failed].include?(code)

  progressables.each do |callback|
    callback.call self
  end
  self
end

#progressablesObject



173
174
175
# File 'lib/operation/deferrable.rb', line 173

def progressables
  @progressables ||= []
end

#set_status(new_status, *args) ⇒ Object

Status Setters



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/operation/deferrable.rb', line 119

def set_status(new_status, *args)
  @arguments = args unless args.empty?
  @status = new_status

  call_startables!
  if succeeded? && !successables.empty?
    while callback = successables.pop
      callback.call(*arguments)
    end
    call_finishables!
    failables.clear if !failables.empty?

  elsif failed? && !failables.empty?
    while callback = failables.pop
      callback.call(*arguments)
    end
    call_finishables!
    successables.clear if !failables.empty?
  end
  self
end

#start!(*args) ⇒ Object



105
106
107
# File 'lib/operation/deferrable.rb', line 105

def start!(*args)
  set_status :started, *args
end

#startablesObject

Internal Methods



161
162
163
# File 'lib/operation/deferrable.rb', line 161

def startables
  @startables ||= []
end

#started?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/operation/deferrable.rb', line 76

def started?
  status != :unknown
end

#statusObject

Public State Getters



59
60
61
# File 'lib/operation/deferrable.rb', line 59

def status
  @status ||= :unknown
end

#sub_progress!(from, till, sub) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/operation/deferrable.rb', line 94

def sub_progress!(from, till, sub)
  if sub.percent.zero?
    new_percent = 0
  else
    factor = (till.to_f - from.to_f) * (sub.percent.to_f / 100)
    new_percent = from.to_i + factor.to_i
  end
  progress! new_percent, sub.status
  self
end

#succeed!(*args) ⇒ Object



109
110
111
# File 'lib/operation/deferrable.rb', line 109

def succeed!(*args)
  set_status :succeeded, *args
end

#succeeded?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/operation/deferrable.rb', line 68

def succeeded?
  status == :succeeded
end

#successablesObject



165
166
167
# File 'lib/operation/deferrable.rb', line 165

def successables
  @successables ||= []
end