q-defer 0.0.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +1 -0
- data/lib/q.rb +31 -0
- data/lib/q/callback.rb +13 -0
- data/lib/q/defer.rb +38 -0
- data/lib/q/promise.rb +42 -0
- data/lib/q/version.rb +3 -0
- data/q-defer.gemspec +23 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 591018934d9235e63107751fce115fcf0f391d7b
|
4
|
+
data.tar.gz: 61641c5feade2822853173ebef6ca70480157011
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49e33a9fceba7db6a0ed1770c65554a6c6feaabeb4fa104408f96bce1db6b6b0c30f61df3dbb49acdf6168d79f3d616cdf53a3e630173aab89965bcba0c86ddc
|
7
|
+
data.tar.gz: 3b0e041525373eebe55b6ceffa22cc7984561fa046c464ae839b7b6b45e76c62f09a4107db0e136f0c5a48377ed2c19ca2e10c041a5159760acfa70b1618c2b1
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jonathan Clem
|
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,80 @@
|
|
1
|
+
# Q
|
2
|
+
|
3
|
+
Q is a lightweight promise implementation in Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'q-defer', require: 'q'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install q-defer
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Defer a single block:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require "open-uri"
|
25
|
+
|
26
|
+
Q.defer do |defer|
|
27
|
+
Thread.new do
|
28
|
+
response = open("http://www.example.com")
|
29
|
+
|
30
|
+
if response.status[0] == "200"
|
31
|
+
defer.resolve(response)
|
32
|
+
else
|
33
|
+
defer.reject([response.status, response])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end.then do |result|
|
37
|
+
puts "ok"
|
38
|
+
end.fail do |err, result|
|
39
|
+
puts "status code: #{err[0]}"
|
40
|
+
end.always do
|
41
|
+
puts "All finished!"
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Defer multiple blocks:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
def defer_open(url)
|
49
|
+
Q.defer do |defer|
|
50
|
+
Thread.new do
|
51
|
+
response = open("http://www.example.com")
|
52
|
+
|
53
|
+
if response.status[0] == "200"
|
54
|
+
defer.resolve(response)
|
55
|
+
else
|
56
|
+
defer.reject([response.status, response])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
Q.when([
|
63
|
+
defer_open("http://www.example.com"),
|
64
|
+
defer_open("http://www.google.com")
|
65
|
+
]).then do |results|
|
66
|
+
puts "All successful!"
|
67
|
+
end.fail do |err, results|
|
68
|
+
puts "Error: #{err}"
|
69
|
+
end.always do |results|
|
70
|
+
puts "All successful or one failure."
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/q.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "q/version"
|
2
|
+
require "q/callback"
|
3
|
+
require "q/defer"
|
4
|
+
require "q/promise"
|
5
|
+
|
6
|
+
module Q
|
7
|
+
def self.defer(&block)
|
8
|
+
defer = Q::Defer.new
|
9
|
+
|
10
|
+
block.call(defer)
|
11
|
+
|
12
|
+
defer.promise
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.when(promises)
|
16
|
+
defer = Q::Defer.new
|
17
|
+
|
18
|
+
@results = []
|
19
|
+
|
20
|
+
promises.each do |promise|
|
21
|
+
promise.then do |result|
|
22
|
+
@results << result
|
23
|
+
defer.resolve(@results) if @results.length == promises.length
|
24
|
+
end.fail do |result|
|
25
|
+
defer.reject([result, @results])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
defer.promise
|
30
|
+
end
|
31
|
+
end
|
data/lib/q/callback.rb
ADDED
data/lib/q/defer.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class Q::Defer
|
2
|
+
attr_reader :promise, :state
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@promise = Q::Promise.new(self)
|
6
|
+
@state = :active
|
7
|
+
end
|
8
|
+
|
9
|
+
def resolve(result = nil)
|
10
|
+
unless finished?
|
11
|
+
@state = :resolved
|
12
|
+
@promise.fulfill(@state, result)
|
13
|
+
end
|
14
|
+
|
15
|
+
@promise
|
16
|
+
end
|
17
|
+
|
18
|
+
def reject(result = nil)
|
19
|
+
unless finished?
|
20
|
+
@state = :rejected
|
21
|
+
@promise.fulfill(@state, result)
|
22
|
+
end
|
23
|
+
|
24
|
+
@promise
|
25
|
+
end
|
26
|
+
|
27
|
+
def resolved?
|
28
|
+
@state == :resolved
|
29
|
+
end
|
30
|
+
|
31
|
+
def rejected?
|
32
|
+
@state == :rejected
|
33
|
+
end
|
34
|
+
|
35
|
+
def finished?
|
36
|
+
@state == :resolved || @state == :rejected
|
37
|
+
end
|
38
|
+
end
|
data/lib/q/promise.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class Q::Promise
|
2
|
+
def initialize(defer)
|
3
|
+
@defer = defer
|
4
|
+
|
5
|
+
@thens = []
|
6
|
+
@fails = []
|
7
|
+
@alwayses = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def then(&block)
|
11
|
+
enqueue_and_call(@thens, block, :resolved?)
|
12
|
+
end
|
13
|
+
|
14
|
+
def fail(&block)
|
15
|
+
enqueue_and_call(@fails, block, :rejected?)
|
16
|
+
end
|
17
|
+
|
18
|
+
def always(&block)
|
19
|
+
enqueue_and_call(@alwayses, block, :finished?)
|
20
|
+
end
|
21
|
+
|
22
|
+
def fulfill(type, result)
|
23
|
+
@result = result
|
24
|
+
|
25
|
+
if type == :resolved
|
26
|
+
@thens.each { |callback| callback.call(@result) }
|
27
|
+
elsif type == :rejected
|
28
|
+
@fails.each { |callback| callback.call(@result) }
|
29
|
+
end
|
30
|
+
|
31
|
+
@alwayses.each { |callback| callback.call(@result) }
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def enqueue_and_call(queue, block, call_condition)
|
37
|
+
callback = Q::Callback.new(self, block)
|
38
|
+
queue << callback
|
39
|
+
callback.call(@result) if @defer.send(call_condition)
|
40
|
+
self
|
41
|
+
end
|
42
|
+
end
|
data/lib/q/version.rb
ADDED
data/q-defer.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'q/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "q-defer"
|
8
|
+
spec.version = Q::VERSION
|
9
|
+
spec.authors = ["Jonathan Clem"]
|
10
|
+
spec.email = ["jonathan@jclem.net"]
|
11
|
+
spec.description = %q{Q is a lightweight deferred library for Ruby.}
|
12
|
+
spec.summary = %q{Q is a lightweight deferred library for Ruby.}
|
13
|
+
spec.homepage = "https://github.com/jclem/q-defer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: q-defer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Clem
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Q is a lightweight deferred library for Ruby.
|
42
|
+
email:
|
43
|
+
- jonathan@jclem.net
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/q.rb
|
54
|
+
- lib/q/callback.rb
|
55
|
+
- lib/q/defer.rb
|
56
|
+
- lib/q/promise.rb
|
57
|
+
- lib/q/version.rb
|
58
|
+
- q-defer.gemspec
|
59
|
+
homepage: https://github.com/jclem/q-defer
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>'
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.3.1
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Q is a lightweight deferred library for Ruby.
|
83
|
+
test_files: []
|