Class: Nanoc3::Rule
- Inherits:
-
Object
- Object
- Nanoc3::Rule
- Defined in:
- lib/nanoc3/base/compilation/rule.rb
Overview
Contains the processing information for a item.
Instance Attribute Summary collapse
-
#identifier_regex ⇒ Regexp
readonly
The regex that determines which items this rule can be applied to.
-
#rep_name ⇒ Symbol
readonly
The name of the representation that will be compiled using this rule.
-
#snapshot_name ⇒ Symbol
readonly
The name of the snapshot this rule will apply to.
Instance Method Summary collapse
-
#applicable_to?(item) ⇒ Boolean
True if this rule can be applied to the given item rep, false otherwise.
-
#apply_to(rep, params = {}) ⇒ void
Applies this rule to the given item rep.
-
#initialize(identifier_regex, rep_name, block, params = {}) ⇒ Rule
constructor
Creates a new item compilation rule with the given identifier regex, compiler and block.
Constructor Details
#initialize(identifier_regex, rep_name, block, params = {}) ⇒ Rule
Creates a new item compilation rule with the given identifier regex, compiler and block. The block will be called during compilation with the item rep as its argument.
39 40 41 42 43 44 45 |
# File 'lib/nanoc3/base/compilation/rule.rb', line 39 def initialize(identifier_regex, rep_name, block, params={}) @identifier_regex = identifier_regex @rep_name = rep_name.to_sym @snapshot_name = params[:snapshot_name] @block = block end |
Instance Attribute Details
#identifier_regex ⇒ Regexp (readonly)
Returns The regex that determines which items this rule can be applied to. This rule can be applied to items with a identifier matching this regex.
11 12 13 |
# File 'lib/nanoc3/base/compilation/rule.rb', line 11 def identifier_regex @identifier_regex end |
#rep_name ⇒ Symbol (readonly)
Returns The name of the representation that will be compiled using this rule.
15 16 17 |
# File 'lib/nanoc3/base/compilation/rule.rb', line 15 def rep_name @rep_name end |
#snapshot_name ⇒ Symbol (readonly)
Returns The name of the snapshot this rule will apply to. Ignored for compilation rules, but used for routing rules.
21 22 23 |
# File 'lib/nanoc3/base/compilation/rule.rb', line 21 def snapshot_name @snapshot_name end |
Instance Method Details
#applicable_to?(item) ⇒ Boolean
Returns true if this rule can be applied to the given item rep, false otherwise.
51 52 53 |
# File 'lib/nanoc3/base/compilation/rule.rb', line 51 def applicable_to?(item) item.identifier =~ @identifier_regex end |
#apply_to(rep, params = {}) ⇒ void
This method returns an undefined value.
Applies this rule to the given item rep.
65 66 67 68 69 |
# File 'lib/nanoc3/base/compilation/rule.rb', line 65 def apply_to(rep, params={}) compiler = params[:compiler] or raise ArgumentError, "Required :compiler option is missing" rep = Nanoc3::ItemRepProxy.new(rep, compiler) unless rep.is_proxy? Nanoc3::RuleContext.new(:rep => rep, :compiler => compiler).instance_eval &@block end |