ruby-promises 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +1 -0
- data/examples/Gemfile +4 -0
- data/examples/error.rb +33 -0
- data/examples/examples.rb +8 -0
- data/examples/success.rb +39 -0
- data/lib/ruby-promises.rb +71 -0
- data/ruby-promises.gemspec +22 -0
- metadata +103 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Bachue Zhou
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# RubyPromises
|
2
|
+
|
3
|
+
A lightweight gem implement AngularJS Promises in Ruby
|
4
|
+
|
5
|
+
AngularJS Promise introduction: <http://urish.org/angular/AngularPromises.pdf>
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'ruby-promises'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ruby-promises
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
EventMachine.run do
|
25
|
+
promise = Promise.new
|
26
|
+
promise.then {|url|
|
27
|
+
http = EventMachine::HttpRequest.new(url).get
|
28
|
+
http.callback {
|
29
|
+
p http.response_header
|
30
|
+
resolve 'http://www.ruby-china.org'
|
31
|
+
}
|
32
|
+
}.then {|url|
|
33
|
+
http = EventMachine::HttpRequest.new(url).get
|
34
|
+
http.callback {
|
35
|
+
p http.response_header
|
36
|
+
resolve 'http://www.vmware.com'
|
37
|
+
}
|
38
|
+
}.then {|url|
|
39
|
+
http = EventMachine::HttpRequest.new(url).get
|
40
|
+
http.callback {
|
41
|
+
p http.response_header
|
42
|
+
resolve 'http://www.emc.com'
|
43
|
+
}
|
44
|
+
}.then {
|
45
|
+
http = EventMachine::HttpRequest.new(url).get
|
46
|
+
http.callback {
|
47
|
+
p http.response_header
|
48
|
+
resolve
|
49
|
+
puts 'Success Here! Shutdown the EventMachine!'
|
50
|
+
EM.stop
|
51
|
+
}
|
52
|
+
}.resolve 'http://www.google.com'
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/examples/Gemfile
ADDED
data/examples/error.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'examples'
|
3
|
+
|
4
|
+
EventMachine.run do
|
5
|
+
promise = Promise.new
|
6
|
+
promise.then(proc {|url|
|
7
|
+
http = EventMachine::HttpRequest.new(url).get
|
8
|
+
http.callback {
|
9
|
+
p http.response_header
|
10
|
+
resolve 'http://www.ruby-china.org'
|
11
|
+
}
|
12
|
+
}, proc {|err|
|
13
|
+
EM.stop
|
14
|
+
puts 'Succeed to stop EventMachine!'
|
15
|
+
}).then {|url|
|
16
|
+
http = EventMachine::HttpRequest.new(url).get
|
17
|
+
http.callback {
|
18
|
+
p http.response_header
|
19
|
+
resolve 'http://www.baidu.com'
|
20
|
+
}
|
21
|
+
}.then(proc {|url|
|
22
|
+
http = EventMachine::HttpRequest.new(url).get
|
23
|
+
http.callback {
|
24
|
+
p http.response_header
|
25
|
+
reject 'Success!'
|
26
|
+
}
|
27
|
+
}, proc {|err| puts "Get Error here: #{err}" }).then {
|
28
|
+
EM.stop
|
29
|
+
puts 'Fail!'
|
30
|
+
}.resolve 'http://www.google.com'
|
31
|
+
|
32
|
+
puts '## END ##'
|
33
|
+
end
|
data/examples/success.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'examples'
|
3
|
+
|
4
|
+
EventMachine.run do
|
5
|
+
promise = Promise.new
|
6
|
+
promise.then {|url|
|
7
|
+
http = EventMachine::HttpRequest.new(url).get
|
8
|
+
http.callback {
|
9
|
+
p http.response_header
|
10
|
+
resolve 'http://www.ruby-china.org'
|
11
|
+
}
|
12
|
+
}.then {|url|
|
13
|
+
http = EventMachine::HttpRequest.new(url).get
|
14
|
+
http.callback {
|
15
|
+
p http.response_header
|
16
|
+
resolve 'http://www.vmware.com'
|
17
|
+
}
|
18
|
+
}.then {|url|
|
19
|
+
http = EventMachine::HttpRequest.new(url).get
|
20
|
+
http.callback {
|
21
|
+
p http.response_header
|
22
|
+
resolve 'http://www.emc.com'
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
promise.resolve 'http://www.google.com'
|
27
|
+
|
28
|
+
promise.then {|url|
|
29
|
+
http = EventMachine::HttpRequest.new(url).get
|
30
|
+
http.callback {
|
31
|
+
p http.response_header
|
32
|
+
resolve
|
33
|
+
EM.stop
|
34
|
+
puts 'Success Here! Shutdown the EventMachine!'
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
puts '## END ##'
|
39
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module RubyPromises
|
2
|
+
class CleanObject
|
3
|
+
instance_methods.each { |m| undef_method m unless m.to_s =~ /^(?:nil\?|send|object_id)$|^__|^respond_to|^instance_/ }
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class Promise < RubyPromises::CleanObject
|
8
|
+
class ThenEnv < RubyPromises::CleanObject
|
9
|
+
def initialize success, error
|
10
|
+
@success, @error, @started = success, error, false
|
11
|
+
end
|
12
|
+
|
13
|
+
def resolve *r
|
14
|
+
@success.call(*r)
|
15
|
+
end
|
16
|
+
|
17
|
+
def reject *r
|
18
|
+
@error.call(*r)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@success_chain = []
|
24
|
+
@error_chain = []
|
25
|
+
end
|
26
|
+
|
27
|
+
def then success = nil, error = nil, &blk
|
28
|
+
if blk
|
29
|
+
if success.nil?
|
30
|
+
success = blk
|
31
|
+
elsif error.nil?
|
32
|
+
error = blk
|
33
|
+
end
|
34
|
+
end
|
35
|
+
raise ArgumentError.new('the first arg should be callable') unless success.respond_to?(:call)
|
36
|
+
raise ArgumentError.new('the second arg should be callable') unless !error || error.respond_to?(:call)
|
37
|
+
@success_chain << [success, error]
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def resolve arg = nil
|
42
|
+
_rescue 'can\'t call result twice!' if @started
|
43
|
+
@started = true
|
44
|
+
_apply arg
|
45
|
+
end
|
46
|
+
|
47
|
+
def method_missing method, *args
|
48
|
+
result.send method, *args
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
def _apply result = nil
|
53
|
+
@result = result
|
54
|
+
unless @success_chain.empty?
|
55
|
+
current = @success_chain.shift
|
56
|
+
@error_chain.unshift current
|
57
|
+
ThenEnv.new(proc {|*r| _apply(*r) }, proc{|*r| _rescue(*r)}).
|
58
|
+
instance_exec(result, ¤t[0])
|
59
|
+
end
|
60
|
+
@result
|
61
|
+
end
|
62
|
+
|
63
|
+
def _rescue err
|
64
|
+
solved = false
|
65
|
+
@error_chain.map {|_, error| error }.compact.each {|error|
|
66
|
+
error.call err
|
67
|
+
solved = true
|
68
|
+
}
|
69
|
+
raise err unless solved
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = 'ruby-promises'
|
3
|
+
spec.version = '0.0.1'
|
4
|
+
spec.authors = ['Bachue Zhou']
|
5
|
+
spec.email = ['bachue.shu@gmail.com']
|
6
|
+
spec.description = <<-DESC
|
7
|
+
A lightweight gem implement AngularJS Promises in Ruby
|
8
|
+
|
9
|
+
AngularJS Promise introduction: http://urish.org/angular/AngularPromises.pdf
|
10
|
+
DESC
|
11
|
+
spec.summary = spec.description
|
12
|
+
spec.homepage = 'http://bachue.is-programmer.com'
|
13
|
+
spec.license = 'GPLv3'
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-promises
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Bachue Zhou
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2013-10-21 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ~>
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
segments:
|
26
|
+
- 1
|
27
|
+
- 3
|
28
|
+
version: "1.3"
|
29
|
+
requirement: *id001
|
30
|
+
prerelease: false
|
31
|
+
name: bundler
|
32
|
+
type: :development
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
segments:
|
39
|
+
- 0
|
40
|
+
version: "0"
|
41
|
+
requirement: *id002
|
42
|
+
prerelease: false
|
43
|
+
name: rake
|
44
|
+
type: :development
|
45
|
+
description: |
|
46
|
+
A lightweight gem implement AngularJS Promises in Ruby
|
47
|
+
|
48
|
+
AngularJS Promise introduction: http://urish.org/angular/AngularPromises.pdf
|
49
|
+
|
50
|
+
email:
|
51
|
+
- bachue.shu@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- Gemfile.lock
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- examples/Gemfile
|
66
|
+
- examples/Gemfile.lock
|
67
|
+
- examples/error.rb
|
68
|
+
- examples/examples.rb
|
69
|
+
- examples/success.rb
|
70
|
+
- lib/ruby-promises.rb
|
71
|
+
- ruby-promises.gemspec
|
72
|
+
has_rdoc: true
|
73
|
+
homepage: http://bachue.is-programmer.com
|
74
|
+
licenses:
|
75
|
+
- GPLv3
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.3.6
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: "A lightweight gem implement AngularJS Promises in Ruby AngularJS Promise introduction: http://urish.org/angular/AngularPromises.pdf"
|
102
|
+
test_files: []
|
103
|
+
|