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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63bcf7cda061faf5664805076d69ee8026ea25f0
4
- data.tar.gz: da216a0035ae4fe7a33d64f8f9bdec16aa2427d1
3
+ metadata.gz: 15a781ea89e438e07c28ee95dc9eb05e46c87ecd
4
+ data.tar.gz: e7114c16bf3f0830767431df4c770ba70ac88f5c
5
5
  SHA512:
6
- metadata.gz: d0c86e00bb6704c4a06e6c6a1898bb9dcbcfec5da357478029d0d2a82b0675b500c10ef12091a465035d3d564022d52cd645c50e57e4f1865ba3f40cd454ebd4
7
- data.tar.gz: 1d494802f132cdb51133922298c9e1df02a2d8fa40fa3fbdb379e2c3116c865ee30fb3cee83e18ae8bc7b219ca179a5aa5f8c16bd3edf11ac531a328c816a367
6
+ metadata.gz: f0720eec371433ad820a9b1fa0a2ba0822517b0b9f5a2a4752641ad4477b89aa09b46a311a95ea62516a8dbd3e1d6f948eb69e9d2532030738c0c3975fe15b2c
7
+ data.tar.gz: 8a93022f6e221129eb1d24ce8d32dcba57ee8c8f4f512b7bd265ddab32a0b4bc0f87474223a4cb679a8fc0fd652195ca67e0bee71f140c467533739ed50b9c72
@@ -1,6 +1,10 @@
1
1
  # -*- coding: utf-8 -*-
2
+ require "delayer/deferred/deferredable"
3
+ require "delayer/deferred/tools"
4
+
2
5
  module Delayer::Deferred
3
6
  class Deferred
7
+ extend Delayer::Deferred::Tools
4
8
  include Deferredable
5
9
 
6
10
  def self.inherited(subclass)
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Delayer
2
2
  module Deferred
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.2"
4
4
  end
5
5
  end
@@ -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/thread"
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 self
13
-
14
- #真ならデバッグ情報を集める
15
- attr_accessor :debug
13
+ extend Delayer::Deferred::Tools
16
14
 
17
- # 実行中のDeferredを失敗させる。raiseと違って、Exception以外のオブジェクトをtrap()に渡すことができる。
18
- # Deferredのnextとtrapの中でだけ呼び出すことができる。
19
- # ==== Args
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
- # 複数のdeferredを引数に取って、それら全ての実行が終了したら、
27
- # その結果を引数の順番通りに格納したArrayを引数に呼ばれるDeferredを返す。
28
- # 引数のDeferredが一つでも失敗するとこのメソッドの返すDeferredも失敗する。
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
@@ -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.1
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-08-26 00:00:00.000000000 Z
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