dharma 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,7 +5,7 @@ Dharma
5
5
 
6
6
  A futures and promises library [modeled after Scala].
7
7
 
8
- ![Dharma Initiative](http://wallpaperstock.net/lost-dharma-initiative_wallpapers_17255_1920x1440.jpg)
8
+ ![Dharma Initiative](http://api.ning.com/files/GO1Rm-PT2poZlNj*ZI2pv2eil*Pfx1whNze5EWTmXuMthXp*vt2O8Zledx7JGilmjCWGHSNs3tK-j0kGQlJNDn0APhMFwXrY/Dharma_Initiative_Wallpaper_by_MisterCrankyGeek.jpg)
9
9
 
10
10
 
11
11
  # License
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'dharma'
16
- s.version = '0.9.0'
17
- s.date = '2013-05-24'
16
+ s.version = '0.9.1'
17
+ s.date = '2013-05-30'
18
18
 
19
19
  ## Make sure your summary is short. The description may be as long
20
20
  ## as you like.
@@ -1,9 +1,10 @@
1
1
  module Dharma
2
- VERSION = '0.9.0'
2
+ VERSION = '0.9.1'
3
3
 
4
4
  class PromiseFailure < RuntimeError; end
5
5
  class IllegalStateException < RuntimeError; end
6
6
  class TimeoutException < RuntimeError; end
7
+ class NoSuchElementException < RuntimeError; end
7
8
 
8
9
  def self.default_executor
9
10
  @default_executor ||= Dharma::ThreadExecutor.new
@@ -31,11 +32,29 @@ module Dharma
31
32
  end
32
33
  end
33
34
 
35
+ def self.first_completed_of(promises)
36
+ p = Dharma.promise
37
+ promises.each do |promies|
38
+ promise.on_complete do |value, as|
39
+ p.try_complete(value, as)
40
+ end
41
+ end
42
+ p
43
+ end
44
+
34
45
  def self.trace_completion(tag, promise, logger = Rails.logger)
35
46
  promise.on_complete do |value, as|
36
47
  logger.info "#{tag}: #{as}: #{value.inspect}"
37
48
  end
38
49
  end
50
+
51
+ def self.ready(promise, duration = nil)
52
+ promise.ready(duration)
53
+ end
54
+
55
+ def self.result(promise, duration = nil)
56
+ promise.result(duration)
57
+ end
39
58
  end
40
59
 
41
60
  require 'dharma/promise'
@@ -47,7 +47,18 @@ module Dharma
47
47
  cb ||= block
48
48
  p = Dharma.promise
49
49
 
50
- on_complete { |value| p.complete(cb.call(value)) }
50
+ on_complete do |value, as|
51
+ case as
52
+ when :failure
53
+ begin
54
+ p.complete(cb.call(value))
55
+ rescue => e
56
+ p.failure(e)
57
+ end
58
+ when :success
59
+ p.success(value)
60
+ end
61
+ end
51
62
 
52
63
  p
53
64
  end
@@ -64,12 +75,74 @@ module Dharma
64
75
  rescue => e
65
76
  p.failure(e)
66
77
  end
67
- else
78
+ when :success
68
79
  p.complete(value)
69
80
  end
70
81
  end
71
82
 
72
83
  p
73
84
  end
85
+
86
+ def map(cb = nil, &block)
87
+ cb ||= block
88
+ p = Dharma.promise
89
+
90
+ on_complete do |value, as|
91
+ begin
92
+ case as
93
+ when :success
94
+ p.success(cb.call(value))
95
+ when :failure
96
+ p.failure(value)
97
+ end
98
+ rescue => e
99
+ p.failure(e)
100
+ end
101
+ end
102
+
103
+ p
104
+ end
105
+
106
+ def flat_map(cb = nil, &block)
107
+ cb ||= block
108
+ p = Dharma.promise
109
+
110
+ on_complete do |value, as|
111
+ begin
112
+ case as
113
+ when :success
114
+ cb.call(value).on_complete do |value2, as2|
115
+ case as2
116
+ when :success
117
+ p.success(value2)
118
+ when :failure
119
+ p.failure(value2)
120
+ end
121
+ end
122
+ when :failure
123
+ p.failure(value)
124
+ end
125
+ rescue => e
126
+ p.failure(e)
127
+ end
128
+ end
129
+
130
+ p
131
+ end
132
+
133
+ def failed
134
+ p = Dharma.promise
135
+
136
+ on_complete do |value, as|
137
+ case as
138
+ when :failure
139
+ p.success(value)
140
+ when :success
141
+ p.failure(NoSuchElementException.new("Future.failed not completed with an exception."))
142
+ end
143
+ end
144
+
145
+ p
146
+ end
74
147
  end
75
148
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 0
9
- version: 0.9.0
8
+ - 1
9
+ version: 0.9.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Eric Lindvall
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-05-24 00:00:00 -07:00
17
+ date: 2013-05-30 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20