Class: Nanoc3::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc3/base/compilation/rule.rb

Overview

Contains the processing information for a item.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • identifier_regex (Regexp)

    A regular expression that will be used to determine whether this rule is applicable to certain items.

  • rep_name (String, Symbol)

    The name of the item representation where this rule can be applied to

  • block (Proc)

    A block that will be called when matching items are compiled

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :snapshot (Symbol, nil) — default: nil

    The name of the snapshot this rule will apply to. Ignored for compilation rules, but used for routing rules.



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_regexRegexp (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.

Returns:

  • (Regexp)

    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_nameSymbol (readonly)

Returns The name of the representation that will be compiled using this rule.

Returns:

  • (Symbol)

    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_nameSymbol (readonly)

Returns The name of the snapshot this rule will apply to. Ignored for compilation rules, but used for routing rules.

Returns:

  • (Symbol)

    The name of the snapshot this rule will apply to. Ignored for compilation rules, but used for routing rules.

Since:

  • 3.2.0



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.

Parameters:

Returns:

  • (Boolean)

    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.

Parameters:

  • rep (Nanoc3::ItemRep)

    The item representation where this rule should be applied to

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

Raises:

  • (ArgumentError)

    if no compiler is passed



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