luego 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in luego.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brian Ewing
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.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ Luego
2
+ =====
3
+
4
+ A simple gem to bring the wonder of Io Futures to Ruby.
5
+
6
+ Why do you care?
7
+ ----------------
8
+
9
+ *TODO*
10
+
11
+ Usage
12
+ -----
13
+
14
+ ```ruby
15
+ thing = "hello!"
16
+
17
+ string = Luego::Future.new do
18
+ sleep 5
19
+ thing
20
+ end
21
+
22
+ string.upcase! # 5 seconds pass, then => "HELLO!"
23
+ string === thing # true
24
+
25
+ future = Luego::Future.new &some_block
26
+ future.ready? # => false until the block, in a new thread, returns
27
+
28
+ future.await! # => return value of the block, waits for the thread to join
29
+ ```
30
+
31
+ But wait, there's more!
32
+ --
33
+
34
+ Luego also provides `Luego::Delegate`, which is a 100% transparent proxy for objects, toggleable with `delegate!` and `undelegate!` instance methods.
35
+
36
+ This is how Luego::Future 'becomes' the object as in Io - it simply starts delegating.
37
+
38
+ See the [specs](http://github.com/brianewing/luego/tree/master/spec/delegate_spec.rb) for an idea of how transparent the delegation is.
39
+
40
+ Running the tests
41
+ -----------------
42
+
43
+ Clone the repository, `bundle`, `rspec spec`.
44
+ You should see greens greener than green grass from Greenland.
45
+
46
+ Contributing
47
+ ------------
48
+
49
+ 1. Fork, branch, work, commit
50
+ 2. Submit a pull request
51
+ 3. ???
52
+ 4. Profit!
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,35 @@
1
+ module Luego
2
+ class Delegate < BasicObject
3
+ def initialize(object)
4
+ @child = object
5
+ end
6
+
7
+ def send(*args, &block)
8
+ return super unless delegating?
9
+ @child.send *args, &block
10
+ end
11
+
12
+ def method_missing(*args, &block)
13
+ return super unless delegating?
14
+ send *args, &block
15
+ end
16
+
17
+ def delegating?
18
+ !!@delegating
19
+ end
20
+
21
+ def delegate!
22
+ @delegating = true
23
+ end
24
+
25
+ def undelegate!
26
+ @delegating = false
27
+ end
28
+
29
+ BasicObject.instance_methods.each do |m|
30
+ next if m.to_s.start_with? '__'
31
+
32
+ undef_method m
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ require "luego/delegate"
2
+ require "thread"
3
+
4
+ module Luego
5
+ class Future < Delegate
6
+ private :delegate!, :undelegate!, :delegating?
7
+
8
+ def initialize(thread = nil, &block)
9
+ @thread = thread || ::Thread.new(&block)
10
+ @thread.run
11
+ end
12
+
13
+ def method_missing(*args, &block)
14
+ await!
15
+ super
16
+ end
17
+
18
+ def await!
19
+ return @child unless @child.nil?
20
+
21
+ @child = @thread.value
22
+ end
23
+
24
+ def delegating?
25
+ !@thread.alive?
26
+ end
27
+
28
+ alias :ready? :delegating?
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Luego
2
+ VERSION = "0.1.0"
3
+ end
data/lib/luego.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "luego/version"
2
+ require "luego/delegate"
3
+ require "luego/future"
data/luego.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/luego/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Brian Ewing"]
6
+ gem.email = ["me@brianewing.co.uk"]
7
+ gem.description = %q{Simplify your threads with future objects that turn into the thread value when it finishes}
8
+ gem.summary = %q{Implementation of Futures from the great Io language}
9
+ gem.homepage = "http://brianewing.me"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "luego"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Luego::VERSION
17
+
18
+ gem.add_development_dependency 'rspec'
19
+ end
20
+
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Luego::Delegate do
4
+ it "should take an object when initialised" do
5
+ Luego::Delegate.new("hello")
6
+ end
7
+
8
+ it "should not delegate methods until delegate! is called" do
9
+ string = Luego::Delegate.new("hello world")
10
+ Proc.new { string.upcase }.should raise_error NoMethodError
11
+
12
+ string.delegate!
13
+ string.upcase.should == "HELLO WORLD"
14
+ end
15
+
16
+ it "should offer its own methods for handling the delegation" do
17
+ d = Luego::Delegate.new(nil)
18
+ d.delegating?.should == false
19
+
20
+ d.delegate!
21
+ d.delegating?.should == true
22
+
23
+ d.undelegate!
24
+ d.delegating?.should == false
25
+ end
26
+
27
+ it "should stop delegating when undelegate! is called" do
28
+ string = Luego::Delegate.new("hello world")
29
+ string.delegate!
30
+
31
+ string.should be_a String
32
+ string.should_not be_a Luego::Delegate
33
+ end
34
+
35
+ it "should be equal to the child object when delegating" do
36
+ child = "hello world"
37
+ delegate = Luego::Delegate.new(child)
38
+ delegate.delegate!
39
+
40
+ delegate.should === child
41
+ delegate.should eql child
42
+ delegate.should be_equal child
43
+ end
44
+
45
+ it "should completely delegate all methods" do
46
+ s = "hello world"
47
+ clone = Luego::Delegate.new(s.dup)
48
+ clone.delegate!
49
+
50
+ s.public_methods.each do |m|
51
+ s.should be_a_kind_of clone.method(m).owner # ensure method comes (a superclass of) String
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Luego::Future do
4
+ it "should accept both a block and a thread" do
5
+ Luego::Future.new{sleep 1}.ready?.should == false
6
+ Luego::Future.new(Thread.new{sleep 1}).ready?.should == false
7
+ end
8
+
9
+ it "shouldn't be ready until its block returns" do
10
+ future = Luego::Future.new {sleep 0.1}
11
+ future.ready?.should == false
12
+
13
+ sleep 0.2
14
+ future.ready?.should == true
15
+ end
16
+
17
+ it "should 'become' the future object when it's ready" do
18
+ subject = "hello"
19
+ future = Luego::Future.new{subject}
20
+
21
+ future.should === subject
22
+ subject.upcase!
23
+
24
+ future.should == "HELLO"
25
+ end
26
+ end
@@ -0,0 +1,6 @@
1
+ $:.unshift File.expand_path("../lib", __FILE__)
2
+ require 'luego'
3
+
4
+ RSpec.configure do |config|
5
+ config.color_enabled = true
6
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: luego
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Ewing
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ 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
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Simplify your threads with future objects that turn into the thread value
31
+ when it finishes
32
+ email:
33
+ - me@brianewing.co.uk
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/luego.rb
44
+ - lib/luego/delegate.rb
45
+ - lib/luego/future.rb
46
+ - lib/luego/version.rb
47
+ - luego.gemspec
48
+ - spec/delegate_spec.rb
49
+ - spec/future_spec.rb
50
+ - spec/spec_helper.rb
51
+ homepage: http://brianewing.me
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.21
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Implementation of Futures from the great Io language
75
+ test_files:
76
+ - spec/delegate_spec.rb
77
+ - spec/future_spec.rb
78
+ - spec/spec_helper.rb