Class: Nanoc3::RulesCollection Private

Inherits:
Object
  • Object
show all
Extended by:
Memoization
Defined in:
lib/nanoc3/base/compilation/rules_collection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Keeps track of the rules in a site.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Memoization

memoize

Constructor Details

#initialize(compiler) ⇒ RulesCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RulesCollection.

Parameters:



29
30
31
32
33
34
35
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 29

def initialize(compiler)
  @compiler = compiler

  @item_compilation_rules  = []
  @item_routing_rules      = []
  @layout_filter_mapping   = OrderedHash.new
end

Instance Attribute Details

#item_compilation_rulesArray<Nanoc3::Rule> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The list of item compilation rules that will be used to compile items.

Returns:

  • (Array<Nanoc3::Rule>)

    The list of item compilation rules that will be used to compile items.



12
13
14
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 12

def item_compilation_rules
  @item_compilation_rules
end

#item_routing_rulesArray<Nanoc3::Rule> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The list of routing rules that will be used to give all items a path.

Returns:

  • (Array<Nanoc3::Rule>)

    The list of routing rules that will be used to give all items a path.



16
17
18
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 16

def item_routing_rules
  @item_routing_rules
end

#layout_filter_mappingHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Hash)

    The layout-to-filter mapping rules



22
23
24
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 22

def layout_filter_mapping
  @layout_filter_mapping
end

#preprocessorProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The code block that will be executed after all data is loaded but before the site is compiled.

Returns:

  • (Proc)

    The code block that will be executed after all data is loaded but before the site is compiled



26
27
28
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 26

def preprocessor
  @preprocessor
end

Instance Method Details

#add_item_compilation_rule(rule, position = :after) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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

Parameters:

  • rule (Nanoc3::Rule)

    The item compilation rule to add

  • position (:before, :after) (defaults to: :after)

    The place where the rule should be added (either at the beginning or the end of the list of rules)



45
46
47
48
49
50
51
52
53
54
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 45

def add_item_compilation_rule(rule, position=:after)
  case position
  when :before
    @item_compilation_rules.unshift(rule)
  when :after
    @item_compilation_rules << rule
  else
    raise "#add_item_routing_rule expected position to be :after or :before"
  end
end

#add_item_routing_rule(rule, position = :after) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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

Parameters:

  • rule (Nanoc3::Rule)

    The item routing rule to add

  • position (:before, :after) (defaults to: :after)

    The place where the rule should be added (either at the beginning or the end of the list of rules)



64
65
66
67
68
69
70
71
72
73
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 64

def add_item_routing_rule(rule, position=:after)
  case position
  when :before
    @item_routing_rules.unshift(rule)
  when :after
    @item_routing_rules << rule
  else
    raise "#add_item_routing_rule expected position to be :after or :before"
  end
end

#checksumString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The checksum for this object. If its contents change, the checksum will change as well.

Returns:

  • (String)

    The checksum for this object. If its contents change, the checksum will change as well.



181
182
183
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 181

def checksum
  @data.checksum
end

#compilation_rule_for(rep) ⇒ Nanoc3::Rule?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

Returns:

  • (Nanoc3::Rule, nil)

    The compilation rule for the given item rep, or nil if no rules have been found



115
116
117
118
119
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 115

def compilation_rule_for(rep)
  @item_compilation_rules.find do |rule|
    rule.applicable_to?(rep.item) && rule.rep_name == rep.name
  end
end

#dslObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the Nanoc3::CompilerDSL that should be used for this site.



167
168
169
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 167

def dsl
  Nanoc3::CompilerDSL.new(self, @compiler.site.config)
end

#filter_for_layout(layout) ⇒ Array?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • layout (Nanoc3::Layout)

    The layout for which to fetch the filter.

Returns:

  • (Array, nil)

    A tuple containing the filter name and the filter arguments for the given layout.



159
160
161
162
163
164
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 159

