class Nanoc::RuleDSL::RulesCollection

Keeps track of the rules in a site.

@api private

Attributes

data[RW]

@return [String] the contents of the Rules file

layout_filter_mapping[R]

The hash containing layout-to-filter mapping rules. This hash is ordered: iterating over the hash will happen in insertion order.

@return [Hash] The layout-to-filter mapping rules

postprocessors[RW]

The hash containing postprocessor code blocks that will be executed after

all data is loaded and the site is compiled.

@return [Hash] The hash containing the postprocessor code blocks that will

be executed after all data is loaded and the site is compiled
preprocessors[RW]

The hash containing preprocessor code blocks that will be executed after

all data is loaded but before the site is compiled.

@return [Hash] The hash containing the preprocessor code blocks that will

be executed after all data is loaded but before the site is compiled

Public Class Methods

new() click to toggle source
# File lib/nanoc/rule_dsl/rules_collection.rb, line 31
def initialize
  @item_compilation_rules = []
  @item_routing_rules     = []
  @layout_filter_mapping  = {}
  @preprocessors          = {}
  @postprocessors         = {}
end

Public Instance Methods

add_item_compilation_rule(rule) click to toggle source

Add the given rule to the list of item compilation rules.

@param [Nanoc::Int::Rule] rule The item compilation rule to add

@return [void]

# File lib/nanoc/rule_dsl/rules_collection.rb, line 44
def add_item_compilation_rule(rule)
  @item_compilation_rules << rule
end
add_item_routing_rule(rule) click to toggle source

Add the given rule to the list of item routing rules.

@param [Nanoc::Int::Rule] rule The item routing rule to add

@return [void]

# File lib/nanoc/rule_dsl/rules_collection.rb, line 53
def add_item_routing_rule(rule)
  @item_routing_rules << rule
end
compilation_rule_for(rep) click to toggle source

Finds the first matching compilation rule for the given item representation.

@param [Nanoc::Core::ItemRep] rep The item rep for which to fetch the rule

@return [Nanoc::Int::Rule, nil] The compilation rule for the given item rep,

or nil if no rules have been found
# File lib/nanoc/rule_dsl/rules_collection.rb, line 72
def compilation_rule_for(rep)
  @item_compilation_rules.find do |rule|
    rule.applicable_to?(rep.item) && rule.rep_name == rep.name
  end
end
filter_for_layout(layout) click to toggle source

Finds the filter name and arguments to use for the given layout.

@param [Nanoc::Core::Layout] layout The layout for which to fetch the filter.

@return [Array, nil] A tuple containing the filter name and the filter

arguments for the given layout.
# File lib/nanoc/rule_dsl/rules_collection.rb, line 104
def filter_for_layout(layout)
  @layout_filter_mapping.each_pair do |pattern, filter_name_and_args|
    return filter_name_and_args if pattern.match?(layout.identifier)
  end
  nil
end
inspect() click to toggle source
# File lib/nanoc/rule_dsl/rules_collection.rb, line 118
def inspect
  "<#{self.class}>"
end
item_compilation_rules_for(item) click to toggle source

@param [Nanoc::Core::Item] item The item for which the compilation rules

should be retrieved

@return [Array] The list of item compilation rules for the given item

# File lib/nanoc/rule_dsl/rules_collection.rb, line 61
def item_compilation_rules_for(item)
  @item_compilation_rules.select { |r| r.applicable_to?(item) }
end
reference() click to toggle source

Returns an object that can be used for uniquely identifying objects.

@return [Object] An unique reference to this object

# File lib/nanoc/rule_dsl/rules_collection.rb, line 114
def reference
  'rules'
end
routing_rules_for(rep) click to toggle source

Returns the list of routing rules that can be applied to the given item representation. For each snapshot, the first matching rule will be returned. The result is a hash containing the corresponding rule for each snapshot.

@param [Nanoc::Core::ItemRep] rep The item rep for which to fetch the rules

@return [Hash<Symbol, Nanoc::Int::Rule>] The routing rules for the given rep

# File lib/nanoc/rule_dsl/rules_collection.rb, line 86
def routing_rules_for(rep)
  rules = {}
  @item_routing_rules.each do |rule|
    next unless rule.applicable_to?(rep.item)
    next if rule.rep_name != rep.name
    next if rules.key?(rule.snapshot_name)

    rules[rule.snapshot_name] = rule
  end
  rules
end