Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
josepjaume committed May 2, 2013
0 parents commit 864aa91
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -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
1 change: 1 addition & 0 deletions .ruby-gemset
@@ -0,0 +1 @@
futuroscope
1 change: 1 addition & 0 deletions .ruby-version
@@ -0,0 +1 @@
2.0.0
8 changes: 8 additions & 0 deletions Gemfile
@@ -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
13 changes: 13 additions & 0 deletions Guardfile
@@ -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
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -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.
66 changes: 66 additions & 0 deletions README.md
@@ -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
8 changes: 8 additions & 0 deletions Rakefile
@@ -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
24 changes: 24 additions & 0 deletions futuroscope.gemspec
@@ -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
5 changes: 5 additions & 0 deletions lib/futuroscope.rb
@@ -0,0 +1,5 @@
require "futuroscope/version"
require "futuroscope/future"

module Futuroscope
end
7 changes: 7 additions & 0 deletions lib/futuroscope/convenience.rb
@@ -0,0 +1,7 @@
require 'futuroscope/future'

module Kernel
def future(&block)
Futuroscope::Future.new(&block)
end
end
56 changes: 56 additions & 0 deletions lib/futuroscope/future.rb
@@ -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
3 changes: 3 additions & 0 deletions lib/futuroscope/version.rb
@@ -0,0 +1,3 @@
module Futuroscope
VERSION = "0.0.1"
end
15 changes: 15 additions & 0 deletions spec/futuroscope/convenience_spec.rb
@@ -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
23 changes: 23 additions & 0 deletions spec/futuroscope/future_spec.rb
@@ -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 added spec/spec_helper.rb
Empty file.

0 comments on commit 864aa91

Please sign in to comment.