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:



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

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.



14
15
16
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 14

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.



18
19
20
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 18

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



24
25
26
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 24

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



28
29
30
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 28

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)



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

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)



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

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.



183
184
185
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 183

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



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

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.



169
170
171
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 169

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.



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

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.



187
188
189
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 187

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



81
82
83
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 81

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.



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

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



205
206
207
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 205

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



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

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



177
178
179
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 177

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



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

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:



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

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:



225
226
227
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 225

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



214
215
216
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 214

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:



220
221
222
# File 'lib/nanoc3/base/compilation/rules_collection.rb', line 220

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.



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

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