mr_darcy 0.3.0 → 0.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: 5d61c338943c3f4d6e28c6cffdbe3c219d0e0786
4
- data.tar.gz: 5dc024d54bee45d0c3f2b2c2dad483a8bb95b980
3
+ metadata.gz: a30f4cd28db6fa9241372a6ba419149e78acb95f
4
+ data.tar.gz: 14599ca6b1ef196eb134e82eebecf9c1a9b3da4e
5
5
  SHA512:
6
- metadata.gz: 158121e9b077abf2a36fe879548470fcc8338ed50aeff01ccea68b607f470d03cb24d06fdbf7a2167a904531f28f0349e761be2e6dfcef6490180acf4e57f348
7
- data.tar.gz: 8daa06f36f9f7a867098adb3a89cb20cd1833157ce457aac38ff61eb81f6d5f264a9cdc862b667ce7c8e5a61284628c06401a63eb9d91ec8ca9dbb907ea818ca
6
+ metadata.gz: 11879372c631def455d5b15453be331adef2187f0d0076c8cfab6297f8703feffec4e394f123f05fd110b2ebcfefc37df2a0fc7ca02755517206d49043af77e8
7
+ data.tar.gz: da5f40370e5d5d484ab29d9d29100d658742c2134bf82d743da90f92d8f9a0d21ed1f1c4184115b0f8b505847c23c048e8eaca3e89fbde021b13b944d0f8393d
data/README.md CHANGED
@@ -33,6 +33,45 @@ MrDarcy is definitely experimental, and was mostly built over the weekend of
33
33
  amazing and sexy [@breccan](https://twitter.com/breccan) and
34
34
  [@eoinkelly](https://twitter.com/eoinkelly).
35
35
 
36
+ ### API Changes (0.4.0 and above)
37
+
38
+ As of version `0.4.0` the `instance_exec` of promise blocks has been removed
39
+ (meaning that you can't call `resolve` or `reject` from directly inside your
40
+ promise blocks). This is becuase `instance_exec` changes the binding of
41
+ `self` inside the promise, and will cause calls to enclosed methods to fail.
42
+
43
+ For example:
44
+
45
+ ```ruby
46
+ class InstanceExecExample
47
+
48
+ def example_method
49
+ puts "I did a thing"
50
+ end
51
+
52
+ def make_promise
53
+ MrDarcy.promise { resolve example_method }.raise
54
+ end
55
+ end
56
+ ```
57
+
58
+ Will raise:
59
+
60
+ ```
61
+ NameError in `block in make_promise': undefined local variable or method `example_method' for #<MrDarcy::Promise::DSL:0x007fcc542d75d0>
62
+ ```
63
+
64
+ As of `0.4.0` the DSL object wll be passed to the promise block as an argument,
65
+ therefore:
66
+
67
+ ```
68
+ def make_promise
69
+ MrDarcy.promise { |promise| promise.resolve example_method }.raise
70
+ end
71
+ ```
72
+
73
+ will not raise an exception.
74
+
36
75
  #### Should I use MrDarcy in Production?
37
76
 
38
77
  No.
@@ -51,15 +90,15 @@ Here's an example:
51
90
  ```ruby
52
91
  # We're going to wrap an asynchronous web request using EventMachine
53
92
  # in a promise:
54
- data = MrDarcy.promise do
93
+ data = MrDarcy.promise do |promise|
55
94
  EM.run do
56
95
  http = EM.HttpRequest.new('http://camp.ruby.org.nz/').get
57
96
  http.errback do
58
- reject http.error
97
+ promise.reject http.error
59
98
  EM.stop
60
99
  end
61
100
  http.callback do
62
- resolve http.response
101
+ promise.resolve http.response
63
102
  EM.stop
64
103
  end
65
104
  end
@@ -86,12 +125,12 @@ doing async ruby:
86
125
  or reject the promise.
87
126
 
88
127
  ```ruby
89
- MrDarcy.promise do
128
+ MrDarcy.promise do |promise|
90
129
  accellerate_the_delorean
91
130
  if speed >= 88
92
- resolve :time_flux_initiated
131
+ promise.resolve :time_flux_initiated
93
132
  else
94
- reject :engage_service_brake
133
+ promise.reject :engage_service_brake
95
134
  end
96
135
  end
97
136
  ```
@@ -102,9 +141,9 @@ doing async ruby:
102
141
  `fail` calls.
103
142
 
104
143
  ```ruby
105
- MrDarcy.promise do
144
+ MrDarcy.promise do |promise|
106
145
  i = rand
107
- i > 0.5 ? resolve i : reject i
146
+ i > 0.5 ? promise.resolve i : promise.reject i
108
147
  end.then |value|
109
148
  # success
110
149
  end.fail |value|
@@ -118,8 +157,8 @@ doing async ruby:
118
157
  within the `fail` block to pass it along to the next `fail` block.
119
158
 
120
159
  ```ruby
121
- MrDarcy.promise do
122
- reject 2
160
+ MrDarcy.promise do |promise|
161
+ promise.reject 2
123
162
  end.fail |value|
124
163
  value * value
125
164
  end.then |value|
@@ -130,8 +169,8 @@ doing async ruby:
130
169
  4. Failures cascade until they're caught:
131
170
 
132
171
  ```ruby
133
- MrDarcy.promise do
134
- reject :fail
172
+ MrDarcy.promise do |promise|
173
+ promise.reject :fail
135
174
  end.then
136
175
  # I am skipped
137
176
  end.then
@@ -145,11 +184,11 @@ doing async ruby:
145
184
  their resolution until the new promise is resolved:
146
185
 
147
186
  ```ruby
148
- MrDarcy.promise do
149
- resolve 1
187
+ MrDarcy.promise do |promise|
188
+ promise.resolve 1
150
189
  end.then do |value|
151
- MrDarcy.promise do
152
- resolve value * 2
190
+ MrDarcy.promise do |p|
191
+ p.resolve value * 2
153
192
  end
154
193
  end.then |value|
155
194
  # I will be called with 2
@@ -163,7 +202,7 @@ complete then you can use the `MrDarcy.all_promises` method:
163
202
 
164
203
  ```ruby
165
204
  MrDarcy.all_promises do
166
- 10.times.map { |i| MrDarcy.promise { sleep 1; resolve i } }
205
+ 10.times.map { |i| MrDarcy.promise { |p| sleep 1; p.resolve i } }
167
206
  end.then do |values|
168
207
  puts "All done."
169
208
  end
@@ -278,7 +317,7 @@ just work.
278
317
 
279
318
  ## Contributing
280
319
 
281
- 1. Fork it ( http://github.com/<my-github-username>/mr_darcy/fork )
320
+ 1. Fork it ( http://github.com/jamesotron/mr_darcy/fork )
282
321
  2. Create your feature branch (`git checkout -b my-new-feature`)
283
322
  3. Commit your changes (`git commit -am 'Add some feature'`)
284
323
  4. Push to the branch (`git push origin my-new-feature`)
@@ -129,8 +129,7 @@ module MrDarcy
129
129
  def evaluate_promise &block
130
130
  begin
131
131
  dsl = DSL.new(self)
132
- dsl.instance_exec dsl, &block
133
- # block.call DSL.new(self)
132
+ block.call DSL.new(self)
134
133
  rescue Exception => e
135
134
  reject e
136
135
  end
@@ -8,14 +8,12 @@ module MrDarcy
8
8
  include Enumerable
9
9
 
10
10
  def initialize promises, opts={}
11
- @lock = Mutex.new
12
- this = self
13
-
11
+ @lock = Mutex.new
14
12
  @promises = []
15
13
  @size = promises.size
16
14
  @promise = MrDarcy.promise opts do
17
15
  promises.each do |p|
18
- this.send :add_promise, p
16
+ add_promise p
19
17
  end
20
18
  end
21
19
  end
@@ -1,3 +1,3 @@
1
1
  module MrDarcy
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -5,9 +5,9 @@ describe MrDarcy::Promise::Collection do
5
5
  describe "with driver #{driver}" do
6
6
  next if driver == :synchronous
7
7
 
8
- let(:first_promise) { MrDarcy.promise(driver: driver) { resolve 1 } }
9
- let(:second_promise) { MrDarcy.promise(driver: driver) { resolve 2 } }
10
- let(:third_promise) { MrDarcy.promise(driver: driver) { resolve 3 } }
8
+ let(:first_promise) { MrDarcy.promise(driver: driver) { |p| p.resolve 1 } }
9
+ let(:second_promise) { MrDarcy.promise(driver: driver) { |p| p.resolve 2 } }
10
+ let(:third_promise) { MrDarcy.promise(driver: driver) { |p| p.resolve 3 } }
11
11
  let(:three_promises) { [ first_promise, second_promise, third_promise ] }
12
12
  let(:collection) { described_class.new three_promises, driver: driver }
13
13
  subject { collection }
@@ -21,12 +21,12 @@ describe MrDarcy::Promise::Collection do
21
21
  its(:final) { should respond_to :raise }
22
22
 
23
23
  When 'all the promises resolve' do
24
- its(:final) { should be_resolved }
25
24
  its(:result) { should eq [1,2,3] }
25
+ its(:final) { should be_resolved }
26
26
  end
27
27
 
28
28
  When 'any of the promises reject' do
29
- let(:second_promise) { MrDarcy.promise(driver: driver) { reject 2 } }
29
+ let(:second_promise) { MrDarcy.promise(driver: driver) { |p| p.reject 2 } }
30
30
  its(:final) { should be_rejected }
31
31
  its(:result) { should eq 2 }
32
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mr_darcy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Harton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-07 00:00:00.000000000 Z
11
+ date: 2014-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler