futuroscope 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +48 -0
- data/lib/futuroscope/future.rb +10 -10
- data/lib/futuroscope/map.rb +0 -10
- data/lib/futuroscope/version.rb +1 -1
- data/spec/futuroscope/future_spec.rb +11 -0
- metadata +46 -84
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 44424db4bac133c4e8e4009403b02d0ecc750956
|
4
|
+
data.tar.gz: e38c304ced43c047b82c4f9c24d414fb8c5813d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 370dd490ff6939491f44c5860caa2c48be493650c3e2ec56d6fed74194da915e20b36e53d8025141ba695e005b102328047c1e259b72c9bd5912ffc6e8b6f98a
|
7
|
+
data.tar.gz: 3e4e2723daaa6c0e088fc71da56149472ced8d4b08220e7ec3acf3bfa2e8e4c870ef32bd1eae6b9a42e7476991cf6fb0156bf1ef8871bef66b6fabeadc9a31a9
|
data/README.md
CHANGED
@@ -41,6 +41,7 @@ Or install it yourself as:
|
|
41
41
|
|
42
42
|
## Usage
|
43
43
|
|
44
|
+
### Simple futures
|
44
45
|
```Ruby
|
45
46
|
require 'futuroscope'
|
46
47
|
|
@@ -55,6 +56,29 @@ puts x + y + z
|
|
55
56
|
=> 6
|
56
57
|
```
|
57
58
|
|
59
|
+
### Future map
|
60
|
+
```Ruby
|
61
|
+
require 'futuroscope'
|
62
|
+
|
63
|
+
map = Futuroscope::Map.new([1, 2, 3]).map do |i|
|
64
|
+
sleep(1)
|
65
|
+
i + 1
|
66
|
+
end
|
67
|
+
|
68
|
+
puts map.first
|
69
|
+
=> 2
|
70
|
+
|
71
|
+
puts map[1]
|
72
|
+
=> 3
|
73
|
+
|
74
|
+
puts map.last
|
75
|
+
=> 4
|
76
|
+
|
77
|
+
# This action will actually only take 1 second.
|
78
|
+
```
|
79
|
+
|
80
|
+
### Convenience methods
|
81
|
+
|
58
82
|
If you don't mind polluting the `Kernel` module, you can also require
|
59
83
|
futuroscope's convenience `future` method:
|
60
84
|
|
@@ -69,6 +93,30 @@ puts x + y + z
|
|
69
93
|
=> 6
|
70
94
|
```
|
71
95
|
|
96
|
+
Same for a map:
|
97
|
+
|
98
|
+
```Ruby
|
99
|
+
require 'futuroscope/convenience'
|
100
|
+
|
101
|
+
items = [1, 2, 3].future_map do |i|
|
102
|
+
sleep(i)
|
103
|
+
i + 1
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
## Considerations
|
108
|
+
|
109
|
+
You should never add **side-effects** to a future. They have to be thought of
|
110
|
+
like they were a local variable, with the only outcome that they're returning a
|
111
|
+
value.
|
112
|
+
|
113
|
+
You have to take into account that they really run in a different thread, so
|
114
|
+
you'll be potentially accessing code in parallel that could not be threadsafe.
|
115
|
+
|
116
|
+
If you're looking for other ways to improve your code performance via
|
117
|
+
concurrency, you should probably deal directly with [Ruby's
|
118
|
+
threads](http://ruby-doc.org/core-2.0/Thread.html).
|
119
|
+
|
72
120
|
## Ideas for the future
|
73
121
|
|
74
122
|
* Having a thread pool so you can limit maximum concurrency.
|
data/lib/futuroscope/future.rb
CHANGED
@@ -5,7 +5,7 @@ module Futuroscope
|
|
5
5
|
# and will return it instantly if the thread's execution already finished.
|
6
6
|
#
|
7
7
|
class Future
|
8
|
-
attr_writer :
|
8
|
+
attr_writer :future_value
|
9
9
|
extend Forwardable
|
10
10
|
|
11
11
|
# Initializes a future with a block and starts its execution.
|
@@ -27,7 +27,7 @@ module Futuroscope
|
|
27
27
|
|
28
28
|
@thread = Thread.new do
|
29
29
|
result = block.call
|
30
|
-
self.
|
30
|
+
self.future_value = result
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -35,29 +35,29 @@ module Futuroscope
|
|
35
35
|
# completed or return its value otherwise. Can be called multiple times.
|
36
36
|
#
|
37
37
|
# Returns the Future's block execution result.
|
38
|
-
def
|
38
|
+
def future_value
|
39
39
|
@mutex.synchronize do
|
40
|
-
return @
|
40
|
+
return @future_value if defined?(@future_value)
|
41
41
|
end
|
42
42
|
@thread.join
|
43
|
-
@
|
43
|
+
@future_value
|
44
44
|
end
|
45
45
|
|
46
|
-
def_delegators :
|
46
|
+
def_delegators :future_value, :to_s, :==, :kind_of?, :is_a?, :clone, :class
|
47
47
|
|
48
48
|
private
|
49
49
|
|
50
50
|
def method_missing(method, *args)
|
51
|
-
|
51
|
+
future_value.send(method, *args)
|
52
52
|
end
|
53
53
|
|
54
54
|
def respond_to_missing?(method, include_private = false)
|
55
|
-
|
55
|
+
future_value.respond_to?(method, include_private)
|
56
56
|
end
|
57
57
|
|
58
|
-
def
|
58
|
+
def future_value=(value)
|
59
59
|
@mutex.synchronize do
|
60
|
-
@
|
60
|
+
@future_value = value
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
data/lib/futuroscope/map.rb
CHANGED
@@ -25,15 +25,5 @@ module Futuroscope
|
|
25
25
|
end
|
26
26
|
end
|
27
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
28
|
end
|
39
29
|
end
|
data/lib/futuroscope/version.rb
CHANGED
@@ -19,5 +19,16 @@ module Futuroscope
|
|
19
19
|
expect(future).to eq(:edballs)
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
it "delegates some important methods to the original object's" do
|
24
|
+
object = [1, 2, 3]
|
25
|
+
future = Future.new{object}
|
26
|
+
|
27
|
+
expect(future.class).to eq(Array)
|
28
|
+
expect(future).to be_kind_of(Enumerable)
|
29
|
+
expect(future).to be_a(Enumerable)
|
30
|
+
expect(future.clone).to eq(object)
|
31
|
+
expect(future.to_s).to eq(object.to_s)
|
32
|
+
end
|
22
33
|
end
|
23
34
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: futuroscope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.3
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Josep Jaume Rey Peroy
|
@@ -13,52 +12,46 @@ date: 2013-05-03 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
|
-
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
17
16
|
requirements:
|
18
17
|
- - ~>
|
19
18
|
- !ruby/object:Gem::Version
|
20
19
|
version: '1.3'
|
21
|
-
|
22
|
-
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !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
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
|
-
version_requirements: !ruby/object:Gem::Requirement
|
33
|
-
requirements:
|
34
|
-
- - ! '>='
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: '0'
|
37
|
-
none: false
|
38
29
|
requirement: !ruby/object:Gem::Requirement
|
39
30
|
requirements:
|
40
|
-
- -
|
31
|
+
- - '>='
|
41
32
|
- !ruby/object:Gem::Version
|
42
33
|
version: '0'
|
43
|
-
none: false
|
44
|
-
prerelease: false
|
45
34
|
type: :development
|
46
|
-
|
47
|
-
name: rspec
|
35
|
+
prerelease: false
|
48
36
|
version_requirements: !ruby/object:Gem::Requirement
|
49
37
|
requirements:
|
50
|
-
- -
|
38
|
+
- - '>='
|
51
39
|
- !ruby/object:Gem::Version
|
52
40
|
version: '0'
|
53
|
-
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
54
43
|
requirement: !ruby/object:Gem::Requirement
|
55
44
|
requirements:
|
56
|
-
- -
|
45
|
+
- - '>='
|
57
46
|
- !ruby/object:Gem::Version
|
58
47
|
version: '0'
|
59
|
-
none: false
|
60
|
-
prerelease: false
|
61
48
|
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
62
55
|
description: Futuroscope is yet another simple gem that implements the Futures concurrency
|
63
56
|
pattern.
|
64
57
|
email:
|
@@ -67,84 +60,53 @@ executables: []
|
|
67
60
|
extensions: []
|
68
61
|
extra_rdoc_files: []
|
69
62
|
files:
|
70
|
-
-
|
71
|
-
|
72
|
-
-
|
73
|
-
|
74
|
-
-
|
75
|
-
|
76
|
-
-
|
77
|
-
|
78
|
-
-
|
79
|
-
|
80
|
-
-
|
81
|
-
|
82
|
-
-
|
83
|
-
|
84
|
-
-
|
85
|
-
|
86
|
-
-
|
87
|
-
|
88
|
-
-
|
89
|
-
|
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==
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
65
|
+
- .ruby-gemset
|
66
|
+
- .ruby-version
|
67
|
+
- .travis.yml
|
68
|
+
- Gemfile
|
69
|
+
- Guardfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- futuroscope.gemspec
|
74
|
+
- lib/futuroscope.rb
|
75
|
+
- lib/futuroscope/convenience.rb
|
76
|
+
- lib/futuroscope/future.rb
|
77
|
+
- lib/futuroscope/map.rb
|
78
|
+
- lib/futuroscope/version.rb
|
79
|
+
- spec/futuroscope/convenience_spec.rb
|
80
|
+
- spec/futuroscope/future_spec.rb
|
81
|
+
- spec/futuroscope/map_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
110
83
|
homepage: ''
|
111
84
|
licenses:
|
112
85
|
- MIT
|
86
|
+
metadata: {}
|
113
87
|
post_install_message:
|
114
88
|
rdoc_options: []
|
115
89
|
require_paths:
|
116
90
|
- lib
|
117
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
118
92
|
requirements:
|
119
|
-
- -
|
93
|
+
- - '>='
|
120
94
|
- !ruby/object:Gem::Version
|
121
95
|
version: '0'
|
122
|
-
segments:
|
123
|
-
- 0
|
124
|
-
hash: 2002549777813010636
|
125
|
-
none: false
|
126
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
97
|
requirements:
|
128
|
-
- -
|
98
|
+
- - '>='
|
129
99
|
- !ruby/object:Gem::Version
|
130
100
|
version: '0'
|
131
|
-
segments:
|
132
|
-
- 0
|
133
|
-
hash: 2002549777813010636
|
134
|
-
none: false
|
135
101
|
requirements: []
|
136
102
|
rubyforge_project:
|
137
|
-
rubygems_version:
|
103
|
+
rubygems_version: 2.0.2
|
138
104
|
signing_key:
|
139
|
-
specification_version:
|
105
|
+
specification_version: 4
|
140
106
|
summary: Futuroscope is yet another simple gem that implements the Futures concurrency
|
141
107
|
pattern.
|
142
108
|
test_files:
|
143
|
-
-
|
144
|
-
|
145
|
-
-
|
146
|
-
|
147
|
-
- !binary |-
|
148
|
-
c3BlYy9mdXR1cm9zY29wZS9tYXBfc3BlYy5yYg==
|
149
|
-
- !binary |-
|
150
|
-
c3BlYy9zcGVjX2hlbHBlci5yYg==
|
109
|
+
- spec/futuroscope/convenience_spec.rb
|
110
|
+
- spec/futuroscope/future_spec.rb
|
111
|
+
- spec/futuroscope/map_spec.rb
|
112
|
+
- spec/spec_helper.rb
|