Narnach-future 0.1.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/MIT-LICENSE +20 -0
- data/README.rdoc +42 -0
- data/Rakefile +29 -0
- data/future.gemspec +33 -0
- data/lib/future.rb +33 -0
- data/spec/future_spec.rb +28 -0
- data/spec/spec_helper.rb +2 -0
- metadata +63 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Wes Oldenbeuving
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
= Future
|
2
|
+
|
3
|
+
Future implements the future concept from IO.
|
4
|
+
A future is an object that you create now, but which only gets a usable
|
5
|
+
value in the future. Think of asynchronous messaging and long-lasting method
|
6
|
+
calls.
|
7
|
+
|
8
|
+
An example:
|
9
|
+
include Future
|
10
|
+
f = future { sleep 5; 123}
|
11
|
+
puts f * 5
|
12
|
+
The future will take 5 seconds to run, then 123 is returned.
|
13
|
+
The call f.*(5) will block until the future is done.
|
14
|
+
|
15
|
+
Another example:
|
16
|
+
include Future
|
17
|
+
f1 = future { sleep 5; 10}
|
18
|
+
f2 = future { sleep 3; 20}
|
19
|
+
f3 = future { sleep 1; 30}
|
20
|
+
puts f3 + f2 + f1
|
21
|
+
Here it only takes 5 seconds to calculate the sum instead of the 9 it would take when done sequentially.
|
22
|
+
|
23
|
+
== Recent changes
|
24
|
+
|
25
|
+
=== Version 0.1.1
|
26
|
+
* Added specs
|
27
|
+
|
28
|
+
=== Version 0.1.0
|
29
|
+
* Created project
|
30
|
+
* Added first version of future.rb, wrapped in a Module
|
31
|
+
* Added documentation
|
32
|
+
|
33
|
+
== Installation
|
34
|
+
=== From git
|
35
|
+
From the project root, use rake to install.
|
36
|
+
git clone http://github.com/Narnach/future
|
37
|
+
cd future
|
38
|
+
rake install
|
39
|
+
This will build the gem and install it for you.
|
40
|
+
|
41
|
+
== About
|
42
|
+
future was created by Wes Oldenbeuving. It is licensed under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "rake"
|
2
|
+
require "rake/clean"
|
3
|
+
require "rake/gempackagetask"
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
################################################################################
|
7
|
+
### Gem
|
8
|
+
################################################################################
|
9
|
+
|
10
|
+
begin
|
11
|
+
# Parse gemspec using the github safety level.
|
12
|
+
data = File.read('future.gemspec')
|
13
|
+
spec = nil
|
14
|
+
Thread.new { spec = eval("$SAFE = 3
|
15
|
+
%s" % data)}.join
|
16
|
+
|
17
|
+
# Create the gem tasks
|
18
|
+
Rake::GemPackageTask.new(spec) do |package|
|
19
|
+
package.gem_spec = spec
|
20
|
+
end
|
21
|
+
rescue Exception => e
|
22
|
+
printf "WARNING: Error caught (%s): %s
|
23
|
+
", e.class.name, e.message
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Package and install the gem for the current version'
|
27
|
+
task :install => :gem do
|
28
|
+
system "sudo gem install -l pkg/%s-%s.gem" % [spec.name, spec.version]
|
29
|
+
end
|
data/future.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
# Project
|
3
|
+
s.name = 'future'
|
4
|
+
s.summary = "A naieve implementation of futures"
|
5
|
+
s.description = "A naieve implementation of futures"
|
6
|
+
s.version = '0.1.1'
|
7
|
+
s.date = '2008-12-08'
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Wes Oldenbeuving"]
|
10
|
+
s.email = "narnach@gmail.com"
|
11
|
+
s.homepage = "http://www.github.com/Narnach/future"
|
12
|
+
|
13
|
+
# Files
|
14
|
+
root_files = %w[MIT-LICENSE README.rdoc Rakefile future.gemspec]
|
15
|
+
bin_files = %w[]
|
16
|
+
lib_files = %w[future]
|
17
|
+
test_files = %w[]
|
18
|
+
spec_files = %w[future]
|
19
|
+
other_files = %w[spec/spec_helper.rb]
|
20
|
+
s.bindir = "bin"
|
21
|
+
s.require_path = "lib"
|
22
|
+
s.executables = bin_files
|
23
|
+
s.test_files = test_files.map {|f| 'test/%s_test.rb' % f} + spec_files.map {|f| 'spec/%s_spec.rb' % f}
|
24
|
+
s.files = root_files + bin_files.map {|f| 'bin/%s' % f} + lib_files.map {|f| 'lib/%s.rb' % f} + s.test_files + other_files
|
25
|
+
|
26
|
+
# rdoc
|
27
|
+
s.has_rdoc = true
|
28
|
+
s.extra_rdoc_files = %w[ README.rdoc MIT-LICENSE]
|
29
|
+
s.rdoc_options << '--inline-source' << '--line-numbers' << '--main' << 'README.rdoc'
|
30
|
+
|
31
|
+
# Requirements
|
32
|
+
s.required_ruby_version = ">= 1.8.0"
|
33
|
+
end
|
data/lib/future.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Future implements the future concept from IO.
|
2
|
+
# A future is an object that you create now, but which only gets a usable
|
3
|
+
# value in the future. Think of asynchronous messaging and long-lasting method
|
4
|
+
# calls.
|
5
|
+
#
|
6
|
+
# An example:
|
7
|
+
# include Future
|
8
|
+
# f = future { sleep 5; 123}
|
9
|
+
# puts f * 5 #=> 615
|
10
|
+
# The future will take 5 seconds to run, then 123 is returned.
|
11
|
+
# The call f.*(5) will block until the future is done.
|
12
|
+
#
|
13
|
+
# Another example:
|
14
|
+
# include Future
|
15
|
+
# f1 = future { sleep 5; 10}
|
16
|
+
# f2 = future { sleep 3; 20}
|
17
|
+
# f3 = future { sleep 1; 30}
|
18
|
+
# puts f3 + f2 + f1
|
19
|
+
# Here it only takes 5 seconds to calculate the sum instead of the 9 it would take when done sequentially.
|
20
|
+
module Future
|
21
|
+
def future(&block)
|
22
|
+
t = Thread.new(&block)
|
23
|
+
class << t
|
24
|
+
(instance_methods - %w[value __send__ __id__]).each do |meth|
|
25
|
+
eval("undef #{meth}")
|
26
|
+
end
|
27
|
+
def method_missing(*args, &block)
|
28
|
+
value.send(*args,&block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
t
|
32
|
+
end
|
33
|
+
end
|
data/spec/future_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
require 'future'
|
3
|
+
|
4
|
+
class TimeTraveller
|
5
|
+
include Future
|
6
|
+
end
|
7
|
+
|
8
|
+
describe Future, '#future' do
|
9
|
+
it 'should return the return value of the block' do
|
10
|
+
tt = TimeTraveller.new
|
11
|
+
f1 = tt.future { 10 }
|
12
|
+
f2 = tt.future { 20 }
|
13
|
+
f3 = tt.future { 30 }
|
14
|
+
(f3 + f2 + f1).should == 60
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should execute futures in parallel' do
|
18
|
+
tt = TimeTraveller.new
|
19
|
+
now = Time.now
|
20
|
+
f1 = tt.future { sleep 0.2; 10 }
|
21
|
+
f2 = tt.future { sleep 0.1; 20 }
|
22
|
+
f3 = tt.future { 30 }
|
23
|
+
f3+f2+f1
|
24
|
+
diff = Time.now - now
|
25
|
+
diff.should < 0.3
|
26
|
+
diff.should > 0.1
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Narnach-future
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wes Oldenbeuving
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-08 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A naieve implementation of futures
|
17
|
+
email: narnach@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- MIT-LICENSE
|
25
|
+
files:
|
26
|
+
- MIT-LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- future.gemspec
|
30
|
+
- lib/future.rb
|
31
|
+
- spec/future_spec.rb
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://www.github.com/Narnach/future
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --inline-source
|
38
|
+
- --line-numbers
|
39
|
+
- --main
|
40
|
+
- README.rdoc
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.8.0
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: A naieve implementation of futures
|
62
|
+
test_files:
|
63
|
+
- spec/future_spec.rb
|