lib/tap/task.rb in bahuvrihi-tap-0.10.6 vs lib/tap/task.rb in bahuvrihi-tap-0.10.7

- old
+ new

@@ -1,6 +1,5 @@ -require 'tap/support/batchable' require 'tap/support/executable' require 'tap/support/lazydoc/method' autoload(:OptionParser, 'optparse') module Tap @@ -156,11 +155,10 @@ # t2 = t1.initialize_batch_obj # t1.array == t2.array # => true # t1.array.object_id == t2.array.object_id # => false # class Task - include Support::Batchable include Support::Configurable include Support::Executable class << self # Returns the default name for the class: to_s.underscore @@ -355,13 +353,13 @@ def dependency(name, dependency_class, *args) depends_on(dependency_class, *args) define_method(name) do - index = Support::Executable.index(dependency_class.instance, args) - Support::Executable.resolve([index]) - Support::Executable.results[index]._current + index = app.dependencies.index(dependency_class.instance, args) + app.dependencies.resolve([index]) + app.dependencies.results[index]._current end public(name) end @@ -418,14 +416,10 @@ instance_variable_set(:@default_name, 'tap/task') instance_variable_set(:@dependencies, []) lazy_attr :manifest lazy_attr :args - # The application used to load config_file templates - # (and hence, to initialize batched objects). - attr_reader :app - # The name of self. #-- # Currently names may be any object. Audit makes use of name # via to_s, as does app when figuring configuration filepaths. attr_accessor :name @@ -437,18 +431,19 @@ # objects will be initialized for each configuration template # specified by app.each_config_template(config_file) where # config_file = app.config_filepath(name). def initialize(config={}, name=nil, app=App.instance, &task_block) super() - - @app = app + @name = name || self.class.default_name @task_block = (task_block == nil ? default_task_block : task_block) - @_method_name = :execute + @app = app + @_method_name = :execute_with_callbacks @on_complete_block = nil @dependencies = [] + @batch = [self] case config when Support::InstanceConfiguration @config = config config.bind(self) @@ -457,76 +452,30 @@ end self.class.dependencies.each do |task_class, args| depends_on(task_class.instance, *args) end + + workflow end # Creates a new batched object and adds the object to batch. The batched object # will be a duplicate of the current object but with a new name and/or # configurations. def initialize_batch_obj(overrides={}, name=nil) obj = super().reconfigure(overrides) obj.name = name if name obj end - - # Enqueues self and self.batch to app with the inputs. - # The number of inputs provided should match the number - # of inputs specified by the arity of the _method_name method. - def enq(*inputs) - app.queue.enq(self, inputs) - end - batch_function :enq - batch_function(:on_complete) {} - - # Convenience method, equivalent to: - # self.app.sequence([self] + tasks) - def sequence(*tasks) - app.sequence([self] + tasks) - end - - # Convenience method, equivalent to: - # self.app.fork(self, targets) - def fork(*targets) - app.fork(self, targets) - end - - # Convenience method, equivalent to: - # self.app.merge(self, sources) - def merge(*sources) - app.merge(self, sources) - end - - # Convenience method, equivalent to: - # self.app.sync_merge(self, sources) - def sync_merge(*sources) - app.sync_merge(self, sources) - end - - # Convenience method, equivalent to: - # self.app.switch(self, targets, &block) - def switch(*targets, &block) - app.switch(self, targets, &block) - end - # Executes self with the given inputs. Execute provides hooks for subclasses # to insert standard execution code: before_execute, on_execute_error, # and after_execute. Override any/all of these methods as needed. # # Execute passes the inputs to process and returns the result. def execute(*inputs) - before_execute - begin - result = process(*inputs) - rescue - on_execute_error($!) - end - after_execute - - result + _execute(*inputs)._current end # The method for processing inputs into outputs. Override this method in # subclasses to provide class-specific process logic. The number of # arguments specified by process corresponds to the number of arguments @@ -599,10 +548,14 @@ # Hook to set a default task block. By default, nil. def default_task_block nil end + # Hook to define a workflow for defined tasks. + def workflow + end + # Hook to execute code before inputs are processed. def before_execute() end # Hook to execute code after inputs are processed. def after_execute() end @@ -612,9 +565,21 @@ def on_execute_error(err) raise err end private + + def execute_with_callbacks(*inputs) + before_execute + begin + result = process(*inputs) + rescue + on_execute_error($!) + end + after_execute + + result + end def config_task(name, klass=Tap::Task, &block) configs = config[name] || {} raise ArgumentError, "config '#{name}' is not a hash" unless configs.kind_of?(Hash) klass.new(configs, name, &block) \ No newline at end of file