Class: Nanoc::Rule

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

Overview

Contains the processing information for a item.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

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.

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.



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.

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.



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

Returns:

  • (Symbol)

    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.

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



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

Parameters:

Returns:

  • (Boolean)

    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.

Parameters:

  • rep (Nanoc::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



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