operation 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8081d9d41eb780b0a6cfd9eab6d0c52e1943200e
4
- data.tar.gz: c97901feb589bd338c2d4973348a75532f247f8c
3
+ metadata.gz: 4c5ab2af692001edf354b6c55769c944e0d6b305
4
+ data.tar.gz: 6e90ab32338fe7d2ee8b5c2587cb5f80366fcda3
5
5
  SHA512:
6
- metadata.gz: 628bf4e3a84a7398fbb19662f5e1f0eddf7534bd819b8f06643ae751d76f49c42737f7bbeb57b21a5003e46ed9d9e69a3a7b82d09741117e523c438515491f4e
7
- data.tar.gz: a14af00be7b85f775a043aec4d11cfca155aa6acf11d77ad05728bd12499d860390eb904e1cd95f2e5a7fa96b4a6e398c6e641178ce777991f34c43301f7cb4f
6
+ metadata.gz: a82fdaddecfd11cc0807048b908ac4ca11af4c03ee48033d43c6083f30329a8d0d815bb51085154d5913313bf69c93e73c7692b99081ca18e09acb23c9d7694d
7
+ data.tar.gz: f2656a9c0e58277130dcf18fe48e8e2e0d1008dffedc26a106ae448944d5746025dda1ede2e31d649289b50bd78d711d760cb6374c3555c0010588a70979ef94
@@ -4,6 +4,17 @@ class Operation
4
4
 
5
5
  # Public Callbacks
6
6
 
7
+ def on_start(&block)
8
+ return unless block
9
+
10
+ if started?
11
+ block.call
12
+ else
13
+ startables.unshift block
14
+ end
15
+ self
16
+ end
17
+
7
18
  def on_progress(&block)
8
19
  return unless block
9
20
  progressables.unshift block
@@ -36,7 +47,7 @@ class Operation
36
47
  return unless block
37
48
 
38
49
  if succeeded? || failed?
39
- block.call(*arguments)
50
+ block.call
40
51
  else
41
52
  finishables.unshift block
42
53
  end
@@ -62,16 +73,22 @@ class Operation
62
73
  status == :failed
63
74
  end
64
75
 
76
+ def started?
77
+ status != :unknown
78
+ end
79
+
65
80
  # Public State Setters
66
81
 
67
82
  def progress!(new_percent, code = nil)
68
83
  return if succeeded? || failed?
69
84
  @percent = new_percent.to_i
85
+ start! unless started?
70
86
  @status = code unless [:succeeded, :failed].include?(code)
71
87
 
72
88
  progressables.each do |callback|
73
89
  callback.call self
74
90
  end
91
+ self
75
92
  end
76
93
 
77
94
  def sub_progress!(from, till, sub)
@@ -82,6 +99,11 @@ class Operation
82
99
  new_percent = from.to_i + factor.to_i
83
100
  end
84
101
  progress! new_percent, sub.status
102
+ self
103
+ end
104
+
105
+ def start!(*args)
106
+ set_status :started, *args
85
107
  end
86
108
 
87
109
  def succeed!(*args)
@@ -98,28 +120,48 @@ class Operation
98
120
  @arguments = args unless args.empty?
99
121
  @status = new_status
100
122
 
123
+ call_startables!
101
124
  if succeeded? && !successables.empty?
102
125
  while callback = successables.pop
103
126
  callback.call(*arguments)
104
127
  end
105
- while callback = finishables.pop
106
- callback.call(*arguments)
107
- end
128
+ call_finishables!
108
129
  failables.clear if !failables.empty?
109
130
 
110
131
  elsif failed? && !failables.empty?
111
132
  while callback = failables.pop
112
133
  callback.call(*arguments)
113
134
  end
114
- while callback = finishables.pop
115
- callback.call(*arguments)
116
- end
135
+ call_finishables!
117
136
  successables.clear if !failables.empty?
118
137
  end
138
+ self
139
+ end
140
+
141
+ def call_startables!
142
+ return unless startables
143
+ return if startables.empty?
144
+
145
+ while callback = startables.pop
146
+ callback.call
147
+ end
148
+ end
149
+
150
+ def call_finishables!
151
+ return unless finishables
152
+ return if finishables.empty?
153
+
154
+ while callback = finishables.pop
155
+ callback.call
156
+ end
119
157
  end
120
158
 
121
159
  # Internal Methods
122
160
 
161
+ def startables
162
+ @startables ||= []
163
+ end
164
+
123
165
  def successables
124
166
  @successables ||= []
125
167
  end
@@ -1,8 +1,8 @@
1
1
  class Operation
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
- MINOR = 3
5
- TINY = 2
4
+ MINOR = 4
5
+ TINY = 0
6
6
  PRE = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join '.'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: operation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - halo