/lib/chingu/async/task_builder.rb
Ruby | 71 lines | 24 code | 11 blank | 36 comment | 1 complexity | 37eaf1ab2015c8cbe616b9dc555967c0 MD5 | raw file
Possible License(s): LGPL-2.1
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 22module 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 71end