require 'log' require 'forwardable' module MediaShelf module Shovel #This class represents a "workflow" for a file contained in an ingest file being #processed by Shovel #See +Shovel+ for more information and an example class Flow extend Forwardable def_delegators Log.instance, :d, :e, :w, :i attr_reader :reaction_map, :exception_map def initialize #:nodoc: @reaction_map={:stable=>[], :added=>[], :removed=>[], :modified=>[]} @exception_map = {} end #add a process to the flow, to be executed by the Shovel when the #supplied event occurrs. # #event:: the event type, one of :stable, :added, :removed or :modified #block:: your process. the process will be passed hte file extracted \ #from your archive (most likely). # #This method (and on_exception) returns self, so calls #to add_reaction can be chained. def add_reaction(event, &block) @reaction_map[event] << block return self end #add a process to the flow, to be executed by the Shovel when the #supplied Exception occurrs. #klazz:: a class which might be raised at some point in the ingest process #block:: what you want to do about it. The block is passed the file, \ #and the exception instance. # #This method (and add_reaction) returns self, so calls #to add_reaction can be chained. def on_exception(klazz, &block) @exception_map[klazz.name.to_sym]=block return self end #The exception handler. It will try to find processes mapped to #the given exception, and invoke on them. If it can't find a handler #it just logs the exception and bails. def handle_exception(f,x) if @exception_map[x.class.name.to_sym] p = @exception_map[x.class.name.to_sym] p.call(f, x) else log_exception(f,x) end end def log_exception(f,x)#:nodoc: e("Error : #{f} #{x.message}. Stack trace follows:") e(x.backtrace.join("\n")) end end end end