Class: DirectoryTemplate::Processor
- 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
-
#description ⇒ Object
readonly
A human understandable description of the processor.
-
#execute ⇒ Object
readonly
The implementation of the processor.
-
#name ⇒ Object
readonly
A human identifiable name.
-
#pattern ⇒ Object
readonly
The pattern matching proc used to figure whether the processor applies to a ProcessData or not.
-
#pattern_source ⇒ Object
readonly
The source used to create the pattern proc.
Class Method Summary collapse
-
.register(*arguments, &block) ⇒ Processor
Creates a Processor and registers it.
-
.register_all ⇒ Object
Searches for all processors and registers them.
Instance Method Summary collapse
-
#===(process_data) ⇒ Boolean
Whether the processor is suitable for the given ProcessData.
-
#call(process_data) ⇒ DirectoryTemplate::ProcessData
Apply the processor on a ProcessData instance.
-
#initialize(id, pattern, name = nil, description = nil, &execute) ⇒ Processor
constructor
A new instance of Processor.
-
#require(lib) ⇒ Object
Invokes Kernel#require, and fails with a custom error message.
Constructor Details
#initialize(id, pattern, name = nil, description = nil, &execute) ⇒ Processor
Returns a new instance of Processor.
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
#description ⇒ Object (readonly)
A human understandable description of the processor
51 52 53 |
# File 'lib/directory_template/processor.rb', line 51 def description @description end |
#execute ⇒ Object (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 |
#name ⇒ Object (readonly)
A human identifiable name
48 49 50 |
# File 'lib/directory_template/processor.rb', line 48 def name @name end |
#pattern ⇒ Object (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_source ⇒ Object (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.
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_all ⇒ Object
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.
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.
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.
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 |