Class: SiteFuel::Processor::AbstractProcessor

Inherits:
Object
  • Object
show all
Extended by:
ClassLogging
Includes:
Logging, Configurable
Defined in:
lib/sitefuel/processors/AbstractProcessor.rb

Overview

defines the base functions every processor must implement to interface with the sitefuel architecture

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassLogging

debug, error, fatal, info, warn

Methods included from Configurable

#configuration_options, #configure, #ensure_configurable_option, #set_configuration

Methods included from Logging

#debug, #error, #fatal, #info, #logger=, #warn

Constructor Details

#initializeAbstractProcessor

setup an AbstractProcessor



83
84
85
86
87
88
89
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 83

def initialize        
  self.logger = SiteFuelLogger.instance
  @execution_list = []
  @filters = []

  @resource_name = nil
end

Instance Attribute Details

#execution_listObject (readonly)

array of filters to run



266
267
268
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 266

def execution_list
  @execution_list
end

#original_sizeObject

gives the original size of a resource before being processed



122
123
124
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 122

def original_size
  @original_size
end

#processed_sizeObject

gives the size of the resouce now that it’s been processed



125
126
127
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 125

def processed_size
  @processed_size
end

#resource_nameObject

gives the canonical name of the resource



119
120
121
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 119

def resource_name
  @resource_name
end

Class Method Details

.default_filtersetObject

gives the default filterset used



180
181
182
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 180

def self.default_filterset
  nil
end

.file_pattern_match?(filename) ⇒ Boolean

gives true if filename matches one of #file_patterns.

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 150

def self.file_pattern_match?(filename)
  file_patterns.map { |patt|
    case patt
    when String
        regex = Regexp.new("^.*"+Regexp.escape(patt)+"$", Regexp::IGNORECASE)
        return true if filename.match(regex) != nil
    when Regexp
        return true if filename.match(patt) != nil
    end
  }
  
  # if we got this far nothing matched
  return false
end

.file_patternsObject

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

  • regexes are tested against the entire file name



145
146
147
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 145

def self.file_patterns
  []
end

.filter?(name) ⇒ Boolean

gives true if the given filter is known for this processor class

Returns:

  • (Boolean)


256
257
258
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 256

def self.filter?(name)
  filters.include?(name.to_sym)
end

.filtersObject

lists all the filters implemented by a processor



247
248
249
250
251
252
253
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 247

def self.filters
  names = instance_methods
  names = names.delete_if{ |method| not method =~ /^filter_.*$/ }
  names.sort!
  
  names.map { |filter_name| filter_name.sub(/^filter_(.*)$/, '\1').to_sym }
end

.filters_in_filterset(name) ⇒ Object

returns the filters in the given filter set, [] if no such filters exist



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 206

def self.filters_in_filterset(name)
  return [] unless self.filterset?(name)

  filter_list = self.send("filterset_" + name.to_s)
  
  if filter_list == nil
    return []
  else
    return filter_list
  end
end

.filterset?(name) ⇒ Boolean

gives true if the given name is of a filter set for this processor

Returns:

  • (Boolean)


194
195
196
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 194

def self.filterset?(name)
  respond_to?("filterset_" + name.to_s)
end

.filterset_ignoreObject

the ignore filter set is used when configuring sitefuel to not process certain kinds of files.



200
201
202
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 200

def self.filterset_ignore
  []
end

.filtersetsObject

lists all filtersets for this processor



185
186
187
188
189
190
191
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 185

def self.filtersets
  names = methods
  names = names.delete_if{|method| not method =~ /^filterset_.*$/ }
  names.sort!

  names.map { |filterset| filterset.sub(/^filterset_(.*)$/, '\1').to_sym }
end

.find_processorsObject

gives a list of processors that implement AbstractProcessor



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 93

