delayer-deferred 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/delayer/deferred/deferred.rb +4 -0
- data/lib/delayer/deferred/tools.rb +52 -0
- data/lib/delayer/deferred/version.rb +1 -1
- data/lib/delayer/deferred.rb +12 -46
- data/test/deferred_test.rb +50 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15a781ea89e438e07c28ee95dc9eb05e46c87ecd
|
4
|
+
data.tar.gz: e7114c16bf3f0830767431df4c770ba70ac88f5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0720eec371433ad820a9b1fa0a2ba0822517b0b9f5a2a4752641ad4477b89aa09b46a311a95ea62516a8dbd3e1d6f948eb69e9d2532030738c0c3975fe15b2c
|
7
|
+
data.tar.gz: 8a93022f6e221129eb1d24ce8d32dcba57ee8c8f4f512b7bd265ddab32a0b4bc0f87474223a4cb679a8fc0fd652195ca67e0bee71f140c467533739ed50b9c72
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module Delayer::Deferred
|
4
|
+
module Tools
|
5
|
+
def next(&proc)
|
6
|
+
new.next(&proc) end
|
7
|
+
|
8
|
+
def trap(&proc)
|
9
|
+
new.trap(&proc) end
|
10
|
+
|
11
|
+
# 実行中のDeferredを失敗させる。raiseと違って、Exception以外のオブジェクトをtrap()に渡すことができる。
|
12
|
+
# Deferredのnextとtrapの中でだけ呼び出すことができる。
|
13
|
+
# ==== Args
|
14
|
+
# [value] trap()に渡す値
|
15
|
+
# ==== Throw
|
16
|
+
# :__deferredable_fail をthrowする
|
17
|
+
def fail(value)
|
18
|
+
throw(:__deferredable_fail, value) end
|
19
|
+
|
20
|
+
# 複数のdeferredを引数に取って、それら全ての実行が終了したら、
|
21
|
+
# その結果を引数の順番通りに格納したArrayを引数に呼ばれるDeferredを返す。
|
22
|
+
# 引数のDeferredが一つでも失敗するとこのメソッドの返すDeferredも失敗する。
|
23
|
+
# ==== Args
|
24
|
+
# [*args] 終了を待つDeferredオブジェクト
|
25
|
+
# ==== Return
|
26
|
+
# Deferred
|
27
|
+
def when(*args)
|
28
|
+
return self.next{[]} if args.empty?
|
29
|
+
defer, *follow = args
|
30
|
+
raise TypeError, "Argument of Deferred.when must be Delayer::Deferred::Deferredable" unless defer.is_a? Delayer::Deferred::Deferredable
|
31
|
+
if follow.empty?
|
32
|
+
defer.next{|res| [res] }
|
33
|
+
else
|
34
|
+
remain = self.when(*follow)
|
35
|
+
defer.next do |res|
|
36
|
+
remain.next do |follow_res|
|
37
|
+
follow_res.unshift(res) end end end end
|
38
|
+
|
39
|
+
# Kernel#systemを呼び出して、コマンドが成功たら成功するDeferredを返す。
|
40
|
+
# 失敗した場合、trap{}ブロックには $? の値(Process::Status)か、例外が発生した場合それが渡される
|
41
|
+
# ==== Args
|
42
|
+
# [*args] Kernel#system の引数
|
43
|
+
# ==== Return
|
44
|
+
# Deferred
|
45
|
+
def system(*args)
|
46
|
+
delayer.Deferred.Thread.new do
|
47
|
+
if Kernel.system(*args)
|
48
|
+
$?
|
49
|
+
else
|
50
|
+
delayer.Deferred.fail($?) end end end
|
51
|
+
end
|
52
|
+
end
|
data/lib/delayer/deferred.rb
CHANGED
@@ -1,58 +1,24 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
require "delayer"
|
3
|
-
require "delayer/deferred/version"
|
4
|
-
require "delayer/deferred/deferredable"
|
5
3
|
require "delayer/deferred/deferred"
|
6
|
-
require "delayer/deferred/
|
7
|
-
require "delayer/deferred/enumerator"
|
4
|
+
require "delayer/deferred/deferredable"
|
8
5
|
require "delayer/deferred/enumerable"
|
6
|
+
require "delayer/deferred/enumerator"
|
7
|
+
require "delayer/deferred/thread"
|
8
|
+
require "delayer/deferred/tools"
|
9
|
+
require "delayer/deferred/version"
|
9
10
|
|
10
11
|
module Delayer
|
11
12
|
module Deferred
|
12
|
-
extend
|
13
|
-
|
14
|
-
#真ならデバッグ情報を集める
|
15
|
-
attr_accessor :debug
|
13
|
+
extend Delayer::Deferred::Tools
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
# [value] trap()に渡す値
|
21
|
-
# ==== Throw
|
22
|
-
# :__deferredable_fail をthrowする
|
23
|
-
def fail(value)
|
24
|
-
throw(:__deferredable_fail, value) end
|
15
|
+
class << self
|
16
|
+
#真ならデバッグ情報を集める
|
17
|
+
attr_accessor :debug
|
25
18
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
# ==== Args
|
30
|
-
# [defer] 終了を待つDeferredオブジェクト
|
31
|
-
# [*follow] 他のDeferredオブジェクト
|
32
|
-
# ==== Return
|
33
|
-
# Deferred
|
34
|
-
def when(defer, *follow)
|
35
|
-
raise TypeError, "Argument of Deferred.when must be Delayer::Deferred::Deferredable" unless defer.is_a? Delayer::Deferred::Deferredable
|
36
|
-
if follow.empty?
|
37
|
-
defer.next{|res| [res] }
|
38
|
-
else
|
39
|
-
remain = self.when(*follow)
|
40
|
-
defer.next do |res|
|
41
|
-
remain.next do |follow_res|
|
42
|
-
follow_res.unshift(res) end end end end
|
43
|
-
|
44
|
-
# Kernel#systemを呼び出して、コマンドが成功たら成功するDeferredを返す。
|
45
|
-
# 失敗した場合、trap{}ブロックには $? の値(Process::Status)か、例外が発生した場合それが渡される
|
46
|
-
# ==== Args
|
47
|
-
# [*args] Kernel#system の引数
|
48
|
-
# ==== Return
|
49
|
-
# Deferred
|
50
|
-
def system(*args)
|
51
|
-
delayer.Deferred.Thread.new do
|
52
|
-
if Kernel.system(*args)
|
53
|
-
$?
|
54
|
-
else
|
55
|
-
delayer.Deferred.fail($?) end end end
|
19
|
+
def new(*rest, &proc)
|
20
|
+
Delayer::Deferred::Deferred.new(*rest, &proc) end
|
21
|
+
end
|
56
22
|
end
|
57
23
|
|
58
24
|
module Extend
|
data/test/deferred_test.rb
CHANGED
@@ -115,6 +115,21 @@ describe(Delayer::Deferred) do
|
|
115
115
|
assert_equal [1,2,3], result
|
116
116
|
end
|
117
117
|
|
118
|
+
it "default deferred" do
|
119
|
+
result = failure = false
|
120
|
+
eval_all_events do
|
121
|
+
Delayer::Deferred::Deferred.when(
|
122
|
+
Delayer::Deferred::Deferred.new.next{ 1 },
|
123
|
+
Delayer::Deferred::Deferred.new.next{ 2 },
|
124
|
+
Delayer::Deferred::Deferred.new.next{ 3 }
|
125
|
+
).next{ |values|
|
126
|
+
result = values
|
127
|
+
}.trap{ |exception|
|
128
|
+
failure = exception } end
|
129
|
+
assert_equal false, failure
|
130
|
+
assert_equal [1,2,3], result
|
131
|
+
end
|
132
|
+
|
118
133
|
it "give that is not Deferredable" do
|
119
134
|
result = failure = false
|
120
135
|
delayer = Delayer.generate_class
|
@@ -147,6 +162,41 @@ describe(Delayer::Deferred) do
|
|
147
162
|
assert_kind_of RuntimeError, failure
|
148
163
|
assert_equal false, result
|
149
164
|
end
|
165
|
+
|
166
|
+
it "no deferred given" do
|
167
|
+
result = failure = false
|
168
|
+
delayer = Delayer.generate_class
|
169
|
+
eval_all_events(delayer) do
|
170
|
+
delayer.Deferred.when().next{ |values|
|
171
|
+
result = values
|
172
|
+
}.trap{ |exception|
|
173
|
+
failure = exception } end
|
174
|
+
assert_equal false, failure
|
175
|
+
assert_empty result
|
176
|
+
end
|
177
|
+
|
178
|
+
it "no deferred given for default delayer" do
|
179
|
+
result = failure = false
|
180
|
+
eval_all_events do
|
181
|
+
Delayer::Deferred::Deferred.when().next{ |values|
|
182
|
+
result = values
|
183
|
+
}.trap{ |exception|
|
184
|
+
failure = exception } end
|
185
|
+
assert_equal false, failure
|
186
|
+
assert_empty result
|
187
|
+
end
|
188
|
+
|
189
|
+
it "no deferred given for delayer module" do
|
190
|
+
result = failure = false
|
191
|
+
eval_all_events do
|
192
|
+
Delayer::Deferred.when().next{ |values|
|
193
|
+
result = values
|
194
|
+
}.trap{ |exception|
|
195
|
+
failure = exception } end
|
196
|
+
assert_equal false, failure
|
197
|
+
assert_empty result
|
198
|
+
end
|
199
|
+
|
150
200
|
end
|
151
201
|
|
152
202
|
describe "cancel" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayer-deferred
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toshiaki Asai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: delayer
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- lib/delayer/deferred/enumerable.rb
|
92
92
|
- lib/delayer/deferred/enumerator.rb
|
93
93
|
- lib/delayer/deferred/thread.rb
|
94
|
+
- lib/delayer/deferred/tools.rb
|
94
95
|
- lib/delayer/deferred/version.rb
|
95
96
|
- test/deferred_test.rb
|
96
97
|
- test/enumerable_test.rb
|
@@ -117,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
118
|
version: '0'
|
118
119
|
requirements: []
|
119
120
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.4.5
|
121
|
+
rubygems_version: 2.4.5.1
|
121
122
|
signing_key:
|
122
123
|
specification_version: 4
|
123
124
|
summary: Deferred for Delayer
|