lib/sitefuel/processors/AbstractProcessor.rb in sitefuel-0.0.0b vs lib/sitefuel/processors/AbstractProcessor.rb in sitefuel-0.1.0a
- old
+ new
@@ -1,22 +1,26 @@
#
# File:: AbstractProcessor.rb
# Author:: wkm
-# Copyright:: 2009
+# Copyright:: 2009, Zanoccio LLC.
+# License:: GPL version 2.0 (see LICENSE.rb)
#
# Defines an AbstractProcessor class that gives the interface implemented by
# specific processors.
#
module SiteFuel
module Processor
require 'sitefuel/SiteFuelLogger'
+ require 'sitefuel/processors/Configurable'
# raised when a method isn't implemented by a child class.
class NotImplemented < StandardError; end
+
+
# raised when attempting to run a filter that doesn't exist
class UnknownFilter < StandardError
attr_reader :processor, :name
def initialize(processor, name)
@@ -28,10 +32,12 @@
"'%s' called for processor '%s'" %
[@name, @processor.class]
end
end
+
+
class UnknownFilterset < StandardError
attr_reader :processor, :name
def initialize(processor, name)
@processor = processor
@@ -42,10 +48,12 @@
"'%s' called for processor '%s'" %
[@name, @processor.class]
end
end
+
+
# raised when multiple processors trigger off of a single file
class MultipleApplicableProcessors < StandardError
attr_reader :filename, :processors, :chosen_processor
def initialize(filename, processors, chosen_processor)
@@ -58,18 +66,23 @@
"File '%s' triggered processors: %s. Using %s" %
[@filename, @resource_processors.join(', '), @chosen_processor.class]
end
end
+
+
# defines the base functions every processor must implement to
# interface with the sitefuel architecture
class AbstractProcessor
include SiteFuel::Logging
+ extend SiteFuel::ClassLogging
+ include Configurable
+
# setup an AbstractProcessor
- def initialize
+ def initialize
self.logger = SiteFuelLogger.instance
@execution_list = []
@filters = []
end
@@ -112,10 +125,16 @@
# gives the display name for the processor
def self.processor_name
self.to_s.sub(/.*\b(.*)Processor/, '\1')
end
+ # gives the symbol of the processor; used when showing deployment
+ # progress
+ def processor_symbol
+ 'A'
+ end
+
# gives the file patterns that trigger the processor by default; this
# behavior can be over-ridden by configuration files.
#
# * strings are assumed to be extensions and are tested for a literal match
@@ -281,94 +300,92 @@
else
raise UnknownFilter.new(self, name)
end
end
+
# called in #execute _before_ running the execution list of filters; note
# that #setup_filters is only called _once_ before all of the filters are
# batch executed. It is not called before every filter executes.
def setup_filters; end
+
# called in #execute _after_ running the execution list of filters
def finish_filters; end
+
# runs all filters in the execution list
def execute
setup_filters
@execution_list.uniq.each do |filter|
+ info " Running filter: #{filter}"
run_filter(filter)
end
finish_filters
rescue => exception
error 'from %s:%s: %s' % [self.class, resource_name, exception]
end
+ # creates a file with the given name
+ def create_file(base_file_tree)
+ base_file_tree.get_file(resource_name)
+ end
- def save(basepath)
- File.open(File.join(basepath, resource_name), 'w') do |fhndl|
- fhndl.write(document.to_s)
- end
+ # default save method. This will only create a file, the
+ # more specific abstractions need to implement the actual method
+ def save(base_file_tree)
+ create_file(base_file_tree)
end
-
+ protected
#
- # CONFIGURATION SUPPORT
+ # Configuration Support
#
- def configure(config)
+
+ def pre_configuration
@filters_cleared = false
- unless config == nil or config == {}
- config.each_pair do |k, v|
- set_configuration(k, v)
- end
- end
+ end
- if !@filters_cleared && execution_list.empty?
+ def post_configuration
+ if !@filters_cleared and execution_list.empty?
add_filterset(self.class.default_filterset)
end
- @filters_cleared = false
+ @filters_cleared = true
end
- private
- def set_configuration(key, value)
- case key
- when :resource_name
- @resource_name = value
+ # ensures the filters have been cleared during this
+ # configuration phase. Preforms the #clear_filters if they haven't
+ def ensure_cleared_filters
+ if not @filters_cleared
+ clear_filters
+ @filters_cleared = true
+ end
+ end
- when :filters
- if not @filters_cleared
- clear_filters
- @filters_cleared = true
- end
- case value
- when Array
- value.each { |v| add_filter(v) }
- when Symbol, String
- add_filter(value)
- end
+ # sets the name for the resource that is being processed
+ def configure_resource_name(value)
+ @resource_name = value
+ end
- when :filtersets
- if not @filters_cleared
- clear_filters
- @filters_cleared = true
- end
- case value
- when Array
- value.each { |v| add_filterset(v) }
- when Symbol, String
- add_filterset(value)
- end
+ # adds filters to this processor
+ def configure_filters(*values)
+ ensure_cleared_filters
+ values.each { |v| add_filter(v) }
+ end
- else
- raise UnknownConfigurationOption(self.class, key, value)
- end
+
+ # adds filtersets to this processor
+ def configure_filtersets(*values)
+ ensure_cleared_filters
+ values.each { |v| add_filterset(v) }
end
- protected
+
# gives write-access to children classes
attr_writer :original_size
attr_writer :processed_size
attr_writer :resource_name