lib/tap/support/executable.rb in bahuvrihi-tap-0.10.3 vs lib/tap/support/executable.rb in bahuvrihi-tap-0.10.4
- old
+ new
@@ -1,86 +1,38 @@
require 'tap/support/audit'
+require 'tap/support/dependable'
module Tap
module Support
# Executable wraps methods to make them executable by App. Methods are
# wrapped by extending the object that receives them; the easiest way
# to make an object executable is to use Object#_method.
module Executable
+ extend Dependable
# The method called when an Executable is executed via _execute
attr_reader :_method_name
- # Indicates whether or not to execute in multithread mode.
- attr_accessor :multithread
-
- # Stores the on complete block.
+ # Stores the on complete block
attr_reader :on_complete_block
+ # An array of dependency indexes that will be resolved on _execute
attr_reader :dependencies
public
# Extends obj with Executable and sets up all required variables. The
# specified method will be called on _execute.
- def self.initialize(obj, method_name, multithread=false, &on_complete_block)
+ def self.initialize(obj, method_name, &on_complete_block)
obj.extend Executable
obj.instance_variable_set(:@_method_name, method_name)
- obj.instance_variable_set(:@multithread, multithread)
obj.instance_variable_set(:@on_complete_block, on_complete_block)
obj.instance_variable_set(:@dependencies, [])
obj
end
-
- def self.clear_dependencies
- @registry = []
- @results = []
- end
- def self.registry
- @registry
- end
-
- def self.results
- @results
- end
-
- def self.index(instance, args)
- @registry.each_with_index do |entry, index|
- return index if entry[0] == instance && entry[1] == args
- end
- nil
- end
-
- def self.resolved?(index)
- @results[index] != nil
- end
-
- def self.resolve(indicies)
- indicies.each do |index|
- next if @results[index]
- instance, inputs = @registry[index]
- @results[index] = instance._execute(*inputs)
- end
- end
-
- def self.reset(indicies)
- indicies.each {|index| @results[index] = nil }
- end
-
- def self.register(instance, args)
- if existing = index(instance, args)
- return existing
- end
-
- @registry << [instance, args]
- @registry.length - 1
- end
-
- clear_dependencies
-
# Sets a block to receive the results of _execute. Raises an error
# if an on_complete block is already set. Override an existing
# on_complete block by specifying override = true.
#
# Note the block recieves an audited result and not
@@ -91,28 +43,30 @@
end
@on_complete_block = block
end
# Adds the dependency to self, making self dependent on the dependency.
- # The dependency will be called with the input arguments during
- # resolve_dependencies.
+ # The dependency will be resolved by calling dependency._execute with
+ # the input arguments during resolve_dependencies.
def depends_on(dependency, *inputs)
raise ArgumentError, "not an Executable: #{dependency}" unless dependency.kind_of?(Executable)
raise ArgumentError, "cannot depend on self" if dependency == self
index = Executable.register(dependency, inputs)
dependencies << index unless dependencies.include?(index)
index
end
- # Resolves dependencies by calling dependency.resolve with
- # the dependency arguments.
+ # Resolves dependencies by calling dependency._execute with
+ # the dependency arguments. (See Dependable#resolve).
def resolve_dependencies
Executable.resolve(dependencies)
self
end
+ # Resets dependencies so they will be re-resolved on resolve_dependencies.
+ # (See Dependable#reset).
def reset_dependencies
Executable.reset(dependencies)
self
end
@@ -169,22 +123,22 @@
#
# array = []
# push_to_array = array._method(:push)
#
# task = Tap::Task.new
-# task.app.sequence(task, push_to_array)
+# task.sequence(push_to_array)
#
# task.enq(1).enq(2,3)
# task.app.run
#
# array # => [[1],[2,3]]
#
class Object
# Initializes a Tap::Support::Executable using the Method returned by
- # Object#method(method_name), setting multithread and the on_complete
- # block as specified. Returns nil if Object#method returns nil.
- def _method(method_name, multithread=false, &on_complete_block) # :yields: _result
+ # Object#method(method_name), setting the on_complete block as specified.
+ # Returns nil if Object#method returns nil.
+ def _method(method_name, &on_complete_block) # :yields: _result
return nil unless m = method(method_name)
- Tap::Support::Executable.initialize(m, :call, multithread, &on_complete_block)
+ Tap::Support::Executable.initialize(m, :call, &on_complete_block)
end
end
\ No newline at end of file