futuroscope 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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +10 -0
- data/Guardfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +8 -0
- data/futuroscope.gemspec +24 -0
- data/lib/futuroscope.rb +5 -0
- data/lib/futuroscope/convenience.rb +7 -0
- data/lib/futuroscope/future.rb +56 -0
- data/lib/futuroscope/version.rb +3 -0
- data/spec/futuroscope/convenience_spec.rb +15 -0
- data/spec/futuroscope/future_spec.rb +23 -0
- data/spec/spec_helper.rb +4 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 702932e991c91726ce5ff65ff9a673261c227e6d
|
4
|
+
data.tar.gz: ae1f97ea456347b722cbd7b41c3038a799a29f77
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 87cd6524f383af41f7ee3443115398cf3de5c8014a343bfcc33651d45535ff8947576cd2a098aba38011d53d5dca4f710e776ff0ae065400f8dc06f314ad4be8
|
7
|
+
data.tar.gz: be014a3a3dfe04124b54ea31555ca12d75be30cf39a8ba10824d5569da88eb6655b018973f921451273e0ab8fa73c62df2c617a717cc31df309bd18409c8a10c
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
futuroscope
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec' do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
9
|
+
|
10
|
+
guard 'bundler' do
|
11
|
+
watch('Gemfile')
|
12
|
+
watch(/^.+\\.gemspec/)
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Josep Jaume Rey Peroy
|
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,75 @@
|
|
1
|
+
# Futuroscope
|
2
|
+
[](http://badge.fury.io/rb/futuroscope)
|
3
|
+
[](https://travis-ci.org/codegram/futuroscope)
|
4
|
+
[](https://gemnasium.com/codegram/futuroscope)
|
5
|
+
[](https://coveralls.io/r/codegram/futuroscope)
|
6
|
+
|
7
|
+
Futursocope is a simple library that implements futures in ruby. Futures are a
|
8
|
+
concurrency pattern meant to help you deal with concurrency in a simple way.
|
9
|
+
|
10
|
+
[](http://futuroscope.com)
|
11
|
+
|
12
|
+
You can learn more about futures here in this excellent article from @jpignata:
|
13
|
+
[Concurrency Patterns in Ruby:
|
14
|
+
Futures](http://tx.pignata.com/2012/11/concurrency-patterns-in-ruby-futures.html)
|
15
|
+
|
16
|
+
In Futuroscope, futures are instanciated with a simple ruby block. The future's
|
17
|
+
execution will immediately start in a different thread and when you call a
|
18
|
+
method on in it will be forwarded to the block's return value.
|
19
|
+
|
20
|
+
If the thread didn't finish yet, it will block the program's execution until
|
21
|
+
it's finished. Otherwise, it will immediataly return its value.
|
22
|
+
|
23
|
+
Futuroscope is tested on `MRI 1.9.3`, `MRI 2.0.0`, `JRuby` and `Rubinius`.
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
gem 'futuroscope'
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install futuroscope
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
```Ruby
|
42
|
+
require 'futuroscope'
|
43
|
+
|
44
|
+
x = Futuroscope::Future.new{ sleep(1); 1 }
|
45
|
+
y = Futuroscope::Future.new{ sleep(1); 2 }
|
46
|
+
z = Futuroscope::Future.new{ sleep(1); 3 }
|
47
|
+
|
48
|
+
# This execution will actually take just one second and not three like you
|
49
|
+
# would expect.
|
50
|
+
|
51
|
+
puts x + y + z
|
52
|
+
=> 6
|
53
|
+
```
|
54
|
+
|
55
|
+
If you don't mind polluting the `Kernel` module, you can also require
|
56
|
+
futuroscope's convenience `future` method:
|
57
|
+
|
58
|
+
```Ruby
|
59
|
+
require 'futuroscope/convenience'
|
60
|
+
|
61
|
+
x = future{ sleep(1); 1 }
|
62
|
+
y = future{ sleep(1); 2 }
|
63
|
+
z = future{ sleep(1); 3 }
|
64
|
+
|
65
|
+
puts x + y + z
|
66
|
+
=> 6
|
67
|
+
```
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/futuroscope.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'futuroscope/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "futuroscope"
|
8
|
+
spec.version = Futuroscope::VERSION
|
9
|
+
spec.authors = ["Josep Jaume Rey Peroy"]
|
10
|
+
spec.email = ["josepjaume@gmail.com"]
|
11
|
+
spec.description = %q{Futuroscope is yet another simple gem that implements the Futures concurrency pattern.}
|
12
|
+
spec.summary = %q{Futuroscope is yet another simple gem that implements the Futures concurrency pattern.}
|
13
|
+
spec.homepage = ""
|
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
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/futuroscope.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module Futuroscope
|
2
|
+
class Future
|
3
|
+
attr_writer :__value
|
4
|
+
|
5
|
+
# Initializes a future with a block and starts its execution.
|
6
|
+
#
|
7
|
+
# Examples:
|
8
|
+
#
|
9
|
+
# future = Futuroscope::Future.new { sleep(1); :edballs }
|
10
|
+
# sleep(1)
|
11
|
+
# future.value
|
12
|
+
# => :edballs
|
13
|
+
# # This will return in 1 second and not 2 if the execution wasn't
|
14
|
+
# # deferred to a thread.
|
15
|
+
#
|
16
|
+
# block - A block that will be run in the background.
|
17
|
+
#
|
18
|
+
# Returns a Future
|
19
|
+
def initialize(&block)
|
20
|
+
@mutex = Mutex.new
|
21
|
+
|
22
|
+
@thread = Thread.new do
|
23
|
+
result = block.call
|
24
|
+
self.__value = result
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Semipublic: Returns the future's value. Will wait for the future to be
|
29
|
+
# completed or return its value otherwise. Can be called multiple times.
|
30
|
+
#
|
31
|
+
# Returns the Future's block execution result.
|
32
|
+
def __value
|
33
|
+
@mutex.synchronize do
|
34
|
+
return @__value if defined?(@__value)
|
35
|
+
end
|
36
|
+
@thread.join
|
37
|
+
@__value
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def method_missing(method, *args)
|
43
|
+
__value.send(method, *args)
|
44
|
+
end
|
45
|
+
|
46
|
+
def respond_to_missing?(method, include_private = false)
|
47
|
+
__value.respond_to?(method, include_private)
|
48
|
+
end
|
49
|
+
|
50
|
+
def __value=(value)
|
51
|
+
@mutex.synchronize do
|
52
|
+
@__value = value
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'futuroscope/convenience'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
describe "Kernel#future" do
|
6
|
+
it "adds a convenience method to ruby's kernel" do
|
7
|
+
x = future{ sleep(1); 1 }
|
8
|
+
y = future{ sleep(1); 2 }
|
9
|
+
z = future{ sleep(1); 3 }
|
10
|
+
|
11
|
+
Timeout::timeout(1.5) do
|
12
|
+
expect(x + y + z).to eq(6)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'futuroscope/future'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
module Futuroscope
|
6
|
+
describe Future do
|
7
|
+
it "will return an instant value" do
|
8
|
+
future = Future.new{ :edballs }
|
9
|
+
sleep(0.1)
|
10
|
+
|
11
|
+
expect(future.to_sym).to eq(:edballs)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "will execute the future in the background and wait for it" do
|
15
|
+
future = Future.new{ sleep(0.1); :edballs }
|
16
|
+
|
17
|
+
Timeout::timeout(0.15) do
|
18
|
+
sleep(0.1)
|
19
|
+
expect(future.to_sym).to eq(:edballs)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: futuroscope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josep Jaume Rey Peroy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-02 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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Futuroscope is yet another simple gem that implements the Futures concurrency
|
56
|
+
pattern.
|
57
|
+
email:
|
58
|
+
- josepjaume@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- .ruby-gemset
|
65
|
+
- .ruby-version
|
66
|
+
- .travis.yml
|
67
|
+
- Gemfile
|
68
|
+
- Guardfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- futuroscope.gemspec
|
73
|
+
- lib/futuroscope.rb
|
74
|
+
- lib/futuroscope/convenience.rb
|
75
|
+
- lib/futuroscope/future.rb
|
76
|
+
- lib/futuroscope/version.rb
|
77
|
+
- spec/futuroscope/convenience_spec.rb
|
78
|
+
- spec/futuroscope/future_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
homepage: ''
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.0.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Futuroscope is yet another simple gem that implements the Futures concurrency
|
104
|
+
pattern.
|
105
|
+
test_files:
|
106
|
+
- spec/futuroscope/convenience_spec.rb
|
107
|
+
- spec/futuroscope/future_spec.rb
|
108
|
+
- spec/spec_helper.rb
|