futuroscope 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ .rbx
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/README.md CHANGED
@@ -7,6 +7,9 @@
7
7
  Futursocope is a simple library that implements futures in ruby. Futures are a
8
8
  concurrency pattern meant to help you deal with concurrency in a simple way.
9
9
 
10
+ It's specially useful when working in Service Oriented Architectures where HTTP
11
+ calls can take a long time and you only expect a value from them.
12
+
10
13
  [![The awesome Futuroscope park](http://europe.eurostar.com/wp-content/uploads/2011/06/Futuroscope10-59-of-107.jpg)](http://futuroscope.com)
11
14
 
12
15
  You can learn more about futures here in this excellent article from @jpignata:
@@ -66,6 +69,10 @@ puts x + y + z
66
69
  => 6
67
70
  ```
68
71
 
72
+ ## Ideas for the future
73
+
74
+ * Having a thread pool so you can limit maximum concurrency.
75
+
69
76
  ## Contributing
70
77
 
71
78
  1. Fork it
@@ -1,7 +1,14 @@
1
1
  require 'futuroscope/future'
2
+ require 'futuroscope/map'
2
3
 
3
4
  module Kernel
4
5
  def future(&block)
5
6
  Futuroscope::Future.new(&block)
6
7
  end
7
8
  end
9
+
10
+ module Enumerable
11
+ def future_map(&block)
12
+ Futuroscope::Map.new(self).map(&block)
13
+ end
14
+ end
@@ -1,6 +1,12 @@
1
1
  module Futuroscope
2
+ # A Future is an object that gets initialized with a block and will behave
3
+ # exactly like the block's result, but being able to "borrow" its result from
4
+ # the future. That is, will block when the result is not ready until it is,
5
+ # and will return it instantly if the thread's execution already finished.
6
+ #
2
7
  class Future
3
8
  attr_writer :__value
9
+ extend Forwardable
4
10
 
5
11
  # Initializes a future with a block and starts its execution.
6
12
  #
@@ -37,6 +43,8 @@ module Futuroscope
37
43
  @__value
38
44
  end
39
45
 
46
+ def_delegators :__value, :to_s, :==
47
+
40
48
  private
41
49
 
42
50
  def method_missing(method, *args)
@@ -0,0 +1,39 @@
1
+ require 'futuroscope/future'
2
+
3
+ module Futuroscope
4
+ # A futuroscope map behaves like a regular map but performs all operations
5
+ # using futures so they're effectively parallel.
6
+ #
7
+ class Map
8
+ # Initializes a map with a set of items.
9
+ #
10
+ # items - Items in which to perform the mapping
11
+ #
12
+ def initialize(items)
13
+ @items = items
14
+ end
15
+
16
+ # Maps each item with a future.
17
+ #
18
+ # block - A block that will be executed passing each element as a parameter
19
+ #
20
+ # Returns an array of futures that behave like the original objects.
21
+ def map(&block)
22
+ @items.map do |item|
23
+ Future.new do
24
+ block.call(item)
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def method_missing(method, *args)
32
+ @map.send(method, *args)
33
+ end
34
+
35
+ def respond_to_missing?(method, include_private = false)
36
+ @map.respond_to?(method, include_private)
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module Futuroscope
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,8 +8,24 @@ describe "Kernel#future" do
8
8
  y = future{ sleep(1); 2 }
9
9
  z = future{ sleep(1); 3 }
10
10
 
11
- Timeout::timeout(1.5) do
11
+ Timeout::timeout(2.5) do
12
12
  expect(x + y + z).to eq(6)
13
13
  end
14
14
  end
15
15
  end
16
+
17
+ describe "Enumerable#future_map" do
18
+ it "adds a future_map method do Enumerable" do
19
+ items = [1, 2, 3]
20
+ map = items.future_map do |i|
21
+ sleep(1)
22
+ i + 1
23
+ end
24
+
25
+ Timeout::timeout(2.5) do
26
+ expect(map.first).to eq(2)
27
+ expect(map[1]).to eq(3)
28
+ expect(map.last).to eq(4)
29
+ end
30
+ end
31
+ end
@@ -8,15 +8,15 @@ module Futuroscope
8
8
  future = Future.new{ :edballs }
9
9
  sleep(0.1)
10
10
 
11
- expect(future.to_sym).to eq(:edballs)
11
+ expect(future).to eq(:edballs)
12
12
  end
13
13
 
14
14
  it "will execute the future in the background and wait for it" do
15
- future = Future.new{ sleep(0.1); :edballs }
15
+ future = Future.new{ sleep(1); :edballs }
16
16
 
17
- Timeout::timeout(0.15) do
18
- sleep(0.1)
19
- expect(future.to_sym).to eq(:edballs)
17
+ sleep(1)
18
+ Timeout::timeout(0.9) do
19
+ expect(future).to eq(:edballs)
20
20
  end
21
21
  end
22
22
  end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'futuroscope/map'
3
+
4
+ module Futuroscope
5
+ describe Map do
6
+ it "behaves like a normal map" do
7
+ items = [1, 2, 3]
8
+ result = Map.new(items).map do |item|
9
+ sleep(item)
10
+ "Item #{item}"
11
+ end
12
+
13
+ Timeout::timeout(4) do
14
+ expect(result.first).to eq("Item 1")
15
+ expect(result[1]).to eq("Item 2")
16
+ expect(result.last).to eq("Item 3")
17
+ end
18
+ end
19
+ end
20
+ end
metadata CHANGED
@@ -1,57 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: futuroscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Josep Jaume Rey Peroy
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-05-02 00:00:00.000000000 Z
12
+ date: 2013-05-03 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
+ version_requirements: !ruby/object:Gem::Requirement
16
17
  requirements:
17
18
  - - ~>
18
19
  - !ruby/object:Gem::Version
19
20
  version: '1.3'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
+ none: false
28
+ prerelease: false
29
+ type: :development
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rake
29
- requirement: !ruby/object:Gem::Requirement
32
+ version_requirements: !ruby/object:Gem::Requirement
30
33
  requirements:
31
- - - '>='
34
+ - - ! '>='
32
35
  - !ruby/object:Gem::Version
33
36
  version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
37
39
  requirements:
38
- - - '>='
40
+ - - ! '>='
39
41
  - !ruby/object:Gem::Version
40
42
  version: '0'
43
+ none: false
44
+ prerelease: false
45
+ type: :development
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rspec
43
- requirement: !ruby/object:Gem::Requirement
48
+ version_requirements: !ruby/object:Gem::Requirement
44
49
  requirements:
45
- - - '>='
50
+ - - ! '>='
46
51
  - !ruby/object:Gem::Version
47
52
  version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirement: !ruby/object:Gem::Requirement
51
55
  requirements:
52
- - - '>='
56
+ - - ! '>='
53
57
  - !ruby/object:Gem::Version
54
58
  version: '0'
59
+ none: false
60
+ prerelease: false
61
+ type: :development
55
62
  description: Futuroscope is yet another simple gem that implements the Futures concurrency
56
63
  pattern.
57
64
  email:
@@ -60,49 +67,84 @@ executables: []
60
67
  extensions: []
61
68
  extra_rdoc_files: []
62
69
  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
70
+ - !binary |-
71
+ LmdpdGlnbm9yZQ==
72
+ - !binary |-
73
+ LnJzcGVj
74
+ - !binary |-
75
+ LnJ1YnktZ2Vtc2V0
76
+ - !binary |-
77
+ LnJ1YnktdmVyc2lvbg==
78
+ - !binary |-
79
+ LnRyYXZpcy55bWw=
80
+ - !binary |-
81
+ R2VtZmlsZQ==
82
+ - !binary |-
83
+ R3VhcmRmaWxl
84
+ - !binary |-
85
+ TElDRU5TRS50eHQ=
86
+ - !binary |-
87
+ UkVBRE1FLm1k
88
+ - !binary |-
89
+ UmFrZWZpbGU=
90
+ - !binary |-
91
+ ZnV0dXJvc2NvcGUuZ2Vtc3BlYw==
92
+ - !binary |-
93
+ bGliL2Z1dHVyb3Njb3BlLnJi
94
+ - !binary |-
95
+ bGliL2Z1dHVyb3Njb3BlL2NvbnZlbmllbmNlLnJi
96
+ - !binary |-
97
+ bGliL2Z1dHVyb3Njb3BlL2Z1dHVyZS5yYg==
98
+ - !binary |-
99
+ bGliL2Z1dHVyb3Njb3BlL21hcC5yYg==
100
+ - !binary |-
101
+ bGliL2Z1dHVyb3Njb3BlL3ZlcnNpb24ucmI=
102
+ - !binary |-
103
+ c3BlYy9mdXR1cm9zY29wZS9jb252ZW5pZW5jZV9zcGVjLnJi
104
+ - !binary |-
105
+ c3BlYy9mdXR1cm9zY29wZS9mdXR1cmVfc3BlYy5yYg==
106
+ - !binary |-
107
+ c3BlYy9mdXR1cm9zY29wZS9tYXBfc3BlYy5yYg==
108
+ - !binary |-
109
+ c3BlYy9zcGVjX2hlbHBlci5yYg==
80
110
  homepage: ''
81
111
  licenses:
82
112
  - MIT
83
- metadata: {}
84
113
  post_install_message:
85
114
  rdoc_options: []
86
115
  require_paths:
87
116
  - lib
88
117
  required_ruby_version: !ruby/object:Gem::Requirement
89
118
  requirements:
90
- - - '>='
119
+ - - ! '>='
91
120
  - !ruby/object:Gem::Version
92
121
  version: '0'
122
+ segments:
123
+ - 0
124
+ hash: 2002549777813010636
125
+ none: false
93
126
  required_rubygems_version: !ruby/object:Gem::Requirement
94
127
  requirements:
95
- - - '>='
128
+ - - ! '>='
96
129
  - !ruby/object:Gem::Version
97
130
  version: '0'
131
+ segments:
132
+ - 0
133
+ hash: 2002549777813010636
134
+ none: false
98
135
  requirements: []
99
136
  rubyforge_project:
100
- rubygems_version: 2.0.2
137
+ rubygems_version: 1.8.25
101
138
  signing_key:
102
- specification_version: 4
139
+ specification_version: 3
103
140
  summary: Futuroscope is yet another simple gem that implements the Futures concurrency
104
141
  pattern.
105
142
  test_files:
106
- - spec/futuroscope/convenience_spec.rb
107
- - spec/futuroscope/future_spec.rb
108
- - spec/spec_helper.rb
143
+ - !binary |-
144
+ c3BlYy9mdXR1cm9zY29wZS9jb252ZW5pZW5jZV9zcGVjLnJi
145
+ - !binary |-
146
+ c3BlYy9mdXR1cm9zY29wZS9mdXR1cmVfc3BlYy5yYg==
147
+ - !binary |-
148
+ c3BlYy9mdXR1cm9zY29wZS9tYXBfc3BlYy5yYg==
149
+ - !binary |-
150
+ c3BlYy9zcGVjX2hlbHBlci5yYg==
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 702932e991c91726ce5ff65ff9a673261c227e6d
4
- data.tar.gz: ae1f97ea456347b722cbd7b41c3038a799a29f77
5
- SHA512:
6
- metadata.gz: 87cd6524f383af41f7ee3443115398cf3de5c8014a343bfcc33651d45535ff8947576cd2a098aba38011d53d5dca4f710e776ff0ae065400f8dc06f314ad4be8
7
- data.tar.gz: be014a3a3dfe04124b54ea31555ca12d75be30cf39a8ba10824d5569da88eb6655b018973f921451273e0ab8fa73c62df2c617a717cc31df309bd18409c8a10c