future.rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 062317c445eef79ce86d7ce5c8fbc2bfdd732c64
4
+ data.tar.gz: 596430e75506e3bf582244c2ca688b8fdc424846
5
+ SHA512:
6
+ metadata.gz: a4e35cf81995eb26961b3c44efcc1215539ee3a40f9c216105b75b94361c2497e98d576bf127f82a54e973fcda858fc81c51fadb65326dd777cfed20078ebdef
7
+ data.tar.gz: bf6cb1786651ff67e663ee83d3a14e1c778def0bbde4f9b1aa799476dc328bbfb42eaa87d940265b07f2ba76b0cb7470068c682c6079e26a2aefec8390ab4c0f
@@ -0,0 +1 @@
1
+ *.gem
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.1.0
5
+ - 2.0.0
6
+ - 1.9.3
7
+ - 1.9.2
8
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "minitest"
6
+ gem "rake"
@@ -0,0 +1,18 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ future.rb (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.3.3)
10
+ rake (10.3.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ future.rb!
17
+ minitest
18
+ rake
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Charlie Somerville and contributors.
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.
@@ -0,0 +1,72 @@
1
+ # future.rb ![](https://api.travis-ci.org/charliesome/future.rb.svg)
2
+
3
+ A simple concurrent [futures](https://en.wikipedia.org/wiki/Futures_and_promises) library for Ruby.
4
+
5
+ Futures are a simple construct that make it easier to write concurrent/asynchronous programs. As an example of their use, here's a dead simple concurrent web crawler using future.rb:
6
+
7
+ ```ruby
8
+ require "future"
9
+ require "open-uri"
10
+
11
+ def fetch_url(url)
12
+ Future { open(url).read }
13
+ end
14
+
15
+ def crawl_urls(urls)
16
+ Future.all(urls.map {
17
+ fetch_url(url)
18
+ })
19
+ end
20
+
21
+ results = crawl_urls([
22
+ "http://google.com/",
23
+ "http://twitter.com/",
24
+ "http://github.com/",
25
+ ]).value
26
+ ```
27
+
28
+ Notice that instead of returning values directly, the methods in the code above return `Future` instances. Because in-progress computation or I/O is represented as a value, we can apply the same reasoning to code dealing with futures as we can with code dealing with the values themselves.
29
+
30
+ ## API
31
+
32
+ future.rb's core API is very small and easy to understand.
33
+
34
+ `Future.new { code }` (or `Future { code }`) creates a new future object representing the value computed by the code in the block. This method returns immediately while the computation is performed asynchronously.
35
+
36
+ When the computation performed by the future blocks on I/O, future.rb automatically starts running other code (including other futures) while waiting for that I/O to complete. This approach to concurrency is quite similar to that used by node.js - but with none of the callback hell!
37
+
38
+ When your program needs the value of a `Future` instance, just call the `value` method. If the future has completed, this method will return immediately. If the future is still in the process of performing computation or I/O, future.rb will wait for it to complete before returning. The library cleverly makes use of this time spent waiting on futures to complete by running other code/futures in the meantime!
39
+
40
+ If a future has failed, `value` will raise the failing exception when called. This approach to error handling results in far less boilerplate than more manual approaches, such as 'errbacks' (error callbacks) or explicit error return values.
41
+
42
+ Finally, the `Future.all` helper method can be used to convert an array of futures into a future of an array of values. For example:
43
+
44
+ ```ruby
45
+ futures = [
46
+ Future { 123 },
47
+ Future { 456 },
48
+ Future { 789 },
49
+ ]
50
+
51
+ Future.all(futures).value # => [123, 456, 789]
52
+ ```
53
+
54
+ ## Installation
55
+
56
+ Add the following line to your `Gemfile`:
57
+
58
+ ```ruby
59
+ gem "future.rb"
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
69
+
70
+ ## License
71
+
72
+ [MIT](https://github.com/charliesome/future.rb/blob/master/LICENSE)
@@ -0,0 +1,5 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new "test"
4
+
5
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ $:.unshift File.expand_path("../lib", __FILE__)
2
+ require "future"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "future.rb"
6
+ s.version = Future::VERSION
7
+ s.summary = "A simple concurrent futures library for Ruby."
8
+ s.description = "A simple concurrent futures library for Ruby with the goal of making concurrent/asynchronous programming easier."
9
+ s.author = "Charlie Somerville"
10
+ s.email = "charlie@charliesomerville.com"
11
+ s.homepage = "https://github.com/charliesome/future.rb"
12
+ s.license = "MIT"
13
+ s.files = `git ls-files`.split("\n")
14
+ end
@@ -0,0 +1,11 @@
1
+ class Future < Thread
2
+ VERSION = "0.0.1"
3
+
4
+ def self.all(futures)
5
+ Future { futures.map(&:value) }
6
+ end
7
+ end
8
+
9
+ def Future(&bk)
10
+ Future.start(&bk)
11
+ end
@@ -0,0 +1,20 @@
1
+ require "minitest/autorun"
2
+ require "future"
3
+
4
+ class TestFuture < MiniTest::Test
5
+ def test_future
6
+ assert_kind_of Future, Future { 123 }
7
+ end
8
+
9
+ def test_value
10
+ assert_equal 123, Future { 123 }.value
11
+ end
12
+
13
+ def test_all
14
+ assert_equal [123, 456, 789], Future.all([
15
+ Future { 123 },
16
+ Future { 456 },
17
+ Future { 789 },
18
+ ]).value
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: future.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Charlie Somerville
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple concurrent futures library for Ruby with the goal of making
14
+ concurrent/asynchronous programming easier.
15
+ email: charlie@charliesomerville.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - future.rb.gemspec
28
+ - lib/future.rb
29
+ - test/test_future.rb
30
+ homepage: https://github.com/charliesome/future.rb
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.2.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: A simple concurrent futures library for Ruby.
54
+ test_files: []