/lib/chingu/async/task_builder.rb

http://github.com/ippa/chingu · Ruby · 71 lines · 24 code · 11 blank · 36 comment · 1 complexity · 37eaf1ab2015c8cbe616b9dc555967c0 MD5 · raw file

  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. module Chingu
  22. module Async
  23. #
  24. # Implements a DSL for appending new tasks to an task queue.
  25. #
  26. class TaskBuilder
  27. def initialize(tasks)
  28. @tasks = tasks
  29. end
  30. #
  31. # Add a new task to the queue. The first argument is a Symbol or
  32. # String naming the type of task; remaining arguments are passed
  33. # on to the task's constructor.
  34. #
  35. # If a block is supplied, it is scheduled to be executed as soon as the
  36. # task is finished.
  37. #
  38. def task(task, *args, &block)
  39. case task
  40. when Symbol, String
  41. klass_name = Chingu::Inflector.camelize(task)
  42. klass = Chingu::AsyncTasks.const_get(klass_name)
  43. task = klass.new(*args, &block)
  44. when Chingu::Async::BasicTask
  45. # pass
  46. when Class
  47. task = task.new(*args, &block)
  48. else raise TypeError, "task must be a Task object or task name"
  49. end
  50. @tasks.enq(task)
  51. task
  52. end
  53. #
  54. # Attempting to invoke a nonexistant method automatically calls
  55. # +task+ with the method name as the task type.
  56. #
  57. alias :method_missing :task
  58. end
  59. end
  60. end