Class: DirectoryTemplate::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/directory_template/processor.rb,
lib/directory_template/processor/erb.rb,
lib/directory_template/processor/stop.rb,
lib/directory_template/processor/format.rb,
lib/directory_template/processor/markdown.rb

Overview

The definition of a processor

Use Processor.register to register a processor. A registered processor is available in all DirectoryTemplate instances.

Processor::register takes the same arguments as Processor::new, so look there for how to define a processor.

Take a look at ProcessData to see what data your processor gets and can work on.

Constant Summary collapse

Never =

A matcher-proc to never match

proc { |data| false }
Erb =

The ERB Processor treats the file-content as ERB template.

Processor.register(:erb, '*.erb', 'ERB Template Processor') do |data|
  data.content = ErbTemplate.new(data.content).result(data.content_variables)
  data.chomp_suffix!
end
Stop =

The

Processor.register(:stop, '*.stop', 'Terminate processing queue', 'After .stop, no processor will be run anymore') do |data|
  data.chomp_suffix!
  throw :stop_processing
end
Format =

The standard processor for file- and directory-paths. It simply uses String#% style keyword replacement. I.e., ‘%key` is replaced by the variable value passed with :key.

Processor.new(:format, nil, '%{variable} format processor') do |data|
  data.path = data.path % data.path_variables if data.path_variables
end
Markdown =

The ERB Processor treats the file-content as ERB template.

Processor.register(:markdown_to_html, '*.html.markdown', 'Markdown to HTML Template Processor') do |data|
  Markdown.require 'kramdown'
  data.content = Kramdown::Document.new(data.content).to_html
  data.chomp_suffix!
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, pattern, name = nil, description = nil, &execute) ⇒ Processor

Returns a new instance of Processor.

Parameters:

  • id (Symbol)

    A (in the set of Processors) unique id

  • pattern (String, Regexp, Proc, #to_proc, nil)

    The pattern determines upon what DirectoryTemplate::ProcessData this processor is being invoked.

    If you provide a String, it is interpreted as a glob-like-pattern, e.g. ‘*.html.haml’ will match any files whose suffix is ‘.html.haml’. See File::fnmatch for an extensive documentation of glob-patterns.

    If you provide a Regexp, the filename is matched against that regexp.

    If you provide a Proc (or anything that is converted to a proc), the proc gets the ProcessData as sole argument and should return true/false, to indicate, whether the Processor should be invoked or not.

    If you pass nil as pattern, the Processor will never be invoked. This is useful for processors that serve only as path processors.

  • name (String) (defaults to: nil)

    The name of the processor

  • description (String) (defaults to: nil)

    A description, what the processor does

  • execute (#call)

    The implementation of the processor

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/directory_template/processor.rb', line 83

def initialize(id, pattern, name=nil, description=nil, &execute)
  raise ArgumentError, "ID must be a Symbol" unless id.is_a?(Symbol)
  @id             = id
  @pattern_source = pattern
  @pattern        = case pattern
    when String then proc { |data| File.fnmatch?(pattern, data.path) }
    when Regexp then proc { |data| pattern =~ data.path }
    when Proc   then pattern
    when nil    then Never
    else
      raise ArgumentError, "Expected a String, Regexp or Proc as pattern, but got #{pattern.class}"
  end
  @name         = name
  @description  = description
  @execute      = execute
end

Instance Attribute Details

#descriptionObject (readonly)

A human understandable description of the processor



51
52
53
# File 'lib/directory_template/processor.rb', line 51

def description
  @description
end

#executeObject (readonly)

The implementation of the processor. I.e., the block passed to ::new.



54
55
56
# File 'lib/directory_template/processor.rb', line 54

def execute
  @execute
end

#nameObject (readonly)

A human identifiable name



48
49
50
# File 'lib/directory_template/processor.rb', line 48

def name
  @name
end

#patternObject (readonly)

The pattern matching proc used to figure whether the processor applies to a ProcessData or not.



41
42
43
# File 'lib/directory_template/processor.rb', line 41

def pattern
  @pattern
end

#pattern_sourceObject (readonly)

The source used to create the pattern proc. I.e., the value passed to ::new as the pattern parameter.



45
46
47
# File 'lib/directory_template/processor.rb', line 45

def pattern_source
  @pattern_source
end

Class Method Details

.register(*arguments, &block) ⇒ Processor

Creates a Processor and registers it. The arguments are passed verbatim to Processor::new.

Returns:



32
33
34
35
36
37
# File 'lib/directory_template/processor.rb', line 32

def self.register(*arguments, &block)
  processor = new(*arguments, &block)
  DirectoryTemplate.register(processor)

  processor
end

.register_allObject

Searches for all processors and registers them



20
21
22
23
24
25
26
# File 'lib/directory_template/processor.rb', line 20

def self.register_all
  $LOAD_PATH.each do |path|
    Dir.glob(File.join(path, 'directory_template', 'processor', '**', '*.rb')) do |processor|
      require processor
    end
  end
end

Instance Method Details

#===(process_data) ⇒ Boolean

Returns Whether the processor is suitable for the given ProcessData.

Returns:

  • (Boolean)

    Whether the processor is suitable for the given ProcessData



102
103
104
# File 'lib/directory_template/processor.rb', line 102

def ===(process_data)
  @pattern.call(process_data)
end

#call(process_data) ⇒ DirectoryTemplate::ProcessData

Apply the processor on a ProcessData instance.

Parameters:

Returns:



113
114
115
116
117
# File 'lib/directory_template/processor.rb', line 113

def call(process_data)
  @execute.call(process_data)

  process_data
end

#require(lib) ⇒ Object

Invokes Kernel#require, and fails with a custom error message.

See Also:

  • Kernel#require


121
122
123
124
125
# File 'lib/directory_template/processor.rb', line 121

def require(lib)
  super(lib)
rescue LoadError
  raise "The #{@name || @id} processor requires #{lib} in order to work"
end