luego 0.1.0 → 0.2.0
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/.travis.yml +11 -0
- data/README.md +15 -8
- data/lib/luego/delegate.rb +2 -2
- data/lib/luego/future.rb +1 -2
- data/lib/luego/kernel.rb +7 -0
- data/lib/luego/version.rb +1 -1
- data/spec/delegate_spec.rb +2 -2
- data/spec/future_spec.rb +8 -5
- data/spec/kernel_spec.rb +16 -0
- metadata +55 -36
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,20 +1,27 @@
|
|
1
|
-
Luego
|
1
|
+
Luego 
|
2
2
|
=====
|
3
3
|
|
4
4
|
A simple gem to bring the wonder of Io Futures to Ruby.
|
5
5
|
|
6
|
-
|
7
|
-
----------------
|
6
|
+
Futures are objects, created with a(n expensive) block, that can be passed around freely and only block at the point that they're used.
|
8
7
|
|
9
|
-
|
8
|
+
```ruby
|
9
|
+
thing = future { fetch_thing_from_slow_api }
|
10
|
+
```
|
11
|
+
|
12
|
+
`thing` is now just an object that can be passed around. It will 'become' the result as soon as it's used, or your block returns - whichever comes first.
|
13
|
+
|
14
|
+
In practice, you could use futures for things like IO, while passing it around like it's the end result you want.
|
15
|
+
**The rest of your stack can get on with doing its thing until the result is actually needed/used.**
|
10
16
|
|
11
17
|
Usage
|
12
18
|
-----
|
13
19
|
|
14
20
|
```ruby
|
21
|
+
require 'luego/kernel'
|
15
22
|
thing = "hello!"
|
16
23
|
|
17
|
-
string =
|
24
|
+
string = future do
|
18
25
|
sleep 5
|
19
26
|
thing
|
20
27
|
end
|
@@ -22,10 +29,10 @@ end
|
|
22
29
|
string.upcase! # 5 seconds pass, then => "HELLO!"
|
23
30
|
string === thing # true
|
24
31
|
|
25
|
-
|
26
|
-
|
32
|
+
aye = future &some_block
|
33
|
+
aye.ready? # => false until the block, in a new thread, returns
|
27
34
|
|
28
|
-
|
35
|
+
aye.await! # => return value of the block, waits for the thread to join
|
29
36
|
```
|
30
37
|
|
31
38
|
But wait, there's more!
|
data/lib/luego/delegate.rb
CHANGED
@@ -5,12 +5,12 @@ module Luego
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def send(*args, &block)
|
8
|
-
return super
|
8
|
+
return super if not delegating?
|
9
9
|
@child.send *args, &block
|
10
10
|
end
|
11
11
|
|
12
12
|
def method_missing(*args, &block)
|
13
|
-
return super
|
13
|
+
return super if not delegating?
|
14
14
|
send *args, &block
|
15
15
|
end
|
16
16
|
|
data/lib/luego/future.rb
CHANGED
@@ -7,14 +7,13 @@ module Luego
|
|
7
7
|
|
8
8
|
def initialize(thread = nil, &block)
|
9
9
|
@thread = thread || ::Thread.new(&block)
|
10
|
-
@thread.run
|
11
10
|
end
|
12
11
|
|
13
12
|
def method_missing(*args, &block)
|
14
13
|
await!
|
15
14
|
super
|
16
15
|
end
|
17
|
-
|
16
|
+
|
18
17
|
def await!
|
19
18
|
return @child unless @child.nil?
|
20
19
|
|
data/lib/luego/kernel.rb
ADDED
data/lib/luego/version.rb
CHANGED
data/spec/delegate_spec.rb
CHANGED
@@ -7,7 +7,7 @@ describe Luego::Delegate do
|
|
7
7
|
|
8
8
|
it "should not delegate methods until delegate! is called" do
|
9
9
|
string = Luego::Delegate.new("hello world")
|
10
|
-
|
10
|
+
lambda { string.upcase }.should raise_error NoMethodError
|
11
11
|
|
12
12
|
string.delegate!
|
13
13
|
string.upcase.should == "HELLO WORLD"
|
@@ -16,7 +16,7 @@ describe Luego::Delegate do
|
|
16
16
|
it "should offer its own methods for handling the delegation" do
|
17
17
|
d = Luego::Delegate.new(nil)
|
18
18
|
d.delegating?.should == false
|
19
|
-
|
19
|
+
|
20
20
|
d.delegate!
|
21
21
|
d.delegating?.should == true
|
22
22
|
|
data/spec/future_spec.rb
CHANGED
@@ -2,21 +2,24 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Luego::Future do
|
4
4
|
it "should accept both a block and a thread" do
|
5
|
-
|
6
|
-
Luego::Future.new(
|
5
|
+
sleeper = lambda { sleep 1 }
|
6
|
+
Luego::Future.new(&sleeper).ready?.should == false
|
7
|
+
Luego::Future.new(Thread.new &sleeper).ready?.should == false
|
7
8
|
end
|
8
9
|
|
9
10
|
it "shouldn't be ready until its block returns" do
|
10
|
-
|
11
|
+
thread = Thread.new { sleep 0.05 }
|
12
|
+
|
13
|
+
future = Luego::Future.new(thread)
|
11
14
|
future.ready?.should == false
|
12
15
|
|
13
|
-
|
16
|
+
thread.join
|
14
17
|
future.ready?.should == true
|
15
18
|
end
|
16
19
|
|
17
20
|
it "should 'become' the future object when it's ready" do
|
18
21
|
subject = "hello"
|
19
|
-
future = Luego::Future.new{subject}
|
22
|
+
future = Luego::Future.new { subject }
|
20
23
|
|
21
24
|
future.should === subject
|
22
25
|
subject.upcase!
|
data/spec/kernel_spec.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
describe "Kernel#future" do
|
2
|
+
it "should not be defined before requiring 'luego/kernel'" do
|
3
|
+
Kernel.instance_methods.should_not include(:future)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "should define Kernel#future" do
|
7
|
+
require 'luego/kernel'
|
8
|
+
|
9
|
+
Kernel.instance_methods.should include(:future)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should work like using Luego::Future.new" do
|
13
|
+
future { 5 }.should == 5
|
14
|
+
future { sleep 5 }.ready?.should == false
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,41 +1,48 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: luego
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 2380615109590600174
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Brian Ewing
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-10-31 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: rspec
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :development
|
23
22
|
prerelease: false
|
24
|
-
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
24
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 2002549777813010636
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Simplify your threads with future objects that turn into the thread value when it finishes
|
35
|
+
email:
|
33
36
|
- me@brianewing.co.uk
|
34
37
|
executables: []
|
38
|
+
|
35
39
|
extensions: []
|
40
|
+
|
36
41
|
extra_rdoc_files: []
|
37
|
-
|
42
|
+
|
43
|
+
files:
|
38
44
|
- .gitignore
|
45
|
+
- .travis.yml
|
39
46
|
- Gemfile
|
40
47
|
- LICENSE
|
41
48
|
- README.md
|
@@ -43,36 +50,48 @@ files:
|
|
43
50
|
- lib/luego.rb
|
44
51
|
- lib/luego/delegate.rb
|
45
52
|
- lib/luego/future.rb
|
53
|
+
- lib/luego/kernel.rb
|
46
54
|
- lib/luego/version.rb
|
47
55
|
- luego.gemspec
|
48
56
|
- spec/delegate_spec.rb
|
49
57
|
- spec/future_spec.rb
|
58
|
+
- spec/kernel_spec.rb
|
50
59
|
- spec/spec_helper.rb
|
51
60
|
homepage: http://brianewing.me
|
52
61
|
licenses: []
|
62
|
+
|
53
63
|
post_install_message:
|
54
64
|
rdoc_options: []
|
55
|
-
|
65
|
+
|
66
|
+
require_paths:
|
56
67
|
- lib
|
57
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
69
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 2002549777813010636
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
78
|
none: false
|
65
|
-
requirements:
|
66
|
-
- -
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 2002549777813010636
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
69
86
|
requirements: []
|
87
|
+
|
70
88
|
rubyforge_project:
|
71
|
-
rubygems_version: 1.8.
|
89
|
+
rubygems_version: 1.8.24
|
72
90
|
signing_key:
|
73
91
|
specification_version: 3
|
74
92
|
summary: Implementation of Futures from the great Io language
|
75
|
-
test_files:
|
93
|
+
test_files:
|
76
94
|
- spec/delegate_spec.rb
|
77
95
|
- spec/future_spec.rb
|
96
|
+
- spec/kernel_spec.rb
|
78
97
|
- spec/spec_helper.rb
|