Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 864aa91
Showing
16 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
futuroscope |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'guard' | ||
gem 'guard-rspec' | ||
gem 'guard-bundler' | ||
|
||
# Specify your gem's dependencies in futuroscope.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# A sample Guardfile | ||
# More info at https://github.com/guard/guard#readme | ||
|
||
guard 'rspec' do | ||
watch(%r{^spec/.+_spec\.rb$}) | ||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
watch('spec/spec_helper.rb') { "spec" } | ||
end | ||
|
||
guard 'bundler' do | ||
watch('Gemfile') | ||
watch(/^.+\\.gemspec/) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2013 Josep Jaume Rey Peroy | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Futuroscope | ||
|
||
Futursocope is a simple library that implements futures in ruby. Futures are a | ||
concurrency pattern meant to help you deal with concurrency in a simple way. | ||
|
||
You can learn more about futures here in this excellent article from @jpignata: | ||
[Concurrency Patterns in Ruby: | ||
Futures](http://tx.pignata.com/2012/11/concurrency-patterns-in-ruby-futures.html) | ||
|
||
In Futuroscope, futures are instanciated with a simple ruby block. The future's | ||
execution will immediately start in a different thread and when you call a | ||
method on in it will be forwarded to the block's return value. | ||
|
||
If the thread didn't finish yet, it will block the program's execution until | ||
it's finished. Otherwise, it will immediataly return its value. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'futuroscope' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install futuroscope | ||
|
||
## Usage | ||
|
||
```Ruby | ||
require 'futuroscope' | ||
|
||
x = Futuroscope::Future.new{ sleep(1); 1 } | ||
y = Futuroscope::Future.new{ sleep(1); 2 } | ||
z = Futuroscope::Future.new{ sleep(1); 3 } | ||
|
||
# This execution will actually take just one second and not three like you | ||
# would expect. | ||
|
||
puts x + y + z | ||
=> 6 | ||
``` | ||
|
||
If you don't mind polluting the `Kernel` module, you can also require | ||
futuroscope's convenience `future` method: | ||
|
||
```Ruby | ||
require 'futuroscope/convenience' | ||
|
||
x = future{ sleep(1); 1 } | ||
y = future{ sleep(1); 2 } | ||
z = future{ sleep(1); 3 } | ||
|
||
puts x + y + z | ||
=> 6 | ||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require "bundler/gem_tasks" | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new do |t| | ||
t.pattern = "spec/**/*_spec.rb" | ||
end | ||
|
||
task :default => :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'futuroscope/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "futuroscope" | ||
spec.version = Futuroscope::VERSION | ||
spec.authors = ["Josep Jaume Rey Peroy"] | ||
spec.email = ["josepjaume@gmail.com"] | ||
spec.description = %q{TODO: Write a gem description} | ||
spec.summary = %q{TODO: Write a gem summary} | ||
spec.homepage = "" | ||
spec.license = "MIT" | ||
|
||
spec.files = `git ls-files`.split($/) | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.3" | ||
spec.add_development_dependency "rake" | ||
spec.add_development_dependency "rspec" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "futuroscope/version" | ||
require "futuroscope/future" | ||
|
||
module Futuroscope | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'futuroscope/future' | ||
|
||
module Kernel | ||
def future(&block) | ||
Futuroscope::Future.new(&block) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Futuroscope | ||
class Future | ||
attr_writer :__value | ||
|
||
# Initializes a future with a block and starts its execution. | ||
# | ||
# Examples: | ||
# | ||
# future = Futuroscope::Future.new { sleep(1); :edballs } | ||
# sleep(1) | ||
# future.value | ||
# => :edballs | ||
# # This will return in 1 second and not 2 if the execution wasn't | ||
# # deferred to a thread. | ||
# | ||
# block - A block that will be run in the background. | ||
# | ||
# Returns a Future | ||
def initialize(&block) | ||
@mutex = Mutex.new | ||
|
||
@thread = Thread.new do | ||
result = block.call | ||
self.__value = result | ||
end | ||
end | ||
|
||
# Semipublic: Returns the future's value. Will wait for the future to be | ||
# completed or return its value otherwise. Can be called multiple times. | ||
# | ||
# Returns the Future's block execution result. | ||
def __value | ||
@mutex.synchronize do | ||
return @__value if defined?(@__value) | ||
end | ||
@thread.join | ||
@__value | ||
end | ||
|
||
private | ||
|
||
def method_missing(method, *args) | ||
__value.send(method, *args) | ||
end | ||
|
||
def respond_to_missing?(method, include_private = false) | ||
__value.respond_to?(method, include_private) | ||
end | ||
|
||
def __value=(value) | ||
@mutex.synchronize do | ||
@__value = value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Futuroscope | ||
VERSION = "0.0.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'spec_helper' | ||
require 'futuroscope/convenience' | ||
require 'timeout' | ||
|
||
describe "Kernel#future" do | ||
it "adds a convenience method to ruby's kernel" do | ||
x = future{ sleep(1); 1 } | ||
y = future{ sleep(1); 2 } | ||
z = future{ sleep(1); 3 } | ||
|
||
Timeout::timeout(1.5) do | ||
expect(x + y + z).to eq(6) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'spec_helper' | ||
require 'futuroscope/future' | ||
require 'timeout' | ||
|
||
module Futuroscope | ||
describe Future do | ||
it "will return an instant value" do | ||
future = Future.new{ :edballs } | ||
sleep(0.1) | ||
|
||
expect(future.to_sym).to eq(:edballs) | ||
end | ||
|
||
it "will execute the future in the background and wait for it" do | ||
future = Future.new{ sleep(0.1); :edballs } | ||
|
||
Timeout::timeout(0.15) do | ||
sleep(0.1) | ||
expect(future.to_sym).to eq(:edballs) | ||
end | ||
end | ||
end | ||
end |
Empty file.