def self.find_processors
  procs = []
  ObjectSpace.each_object(Class) do |cls|
    if cls.ancestors.include?(self) and
       cls.to_s =~ /^.*Processor$/ and
       not cls.to_s =~ /^.*Abstract.*Processor$/
    then
      procs << cls
    end
  end

  procs
end

.processes_file?(filename) ⇒ Boolean

uses #file_pattern_match? to decide if the file can be processed eventually this may use other metrics (like mime types)

Returns:

  • (Boolean)


168
169
170
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 168

def self.processes_file?(filename)
  file_pattern_match? filename
end

.processor_nameObject

gives the display name for the processor



128
129
130
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 128

def self.processor_name
  self.to_s.sub(/.*\b(.*)Processor/, '\1')
end

.processor_typeObject

gives the type of the processor, usually implemented by the more specific abstract processors.



109
110
111
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 109

def self.processor_type
  ''
end

Instance Method Details

#add_filter(filter) ⇒ Object

adds a filter or array of filters to the execution list

add_filter(:minify)
add_filter([:beautifytext, :minify])


272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 272

def add_filter(filter)
  case filter
  when Array
    filter.each do |f|
      add_filter f
    end
  when Symbol, String
    if filter?(filter)
      @execution_list << filter
    else
      raise UnknownFilter.new(self, filter)
    end
  end
end

#add_filterset(filterset) ⇒ Object

adds the filters in a filterset to the execution list



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 219

def add_filterset(filterset)
  if self.class.filterset?(filterset)
    # extract the filters in the filterset and add them to the list
    filter_list = self.class.filters_in_filterset(filterset)
    filter_list.each do |filter|
      add_filter(filter)
    end
    @execution_list
  else
    raise UnknownFilterset.new(self, filterset)
  end
end

#clear_filtersObject

clears all filters from the execution list



288
289
290
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 288

def clear_filters
  @execution_list = []
end

#create_file(base_file_tree) ⇒ Object

creates a file with the given name



332
333
334
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 332

def create_file(base_file_tree)
  base_file_tree.get_file(resource_name)
end

#drop_filter(filter) ⇒ Object

drops a filter from the execution list



293
294
295
296
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 293

def drop_filter(filter)
  @execution_list.delete(filter)
  @execution_list
end

#executeObject

runs all filters in the execution list



319
320
321
322
323
324
325
326
327
328
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 319

def execute
  setup_filters
  @execution_list.uniq.each do |filter|
    info "\t\tRunning filter: #{filter}"
    run_filter(filter)
  end
  finish_filters
rescue => exception
  error 'from %s:%s: %s' % [self.class, resource_name, exception]
end

#filter?(filter) ⇒ Boolean

gives true if the given filter is known for this processor instance

Returns:

  • (Boolean)


261
262
263
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 261

def filter?(filter)
  respond_to?("filter_" + filter.to_s)
end

#finish_filtersObject

called in #execute after running the execution list of filters



315
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 315

def finish_filters; end

#processor_symbolObject

gives the symbol of the processor; used when showing deployment progress



134
135
136
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 134

def processor_symbol
  'A'
end

#run_filter(name) ⇒ Object

runs a particular filter



299
300
301
302
303
304
305
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 299

def run_filter(name)
  if respond_to?("filter_" + name.to_s)
    self.send("filter_"+name.to_s)
  else
    raise UnknownFilter.new(self, name)
  end
end

#run_filterset(name) ⇒ Object

evaluate a filterset



233
234
235
236
237
238
239
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 233

def run_filterset(name)
  if self.class.filter_set?("filterset_" + name.to_s)
    self.send("filterset_" + name.to_s)
  else
    raise UnknownFilterset(self, name)
  end
end

#save(base_file_tree) ⇒ Object

default save method. This will only create a file, the more specific abstractions need to implement the actual method



339
340
341
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 339

def save(base_file_tree)
  create_file(base_file_tree)
end

#setup_filtersObject

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.



311
# File 'lib/sitefuel/processors/AbstractProcessor.rb', line 311

def setup_filters; end