operation 1.2.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/operation.rb +0 -1
- data/lib/operation/core.rb +0 -1
- data/lib/operation/deferrable.rb +52 -22
- data/lib/operation/version.rb +2 -2
- metadata +1 -2
- data/lib/operation/progress.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 350d9462e55e2a29b24502967a4c5f96559bf41f
|
4
|
+
data.tar.gz: 31b2e4cdc6e21409be85a770a42fa93287e03c7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eac059b32ff9ee284ad0c024e742fdd6e95e469b5b239ddf3b1f7909408d75560a5aa8209bd5413f82d0f1e0281ba53d3ecd946536de0d1a9387e53355062854
|
7
|
+
data.tar.gz: 6aa9eea323fe338cf322fc4a0257c4e7aadc8807d98845a3d8637bd7b175b22dfd6b2c661d9cbd1a7b6bf62cd75198a68a09182ae02e46ace0104ce0f702edc4
|
data/lib/operation.rb
CHANGED
data/lib/operation/core.rb
CHANGED
data/lib/operation/deferrable.rb
CHANGED
@@ -2,6 +2,8 @@ class Operation
|
|
2
2
|
module Deferrable
|
3
3
|
attr_reader :arguments
|
4
4
|
|
5
|
+
# Public Callbacks
|
6
|
+
|
5
7
|
def on_progress(&block)
|
6
8
|
return unless block
|
7
9
|
progressables.unshift block
|
@@ -30,6 +32,17 @@ class Operation
|
|
30
32
|
self
|
31
33
|
end
|
32
34
|
|
35
|
+
# Public State Getters
|
36
|
+
|
37
|
+
def status
|
38
|
+
@status ||= :unknown
|
39
|
+
end
|
40
|
+
|
41
|
+
def percent
|
42
|
+
return 100 if succeeded?
|
43
|
+
@percent ||= 0
|
44
|
+
end
|
45
|
+
|
33
46
|
def succeeded?
|
34
47
|
status == :succeeded
|
35
48
|
end
|
@@ -38,12 +51,40 @@ class Operation
|
|
38
51
|
status == :failed
|
39
52
|
end
|
40
53
|
|
41
|
-
|
42
|
-
|
54
|
+
# Public State Setters
|
55
|
+
|
56
|
+
def progress!(new_percent, code = nil)
|
57
|
+
return if succeeded? || failed?
|
58
|
+
@percent = new_percent.to_i
|
59
|
+
@status = code unless %i(succeeded failed).include?(code)
|
60
|
+
|
61
|
+
progressables.each do |callback|
|
62
|
+
callback.call self
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def sub_progress!(from, till, sub)
|
67
|
+
if sub.percent.zero?
|
68
|
+
new_percent = 0
|
69
|
+
else
|
70
|
+
factor = (till.to_f - from.to_f) * (sub.percent.to_f / 100)
|
71
|
+
new_percent = from.to_i + factor.to_i
|
72
|
+
end
|
73
|
+
progress! new_percent, sub.status
|
74
|
+
end
|
75
|
+
|
76
|
+
def succeed!(*args)
|
77
|
+
set_status :succeeded, *args
|
78
|
+
end
|
79
|
+
|
80
|
+
def fail!(*args)
|
81
|
+
set_status :failed, *args
|
43
82
|
end
|
44
83
|
|
84
|
+
# Status Setters
|
85
|
+
|
45
86
|
def set_status(new_status, *args)
|
46
|
-
@arguments = args
|
87
|
+
@arguments = args unless args.empty?
|
47
88
|
@status = new_status
|
48
89
|
|
49
90
|
if succeeded? && !successables.empty?
|
@@ -60,25 +101,7 @@ class Operation
|
|
60
101
|
end
|
61
102
|
end
|
62
103
|
|
63
|
-
|
64
|
-
return if succeeded? || failed?
|
65
|
-
|
66
|
-
progressables.each do |callback|
|
67
|
-
if percent.is_a?(::Operation::Progress)
|
68
|
-
callback.call percent
|
69
|
-
else
|
70
|
-
callback.call ::Operation::Progress.new(percent, code)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def succeed!(*args)
|
76
|
-
set_status :succeeded, *args
|
77
|
-
end
|
78
|
-
|
79
|
-
def fail!(*args)
|
80
|
-
set_status :failed, *args
|
81
|
-
end
|
104
|
+
# Internal Methods
|
82
105
|
|
83
106
|
def successables
|
84
107
|
@successables ||= []
|
@@ -94,3 +117,10 @@ class Operation
|
|
94
117
|
|
95
118
|
end
|
96
119
|
end
|
120
|
+
|
121
|
+
class Operation
|
122
|
+
class Defer
|
123
|
+
include ::Operation::Deferrable
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
data/lib/operation/version.rb
CHANGED
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.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- halo
|
@@ -80,7 +80,6 @@ files:
|
|
80
80
|
- lib/operation.rb
|
81
81
|
- lib/operation/core.rb
|
82
82
|
- lib/operation/deferrable.rb
|
83
|
-
- lib/operation/progress.rb
|
84
83
|
- lib/operation/version.rb
|
85
84
|
- lib/operations.rb
|
86
85
|
homepage: https://github.com/halo/operation
|
data/lib/operation/progress.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
class Operation
|
2
|
-
class Progress
|
3
|
-
|
4
|
-
def initialize(percent, code = nil)
|
5
|
-
@percent, @code = percent, code
|
6
|
-
end
|
7
|
-
|
8
|
-
def percent
|
9
|
-
@percent.to_i
|
10
|
-
end
|
11
|
-
|
12
|
-
def code
|
13
|
-
return if @code.to_s == ''
|
14
|
-
@code.to_s.to_sym
|
15
|
-
end
|
16
|
-
|
17
|
-
def of(total)
|
18
|
-
partial_percent = (percent.to_f / total.to_i).to_i
|
19
|
-
self.class.new partial_percent, code
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|