future 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 / 2008-06-14
2
+
3
+ * initial release
4
+
data/README ADDED
@@ -0,0 +1,29 @@
1
+
2
+ = future
3
+
4
+
5
+ == Description
6
+
7
+
8
+ == Installation
9
+
10
+ === Archive Installation
11
+
12
+ rake install
13
+
14
+ === Gem Installation
15
+
16
+ gem install future
17
+
18
+
19
+ == Features/Problems
20
+
21
+
22
+ == Synopsis
23
+
24
+
25
+ == Copyright
26
+
27
+ Author:: cho45 <cho45@lowreal.net>
28
+ Copyright:: Copyright (c) 2008 cho45
29
+ License::
data/Rakefile ADDED
@@ -0,0 +1,135 @@
1
+ require 'rubygems'
2
+ require "shipit"
3
+ require 'rake'
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+ require 'rake/packagetask'
7
+ require 'rake/gempackagetask'
8
+ require 'rake/rdoctask'
9
+ require 'rake/contrib/rubyforgepublisher'
10
+ require 'rake/contrib/sshpublisher'
11
+ require 'spec/rake/spectask'
12
+ require 'fileutils'
13
+ include FileUtils
14
+
15
+ $LOAD_PATH.unshift "lib"
16
+ require "future"
17
+
18
+ NAME = "future"
19
+ AUTHOR = "cho45"
20
+ EMAIL = "cho45@lowreal.net"
21
+ DESCRIPTION = "async library"
22
+ RUBYFORGE_PROJECT = "lowreal"
23
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
24
+ BIN_FILES = %w( )
25
+ VERS = Future::VERSION
26
+
27
+ REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
28
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config']
29
+ RDOC_OPTS = [
30
+ '--title', "#{NAME} documentation",
31
+ "--charset", "utf-8",
32
+ "--opname", "index.html",
33
+ "--line-numbers",
34
+ "--main", "README",
35
+ "--inline-source",
36
+ ]
37
+
38
+ task :default => [:spec]
39
+ task :package => [:clean]
40
+
41
+ Spec::Rake::SpecTask.new do |t|
42
+ t.spec_opts = ['--options', "spec/spec.opts"]
43
+ t.spec_files = FileList['spec/*_spec.rb']
44
+ t.rcov = true
45
+ end
46
+
47
+ spec = Gem::Specification.new do |s|
48
+ s.name = NAME
49
+ s.version = VERS
50
+ s.platform = Gem::Platform::RUBY
51
+ s.has_rdoc = true
52
+ s.extra_rdoc_files = ["README", "ChangeLog"]
53
+ s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
54
+ s.summary = DESCRIPTION
55
+ s.description = DESCRIPTION
56
+ s.author = AUTHOR
57
+ s.email = EMAIL
58
+ s.homepage = HOMEPATH
59
+ s.executables = BIN_FILES
60
+ s.rubyforge_project = RUBYFORGE_PROJECT
61
+ s.bindir = "bin"
62
+ s.require_path = "lib"
63
+ s.autorequire = ""
64
+ s.test_files = Dir["test/test_*.rb"]
65
+
66
+ #s.add_dependency('activesupport', '>=1.3.1')
67
+ #s.required_ruby_version = '>= 1.8.2'
68
+
69
+ s.files = %w(README ChangeLog Rakefile) +
70
+ Dir.glob("{bin,doc,spec,test,lib,templates,generator,extras,website,script}/**/*") +
71
+ Dir.glob("ext/**/*.{h,c,rb}") +
72
+ Dir.glob("examples/**/*.rb") +
73
+ Dir.glob("tools/*.rb")
74
+
75
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
76
+ end
77
+
78
+ Rake::GemPackageTask.new(spec) do |p|
79
+ p.need_tar = true
80
+ p.gem_spec = spec
81
+ end
82
+
83
+ task :install do
84
+ name = "#{NAME}-#{VERS}.gem"
85
+ sh %{rake package}
86
+ sh %{sudo gem install pkg/#{name}}
87
+ end
88
+
89
+ task :uninstall => [:clean] do
90
+ sh %{sudo gem uninstall #{NAME}}
91
+ end
92
+
93
+
94
+ Rake::RDocTask.new do |rdoc|
95
+ rdoc.rdoc_dir = 'html'
96
+ rdoc.options += RDOC_OPTS
97
+ rdoc.template = "resh"
98
+ #rdoc.template = "#{ENV['template']}.rb" if ENV['template']
99
+ if ENV['DOC_FILES']
100
+ rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
101
+ else
102
+ rdoc.rdoc_files.include('README', 'ChangeLog')
103
+ rdoc.rdoc_files.include('lib/**/*.rb')
104
+ rdoc.rdoc_files.include('ext/**/*.c')
105
+ end
106
+ end
107
+
108
+ desc "Publish to RubyForge"
109
+ task :rubyforge => [:rdoc, :package] do
110
+ require 'rubyforge'
111
+ @local_dir = "html"
112
+ @host = "cho45@rubyforge.org"
113
+ @remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/#{NAME}"
114
+ sh %{rsync -r --delete --verbose #{@local_dir}/ #{@host}:#{@remote_dir}}
115
+ end
116
+
117
+ desc "Show information about the gem"
118
+ task :debug_gem do
119
+ require "date"
120
+ puts spec.to_ruby
121
+ end
122
+
123
+ Rake::ShipitTask.new do |s|
124
+ s.Step.new {
125
+ system("svn", "up")
126
+ }.and {}
127
+ s.ChangeVersion "lib/future.rb", "VERSION"
128
+ s.Commit
129
+ s.Task :clean, :package
130
+ s.RubyForge
131
+ s.Tag
132
+ s.Twitter
133
+ s.Task :rubyforge
134
+ end
135
+
data/lib/future.rb ADDED
@@ -0,0 +1,48 @@
1
+
2
+ class Future
3
+ VERSION = "0.0.1"
4
+
5
+ def initialize(obj, name, args, block)
6
+ @obj, @name, @args, @block = obj, name, args, block
7
+ @th = Thread.start do
8
+ Thread.pass # 一応明示的に pass しておく
9
+ @obj.send(@name, *@args, &@block)
10
+ end
11
+ end
12
+
13
+ def method_missing(name, *args, &block)
14
+ @th.value.send(name, *args, &block)
15
+ end
16
+
17
+ # 全部委譲してなりすます
18
+ Object.instance_methods.each do |m|
19
+ next if %w|__send__ __id__ object_id|.include? m.to_s
20
+ undef_method m
21
+ end
22
+
23
+ module ObjectExtension
24
+ def async(name=nil, *args, &block)
25
+ name ? Future.new(self, name, args, block) : AsyncObject.new(self)
26
+ end
27
+
28
+ class AsyncObject
29
+ def initialize(obj)
30
+ @obj = obj
31
+ end
32
+
33
+ def method_missing(name, *args, &block)
34
+ Future.new(@obj, name, args, block)
35
+ end
36
+
37
+ Object.instance_methods.each do |m|
38
+ next if %w|__send__ __id__ object_id|.include? m.to_s
39
+ undef_method m
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ class Object
46
+ include Future::ObjectExtension
47
+ end
48
+
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ $LOAD_PATH << "lib"
5
+ $LOAD_PATH << "../lib"
6
+
7
+ require "future"
8
+
9
+ require "rubygems"
10
+ require "spec"
11
+
12
+ require "timeout"
13
+
14
+ class TestObject
15
+ @@result = []
16
+
17
+ def result
18
+ ret = @@result.dup
19
+ @@result.clear
20
+ ret
21
+ end
22
+
23
+ def heavy(n, value, &block)
24
+ sleep n
25
+ @@result << [n, value]
26
+ if block_given?
27
+ yield
28
+ else
29
+ value
30
+ end
31
+ end
32
+ end
33
+
34
+ describe Future do
35
+ before do
36
+ @o = TestObject.new
37
+ end
38
+
39
+ it "should handle message asynchronously" do
40
+ future = nil
41
+
42
+ timeout(0.1) do
43
+ future = Future.new(Kernel, :sleep, [ 0.5 ], nil)
44
+ end
45
+
46
+ Proc.new {
47
+ timeout(0.1) do
48
+ future.inspect
49
+ end
50
+ }.should raise_error(Timeout::Error)
51
+
52
+ future.inspect # block
53
+
54
+ Proc.new {
55
+ timeout(0.1) do
56
+ future.inspect
57
+ end
58
+ }.should_not raise_error(Timeout::Error)
59
+ end
60
+
61
+ it "should append async mehtod to Object" do
62
+ # normal
63
+ @o.heavy(0.1, :foo)
64
+ @o.result.should == [ [0.1, :foo] ]
65
+
66
+ # async
67
+ future = @o.async(:heavy, 0.1, :foo)
68
+ @o.result.should == [ ]
69
+ future.inspect.should == ":foo"
70
+ @o.result.should == [ [0.1, :foo] ]
71
+
72
+ # async object
73
+ future = @o.async.heavy(0.1, :foo)
74
+ @o.result.should == [ ]
75
+ future.inspect.should == ":foo"
76
+ @o.result.should == [ [0.1, :foo] ]
77
+ end
78
+
79
+ it "should handle block correctly" do
80
+ future = @o.async(:heavy, 0.1, :foo) {
81
+ :bar
82
+ }
83
+ @o.result.should == [ ]
84
+ future.inspect.should == ":bar"
85
+ @o.result.should == [ [0.1, :foo] ]
86
+ end
87
+ end
88
+
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: future
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - cho45
8
+ autorequire: ""
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-14 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: async library
17
+ email: cho45@lowreal.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - ChangeLog
25
+ files:
26
+ - README
27
+ - ChangeLog
28
+ - Rakefile
29
+ - spec/future_spec.rb
30
+ - spec/spec.opts
31
+ - lib/future.rb
32
+ has_rdoc: true
33
+ homepage: http://lowreal.rubyforge.org
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --title
37
+ - future documentation
38
+ - --charset
39
+ - utf-8
40
+ - --opname
41
+ - index.html
42
+ - --line-numbers
43
+ - --main
44
+ - README
45
+ - --inline-source
46
+ - --exclude
47
+ - ^(examples|extras)/
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project: lowreal
65
+ rubygems_version: 1.1.1
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: async library
69
+ test_files: []
70
+