class BatchKit::Definable
Captures details of a definable batch process, e.g. a Task
or Job
.
@abstract
Public Class Methods
Register additional properties to be recorded on this definable.
@param props [Array<Symbol>] The names of properties to be added
to the definition. Used by sub-classes to add to the available properties. This provides a mechanism by which associated Run objects can obtain a list of process properties that they can delegate.
# File lib/batch-kit/framework/definable.rb, line 17 def add_properties(*props) attr_accessor(*props) properties.concat(props) end
When this class is inherited, we need to copy the common property names into the sub-class, since each sub-class needs the common property names defined on this base class, as well as any sub- class specific properties.
# File lib/batch-kit/framework/definable.rb, line 34 def inherited(subclass) subclass.instance_variable_set(:@properties, @properties.clone) end
Create a new instance of this definition.
# File lib/batch-kit/framework/definable.rb, line 69 def initialize @runs = [] end
@return [Array<Symbol>] the names of properties available on this
definition.
# File lib/batch-kit/framework/definable.rb, line 25 def properties @properties ||= [] end
Public Instance Methods
Adds an aspect (as in aspect-oriented programming, or AOP) around the existing instance method mthd_name
on tgt_class
. The aspect does the following:
-
Calls the pre_execute method with the object instance on which the aspect method is being invoked. If the pre_execute method returns false, the method call is skipped; otherwise, proceeds to the next step.
-
Calls the around_execute method, which must yield back at the point at which the wrapped method body should be invoked.
-
Calls the post_execute method with a boolean OK indicator, and the result of the method (if OK) or the exception it threw (if not OK).
@param tgt_class [Class] The class on which the method to be wrapped
is defined.
@param mthd_name [Symbol] The name of the instance method to be
wrapped.
# File lib/batch-kit/framework/definable.rb, line 116 def add_aspect(tgt_class, mthd_name) defn = self mthd = tgt_class.instance_method(mthd_name) tgt_class.class_eval do define_method mthd_name do |*args, &block| run = defn.create_run(self, *args) if run.pre_execute(self, *args) ok = false result = nil begin run.around_execute(self, *args) do result = mthd.bind(self).call(*args, &block) end ok = true run.success(self, result) result rescue Exception => ex run.failure(self, ex) unless ok raise rescue Interrupt run.abort(self) unless ok raise ensure run.post_execute(self, ok) end end end end Events.publish(tgt_class, event_name('defined'), mthd_name) end
Creates an associated Runnable
object for this definition. This method must be overridden in sub-classes.
@param process_obj [Object] The process object instance on which the
process method will be invoked.
@param args [Array<Object>] The arguments to be passed to the process
method.
# File lib/batch-kit/framework/definable.rb, line 155 def create_run(process_obj, *args) raise "Not implemented in #{self.class.name}" end
Returns an event name for publication, based on the sub-class of Runnable
that is triggering the event.
# File lib/batch-kit/framework/definable.rb, line 76 def event_name(event) "#{self.class.name.split('::')[1].downcase}.#{event}" end
Sets properties from an options hash.
@param opts [Hash] A hash containing properties to be set on this
definable.
# File lib/batch-kit/framework/definable.rb, line 85 def set_from_options(opts) unknown = opts.keys - self.class.properties if unknown.size > 0 raise ArgumentError, "The following option(s) are invalid for #{ self.class.name}: #{unknown.join(', ')}. Valid options are: #{ self.class.properties.join(', ')}" end self.class.properties.each do |prop| if opts.has_key?(prop) self.send("#{prop}=", opts[prop]) end end end