Module: Operation::Deferrable

Included in:
Operation
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

#fail!(*args) ⇒ Object



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

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

#failablesObject



83
84
85
# File 'lib/operation/deferrable.rb', line 83

def failables
  @failables ||= []
end

#failed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/operation/deferrable.rb', line 37

def failed?
  status == :failed
end

#on_failure(&block) ⇒ Object



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

def on_failure(&block)
  return unless block

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

#on_progress(&block) ⇒ Object



5
6
7
8
9
# File 'lib/operation/deferrable.rb', line 5

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

#on_success(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/operation/deferrable.rb', line 11

def on_success(&block)
  return unless block

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

#progress(percent, code) ⇒ Object



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

def progress(percent, code)
  return if succeeded? || failed?

  progressables.each do |callback|
    callback.call ::Operation::Progress.new(percent, code)
  end
end

#progressablesObject



87
88
89
# File 'lib/operation/deferrable.rb', line 87

def progressables
  @progressables ||= []
end

#set_status(new_status, *args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/operation/deferrable.rb', line 45

def set_status(new_status, *args)
  @arguments = args
  @status = new_status

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

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

#statusObject



41
42
43
# File 'lib/operation/deferrable.rb', line 41

def status
  @status ||= :unknown
end

#succeed!(*args) ⇒ Object



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

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

#succeeded?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/operation/deferrable.rb', line 33

def succeeded?
  status == :succeeded
end

#successablesObject



79
80
81
# File 'lib/operation/deferrable.rb', line 79

def successables
  @successables ||= []
end