Class: Nanoc::Rule
- Inherits:
-
Object
- Object
- Nanoc::Rule
- Defined in:
- lib/nanoc/base/compilation/rule.rb
Overview
Contains the processing information for a item.
Instance Attribute Summary (collapse)
-
- (Regexp) identifier_regex
readonly
The regex that determines which items this rule can be applied to.
-
- (Symbol) rep_name
readonly
The name of the representation that will be compiled using this rule.
-
- (Symbol) snapshot_name
readonly
The name of the snapshot this rule will apply to.
Instance Method Summary (collapse)
-
- (Boolean) applicable_to?(item)
True if this rule can be applied to the given item rep, false otherwise.
-
- (void) apply_to(rep, params = {})
Applies this rule to the given item rep.
-
- (Rule) initialize(identifier_regex, rep_name, block, params = {})
constructor
Creates a new item compilation rule with the given identifier regex, compiler and block.
Constructor Details
- (Rule) initialize(identifier_regex, rep_name, block, params = {})
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.
37 38 39 40 41 42 43 |
# File 'lib/nanoc/base/compilation/rule.rb', line 37 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
- (Regexp) identifier_regex (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.
9 10 11 |
# File 'lib/nanoc/base/compilation/rule.rb', line 9 def identifier_regex @identifier_regex end |
- (Symbol) rep_name (readonly)
Returns The name of the representation that will be compiled using this rule
13 14 15 |
# File 'lib/nanoc/base/compilation/rule.rb', line 13 def rep_name @rep_name end |
- (Symbol) snapshot_name (readonly)
Returns The name of the snapshot this rule will apply to. Ignored for compilation rules, but used for routing rules.
19 20 21 |
# File 'lib/nanoc/base/compilation/rule.rb', line 19 def snapshot_name @snapshot_name end |
Instance Method Details
- (Boolean) applicable_to?(item)
Returns true if this rule can be applied to the given item rep, false otherwise
49 50 51 |
# File 'lib/nanoc/base/compilation/rule.rb', line 49 def applicable_to?(item) item.identifier =~ @identifier_regex end |
- (void) apply_to(rep, params = {})
This method returns an undefined value.
Applies this rule to the given item rep.
63 64 65 66 67 68 69 70 |
# File 'lib/nanoc/base/compilation/rule.rb', line 63 def apply_to(rep, params = {}) compiler = params.fetch(:compiler) do raise ArgumentError, 'Required :compiler option is missing' end rep = Nanoc::ItemRepProxy.new(rep, compiler) unless rep.proxy? context = Nanoc::RuleContext.new(rep: rep, compiler: compiler) context.instance_exec(matches(rep.item.identifier), &@block) end |