def filter_for_layout(layout)
  @layout_filter_mapping.each_pair do |layout_identifier, filter_name_and_args|
    return filter_name_and_args if layout.identifier =~ layout_identifier
  end
  nil
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



185
186
187
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 185

def inspect
  "<#{self.class}:0x#{self.object_id.to_s(16)}>"
end

#item_compilation_rules_for(item) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The list of item compilation rules for the given item.

Parameters:

  • item (Nanoc3::Item)

    The item for which the compilation rules should be retrieved

Returns:

  • (Array)

    The list of item compilation rules for the given item



79
80
81
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 79

def item_compilation_rules_for(item)
  @item_compilation_rules.select { |r| r.applicable_to?(item) }
end

#loadvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Loads this site’s rules.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 86

def load
  # Find rules file
  rules_filenames = [ 'Rules', 'rules', 'Rules.rb', 'rules.rb' ]
  rules_filename = rules_filenames.find { |f| File.file?(f) }
  raise Nanoc3::Errors::NoRulesFileFound.new if rules_filename.nil?

  # Get rule data
  @data = File.read(rules_filename)

  # Load DSL
  dsl.instance_eval(@data, "./#{rules_filename}")
end

#new_rule_memory_for_layout(layout) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The rule memory for the given layout.

Parameters:

Returns:

  • (Array)

    The rule memory for the given layout



203
204
205
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 203

def new_rule_memory_for_layout(layout)
  filter_for_layout(layout)
end

#new_rule_memory_for_rep(rep) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The rule memory for the given item representation.

Parameters:

  • rep (Nanoc3::ItemRep)

    The item representation to get the rule memory for

Returns:

  • (Array)

    The rule memory for the given item representation



193
194
195
196
197
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 193

def new_rule_memory_for_rep(rep)
  recording_proxy = rep.to_recording_proxy
  compilation_rule_for(rep).apply_to(recording_proxy, :compiler => @compiler)
  recording_proxy.rule_memory
end

#referenceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Object)

    An unique reference to this object



175
176
177
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 175

def reference
  :rules
end

#routing_rule_for(rep) ⇒ Nanoc3::Rule?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

Returns:

  • (Nanoc3::Rule, nil)

    The routing rule for the given item rep, or nil if no rules have been found



127
128
129
130
131
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 127

def routing_rule_for(rep)
  @item_routing_rules.find do |rule|
    rule.applicable_to?(rep.item) && rule.rep_name == rep.name
  end
end

#routing_rules_for(rep) ⇒ Hash<Symbol, Nanoc3::Rule>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

Returns:



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 141

def routing_rules_for(rep)
  rules = {}
  @item_routing_rules.each do |rule|
    next if !rule.applicable_to?(rep.item)
    next if rule.rep_name != rep.name
    next if rules.has_key?(rule.snapshot_name)

    rules[rule.snapshot_name] = rule
  end
  rules
end

#rule_memory_calculatorNanoc3::RuleMemoryCalculator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The rule memory calculator.

Returns:



223
224
225
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 223

def rule_memory_calculator
  @compiler.rule_memory_calculator
end

#rule_memory_differs_for(obj) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

changed since the last compilation, false otherwise

Parameters:

  • obj (Nanoc3::Item)

    The object for which to check the rule memory

Returns:

  • (Boolean)

    true if the rule memory for the given object has



212
213
214
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 212

def rule_memory_differs_for(obj)
  !rule_memory_store[obj].eql?(rule_memory_calculator[obj])
end

#rule_memory_storeNanoc3::RuleMemoryStore

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The rule memory store.

Returns:



218
219
220
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 218

def rule_memory_store
  @compiler.rule_memory_store
end

#unloadvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Unloads this site’s rules.



102
103
104
105
106
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 102

def unload
  @item_compilation_rules  = []
  @item_routing_rules      = []
  @layout_filter_mapping   = OrderedHash.new
end