chingu 0.8.1 → 0.9rc1
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/README.rdoc +28 -0
- data/Rakefile +1 -1
- data/benchmarks/benchmark_ping_localhost.rb +63 -0
- data/chingu.gemspec +24 -6
- data/examples/example12_trait_timer.rb +5 -1
- data/examples/example28_networking.rb +75 -0
- data/examples/example29_asynchronous.rb +65 -0
- data/lib/chingu.rb +3 -1
- data/lib/chingu/async/basic_task.rb +46 -0
- data/lib/chingu/async/task_builder.rb +71 -0
- data/lib/chingu/async/task_list.rb +67 -0
- data/lib/chingu/async_tasks/call.rb +48 -0
- data/lib/chingu/async_tasks/exec.rb +48 -0
- data/lib/chingu/async_tasks/move.rb +45 -0
- data/lib/chingu/async_tasks/parallel.rb +63 -0
- data/lib/chingu/async_tasks/tween.rb +73 -0
- data/lib/chingu/async_tasks/wait.rb +54 -0
- data/lib/chingu/classic_game_object.rb +1 -1
- data/lib/chingu/core_ext/range.rb +13 -0
- data/lib/chingu/game_states/enter_name.rb +21 -11
- data/lib/chingu/game_states/network_client.rb +205 -0
- data/lib/chingu/game_states/network_server.rb +262 -0
- data/lib/chingu/simple_menu.rb +10 -2
- data/lib/chingu/traits/asynchronous.rb +84 -0
- data/lib/chingu/traits/simple_sprite.rb +1 -1
- data/lib/chingu/traits/sprite.rb +1 -1
- data/lib/chingu/window.rb +1 -1
- metadata +29 -10
data/README.rdoc
CHANGED
@@ -776,6 +776,34 @@ Adds class and instance methods for basic collision detection.
|
|
776
776
|
# do something
|
777
777
|
end
|
778
778
|
|
779
|
+
==== Trait "asynchronous"
|
780
|
+
Allows your code to specify a GameObject's behavior asynchronously, including
|
781
|
+
tweening, movement and even method calls. Tasks are added to a queue to be
|
782
|
+
processed in order; the task at the front of the queue is updated each tick
|
783
|
+
and removed when it has finished.
|
784
|
+
|
785
|
+
# Simple one-trick example
|
786
|
+
# This will cause an object to move from its current location to 64,64.
|
787
|
+
@guy.async.tween :x => 64, :y => 64
|
788
|
+
|
789
|
+
# Block syntax example
|
790
|
+
# This will cause a line of text to fade out and vanish.
|
791
|
+
Chingu::Text.trait :asynchronous
|
792
|
+
message = Chingu::Text.new 'Goodbye, World!'
|
793
|
+
message.async do |q|
|
794
|
+
q.wait 500
|
795
|
+
q.tween 2000, :alpha => 0, :scale => 2
|
796
|
+
q.call :destroy
|
797
|
+
end
|
798
|
+
|
799
|
+
Currently available tasks are wait(timeout, &condition), tween(timeout,
|
800
|
+
properties), call(method, *arguments) and exec { ... }.
|
801
|
+
|
802
|
+
For a more complete example of how to use this trait, see
|
803
|
+
examples/example_async.rb.
|
804
|
+
|
805
|
+
|
806
|
+
|
779
807
|
==== (IN DEVELOPMENT) Trait "retrofy"
|
780
808
|
Providing easier handling of the "retrofy" effect (non-blurry zoom)
|
781
809
|
Aims to help out when using zoom-factor to create a retrofeeling with big pixels.
|
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ begin
|
|
14
14
|
gemspec.rubyforge_project = "chingu"
|
15
15
|
gemspec.version = Chingu::VERSION
|
16
16
|
|
17
|
-
gemspec.add_dependency 'gosu', '>= 0.7.
|
17
|
+
gemspec.add_dependency 'gosu', '>= 0.7.27'
|
18
18
|
gemspec.add_dependency 'rest-client'
|
19
19
|
gemspec.add_dependency 'crack'
|
20
20
|
gemspec.add_development_dependency 'rspec', '>= 2.1.0'
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require File.join(File.dirname($0), "..", "lib", "chingu")
|
4
|
+
include Gosu
|
5
|
+
include Chingu
|
6
|
+
|
7
|
+
#
|
8
|
+
# Simple
|
9
|
+
#
|
10
|
+
class Game < Chingu::Window
|
11
|
+
|
12
|
+
def setup
|
13
|
+
on_input(:esc, :exit)
|
14
|
+
|
15
|
+
@client = Client.new
|
16
|
+
@server = Server.new
|
17
|
+
|
18
|
+
@server.start("0.0.0.0", 1234)
|
19
|
+
@client.connect("127.0.0.1", 1234)
|
20
|
+
end
|
21
|
+
|
22
|
+
def update
|
23
|
+
@client.update
|
24
|
+
@client.update_trait
|
25
|
+
@server.update
|
26
|
+
@server.update_trait
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# Our Client. We inherit from Chingu::GameStates::NetworkClient
|
33
|
+
#
|
34
|
+
class Client < GameStates::NetworkClient
|
35
|
+
trait :timer
|
36
|
+
|
37
|
+
def on_connect
|
38
|
+
$window.caption = "[CONNECTED]"
|
39
|
+
send_msg(:timestamp => Gosu::milliseconds)
|
40
|
+
every(1000) { send_msg(:timestamp => Gosu::milliseconds) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def on_msg(msg)
|
44
|
+
latency = (Gosu::milliseconds - msg[:timestamp])
|
45
|
+
#puts "PONG: #{latency}ms"
|
46
|
+
$window.caption = "PONG: #{latency}ms"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Our Server. We inherit from Chingu::GameStates::NetworkServer
|
53
|
+
#
|
54
|
+
class Server < GameStates::NetworkServer
|
55
|
+
|
56
|
+
def on_msg(socket, msg)
|
57
|
+
#puts "PING: #{msg[:timestamp]}"
|
58
|
+
send_msg(socket, {:timestamp => msg[:timestamp]})
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
Game.new.show
|
data/chingu.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{chingu}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.9rc1"
|
9
9
|
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new("
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["ippa"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-02-20}
|
13
13
|
s.description = %q{OpenGL accelerated 2D game framework for Ruby. Builds on Gosu (Ruby/C++) which provides all the core functionality. Chingu adds simple yet powerful game states, prettier input handling, deployment safe asset-handling, a basic re-usable game object and stackable game logic.}
|
14
14
|
s.email = %q{ippa@rubylicio.us}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"benchmarks/benchmark4.rb",
|
31
31
|
"benchmarks/benchmark5.rb",
|
32
32
|
"benchmarks/benchmark6.rb",
|
33
|
+
"benchmarks/benchmark_ping_localhost.rb",
|
33
34
|
"benchmarks/game_object_list_benchmark.rb",
|
34
35
|
"benchmarks/game_objects_benchmark.rb",
|
35
36
|
"benchmarks/lookup_benchmark.rb",
|
@@ -60,6 +61,8 @@ Gem::Specification.new do |s|
|
|
60
61
|
"examples/example25_fibers_state_machine.rb",
|
61
62
|
"examples/example26_splash_screen.rb",
|
62
63
|
"examples/example27_console.rb",
|
64
|
+
"examples/example28_networking.rb",
|
65
|
+
"examples/example29_asynchronous.rb",
|
63
66
|
"examples/example2_gamestate_basics.rb",
|
64
67
|
"examples/example3_parallax.rb",
|
65
68
|
"examples/example4_gamestates.rb",
|
@@ -118,10 +121,20 @@ Gem::Specification.new do |s|
|
|
118
121
|
"lib/chingu.rb",
|
119
122
|
"lib/chingu/animation.rb",
|
120
123
|
"lib/chingu/assets.rb",
|
124
|
+
"lib/chingu/async/basic_task.rb",
|
125
|
+
"lib/chingu/async/task_builder.rb",
|
126
|
+
"lib/chingu/async/task_list.rb",
|
127
|
+
"lib/chingu/async_tasks/call.rb",
|
128
|
+
"lib/chingu/async_tasks/exec.rb",
|
129
|
+
"lib/chingu/async_tasks/move.rb",
|
130
|
+
"lib/chingu/async_tasks/parallel.rb",
|
131
|
+
"lib/chingu/async_tasks/tween.rb",
|
132
|
+
"lib/chingu/async_tasks/wait.rb",
|
121
133
|
"lib/chingu/basic_game_object.rb",
|
122
134
|
"lib/chingu/classic_game_object.rb",
|
123
135
|
"lib/chingu/console.rb",
|
124
136
|
"lib/chingu/core_ext/array.rb",
|
137
|
+
"lib/chingu/core_ext/range.rb",
|
125
138
|
"lib/chingu/fpscounter.rb",
|
126
139
|
"lib/chingu/game_object.rb",
|
127
140
|
"lib/chingu/game_object_list.rb",
|
@@ -132,6 +145,8 @@ Gem::Specification.new do |s|
|
|
132
145
|
"lib/chingu/game_states/edit.rb",
|
133
146
|
"lib/chingu/game_states/enter_name.rb",
|
134
147
|
"lib/chingu/game_states/fade_to.rb",
|
148
|
+
"lib/chingu/game_states/network_client.rb",
|
149
|
+
"lib/chingu/game_states/network_server.rb",
|
135
150
|
"lib/chingu/game_states/pause.rb",
|
136
151
|
"lib/chingu/game_states/popup.rb",
|
137
152
|
"lib/chingu/gosu_ext/image.rb",
|
@@ -155,6 +170,7 @@ Gem::Specification.new do |s|
|
|
155
170
|
"lib/chingu/simple_menu.rb",
|
156
171
|
"lib/chingu/text.rb",
|
157
172
|
"lib/chingu/traits/animation.rb",
|
173
|
+
"lib/chingu/traits/asynchronous.rb",
|
158
174
|
"lib/chingu/traits/bounding_box.rb",
|
159
175
|
"lib/chingu/traits/bounding_circle.rb",
|
160
176
|
"lib/chingu/traits/collision_detection.rb",
|
@@ -214,6 +230,8 @@ Gem::Specification.new do |s|
|
|
214
230
|
"examples/example25_fibers_state_machine.rb",
|
215
231
|
"examples/example26_splash_screen.rb",
|
216
232
|
"examples/example27_console.rb",
|
233
|
+
"examples/example28_networking.rb",
|
234
|
+
"examples/example29_asynchronous.rb",
|
217
235
|
"examples/example2_gamestate_basics.rb",
|
218
236
|
"examples/example3_parallax.rb",
|
219
237
|
"examples/example4_gamestates.rb",
|
@@ -250,14 +268,14 @@ Gem::Specification.new do |s|
|
|
250
268
|
s.specification_version = 3
|
251
269
|
|
252
270
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
253
|
-
s.add_runtime_dependency(%q<gosu>, [">= 0.7.
|
271
|
+
s.add_runtime_dependency(%q<gosu>, [">= 0.7.27"])
|
254
272
|
s.add_runtime_dependency(%q<rest-client>, [">= 0"])
|
255
273
|
s.add_runtime_dependency(%q<crack>, [">= 0"])
|
256
274
|
s.add_development_dependency(%q<rspec>, [">= 2.1.0"])
|
257
275
|
s.add_development_dependency(%q<watchr>, [">= 0"])
|
258
276
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
259
277
|
else
|
260
|
-
s.add_dependency(%q<gosu>, [">= 0.7.
|
278
|
+
s.add_dependency(%q<gosu>, [">= 0.7.27"])
|
261
279
|
s.add_dependency(%q<rest-client>, [">= 0"])
|
262
280
|
s.add_dependency(%q<crack>, [">= 0"])
|
263
281
|
s.add_dependency(%q<rspec>, [">= 2.1.0"])
|
@@ -265,7 +283,7 @@ Gem::Specification.new do |s|
|
|
265
283
|
s.add_dependency(%q<rcov>, [">= 0"])
|
266
284
|
end
|
267
285
|
else
|
268
|
-
s.add_dependency(%q<gosu>, [">= 0.7.
|
286
|
+
s.add_dependency(%q<gosu>, [">= 0.7.27"])
|
269
287
|
s.add_dependency(%q<rest-client>, [">= 0"])
|
270
288
|
s.add_dependency(%q<crack>, [">= 0"])
|
271
289
|
s.add_dependency(%q<rspec>, [">= 2.1.0"])
|
@@ -24,7 +24,11 @@ class Cube < GameObject
|
|
24
24
|
def initialize(options)
|
25
25
|
super
|
26
26
|
self.input = { :left => :left, :right => :right, :up => :up, :down => :down,
|
27
|
-
:space => :shake1, :return => :shake2 }
|
27
|
+
:space => :shake1, :return => :shake2, :a => :test_after }
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_after
|
31
|
+
after(1000) { @color = Color::CYAN }
|
28
32
|
end
|
29
33
|
|
30
34
|
def left
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require File.join(File.dirname($0), "..", "lib", "chingu")
|
4
|
+
include Gosu
|
5
|
+
include Chingu
|
6
|
+
|
7
|
+
#
|
8
|
+
# Simple
|
9
|
+
#
|
10
|
+
class Game < Chingu::Window
|
11
|
+
|
12
|
+
def setup
|
13
|
+
on_input(:esc, :exit)
|
14
|
+
|
15
|
+
@client = Client.new
|
16
|
+
@server = Server.new
|
17
|
+
|
18
|
+
@server.start("0.0.0.0", 1234)
|
19
|
+
@client.connect("127.0.0.1", 1234)
|
20
|
+
end
|
21
|
+
|
22
|
+
def update
|
23
|
+
#
|
24
|
+
# Since we have 2 games states going we don't switch to them ( i.e. push_game_state @server )
|
25
|
+
# Rather we just call #update manually so they can do their thing.
|
26
|
+
#
|
27
|
+
# Server#update will poll incoming connections, handle sockets and read/parse incomong data.
|
28
|
+
# Client#update will read/parse incoming data, send output.
|
29
|
+
#
|
30
|
+
@client.update
|
31
|
+
@server.update
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# Our Client. We inherit from Chingu::GameStates::NetworkClient
|
38
|
+
#
|
39
|
+
class Client < GameStates::NetworkClient
|
40
|
+
|
41
|
+
def on_connect
|
42
|
+
puts "[#{self.ip}] Connected! Sending a msg..."
|
43
|
+
send_msg(:hi => :there)
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Only 1 argument, the msg, since client only does 1 connecion (the one to the server)
|
48
|
+
#
|
49
|
+
def on_msg(msg)
|
50
|
+
puts "Client Got: #{msg.inspect}"
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Our Server. We inherit from Chingu::GameStates::NetworkServer
|
57
|
+
#
|
58
|
+
class Server < GameStates::NetworkServer
|
59
|
+
|
60
|
+
#
|
61
|
+
# Overload on_msg callback in server to make something happen
|
62
|
+
# Servers #on_msg takes 2 arguments, since server can handle many clients
|
63
|
+
#
|
64
|
+
def on_connect(socket)
|
65
|
+
send_msg(socket, {:hi => :there})
|
66
|
+
end
|
67
|
+
|
68
|
+
def on_msg(socket, msg)
|
69
|
+
puts "Server Got: #{msg.inspect}"
|
70
|
+
send_msg(socket, {:woff => :mjau, :numbers => [1,2,3]})
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
Game.new.show
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems' rescue nil
|
3
|
+
$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
|
4
|
+
require 'chingu'
|
5
|
+
include Gosu
|
6
|
+
include Chingu
|
7
|
+
$stderr.sync = $stdout.sync = true
|
8
|
+
|
9
|
+
Chingu::Text.trait :asynchronous
|
10
|
+
|
11
|
+
class BadGuy < Chingu::GameObject
|
12
|
+
trait :asynchronous
|
13
|
+
|
14
|
+
def fire!
|
15
|
+
# Create a text object to represent shooting sounds.
|
16
|
+
text = Chingu::Text.create("Pew!", :x => x, :y => y - height)
|
17
|
+
|
18
|
+
# Now, make that text object fade out and disappear asynchronously.
|
19
|
+
text.async do |q|
|
20
|
+
q.tween(750, :alpha => 0, :scale => 2)
|
21
|
+
q.call :destroy
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
class Game < Chingu::Window
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
super 480, 240, false
|
31
|
+
|
32
|
+
self.caption = "Press [SPACE] to run the demo"
|
33
|
+
|
34
|
+
# Create our bad guy!
|
35
|
+
@boss = BadGuy.create :image => 'Starfighter.bmp'
|
36
|
+
|
37
|
+
# Instruct the boss to move along a path while rotating clockwise.
|
38
|
+
# Nothing is actually done until the run loop calls #update.
|
39
|
+
@boss.async do |q|
|
40
|
+
# These tasks are performed sequentially and asynchronously
|
41
|
+
# using the magic of queues. Each GameObject has its own!
|
42
|
+
q.wait { button_down? Gosu::KbSpace }
|
43
|
+
q.tween(1000, :x => 100, :y => 100, :angle => 45)
|
44
|
+
q.tween(1000, :y => 200, :angle => 90)
|
45
|
+
q.call :fire!
|
46
|
+
end
|
47
|
+
|
48
|
+
# Single tasks can also be given with a less verbose syntax.
|
49
|
+
# Wait a second...
|
50
|
+
@boss.async.wait 1000
|
51
|
+
|
52
|
+
# Move and fire one more time, just for good measure.
|
53
|
+
@boss.async do |q|
|
54
|
+
q.tween(1000, :x => 200, :y => 100, :angle => 360)
|
55
|
+
3.times do
|
56
|
+
q.wait 500
|
57
|
+
q.call :fire!
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
Game.new.show
|
data/lib/chingu.rb
CHANGED
@@ -34,10 +34,12 @@ require File.join(CHINGU_ROOT,"chingu","require_all") # Thanks to http://github.
|
|
34
34
|
# and GameObject to get the correct class_inheritable_accssor
|
35
35
|
require_all "#{CHINGU_ROOT}/chingu/helpers"
|
36
36
|
require_all "#{CHINGU_ROOT}/chingu/traits"
|
37
|
+
require_all "#{CHINGU_ROOT}/chingu/async"
|
38
|
+
require_all "#{CHINGU_ROOT}/chingu/async_tasks"
|
37
39
|
require_all "#{CHINGU_ROOT}/chingu"
|
38
40
|
|
39
41
|
module Chingu
|
40
|
-
VERSION = "0.
|
42
|
+
VERSION = "0.9rc1"
|
41
43
|
|
42
44
|
DEBUG_COLOR = Gosu::Color.new(0xFFFF0000)
|
43
45
|
DEBUG_ZORDER = 9999
|
@@ -0,0 +1,46 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Chingu -- OpenGL accelerated 2D game framework for Ruby
|
4
|
+
# Copyright (C) 2009 ippa / ippa@rubylicio.us
|
5
|
+
#
|
6
|
+
# This library is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation; either
|
9
|
+
# version 2.1 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# This library is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with this library; if not, write to the Free Software
|
18
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19
|
+
#
|
20
|
+
#++
|
21
|
+
|
22
|
+
require 'weakref'
|
23
|
+
|
24
|
+
module Chingu
|
25
|
+
module Async
|
26
|
+
|
27
|
+
class BasicTask
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Returns true if the task has finished executing. The meaning of
|
34
|
+
# "finished" is determined by the particular subclass.
|
35
|
+
#
|
36
|
+
def finished?
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
def update(object)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Chingu -- OpenGL accelerated 2D game framework for Ruby
|
4
|
+
# Copyright (C) 2009 ippa / ippa@rubylicio.us
|
5
|
+
#
|
6
|
+
# This library is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation; either
|
9
|
+
# version 2.1 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# This library is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with this library; if not, write to the Free Software
|
18
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19
|
+
#
|
20
|
+
#++
|
21
|
+
|
22
|
+
module Chingu
|
23
|
+
module Async
|
24
|
+
|
25
|
+
#
|
26
|
+
# Implements a DSL for appending new tasks to an task queue.
|
27
|
+
#
|
28
|
+
class TaskBuilder
|
29
|
+
def initialize(tasks)
|
30
|
+
@tasks = tasks
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Add a new task to the queue. The first argument is a Symbol or
|
35
|
+
# String naming the type of task; remaining arguments are passed
|
36
|
+
# on to the task's constructor.
|
37
|
+
#
|
38
|
+
# If a block is supplied, it is scheduled to be executed as soon as the
|
39
|
+
# task is finished.
|
40
|
+
#
|
41
|
+
def task(task, *args, &block)
|
42
|
+
case task
|
43
|
+
when Symbol, String
|
44
|
+
klass_name = Chingu::Inflector.camelize(task)
|
45
|
+
klass = Chingu::AsyncTasks.const_get(klass_name)
|
46
|
+
|
47
|
+
task = klass.new(*args, &block)
|
48
|
+
|
49
|
+
when Chingu::Async::BasicTask
|
50
|
+
# pass
|
51
|
+
|
52
|
+
when Class
|
53
|
+
task = task.new(*args, &block)
|
54
|
+
|
55
|
+
else raise TypeError, "task must be a Task object or task name"
|
56
|
+
end
|
57
|
+
|
58
|
+
@tasks.enq(task)
|
59
|
+
task
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
# Attempting to invoke a nonexistant method automatically calls
|
64
|
+
# +task+ with the method name as the task type.
|
65
|
+
#
|
66
|
+
alias :method_missing :task
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|