Class: Nanoc::RulesCollection Private

Inherits:
Object
  • Object
show all
Extended by:
Memoization
Defined in:
lib/nanoc/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

- (RulesCollection) initialize(compiler)

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:



37
38
39
40
41
42
43
44
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 37

def initialize(compiler)
  @compiler = compiler

  @item_compilation_rules = []
  @item_routing_rules     = []
  @layout_filter_mapping  = {}
  @preprocessors          = {}
end

Instance Attribute Details

- (String) data

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 contents of the Rules file

Returns:

  • (String)

    the contents of the Rules file



11
12
13
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 11

def data
  @data
end

- (Array<Nanoc::Rule>) item_compilation_rules (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<Nanoc::Rule>)

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



17
18
19
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 17

def item_compilation_rules
  @item_compilation_rules
end

- (Array<Nanoc::Rule>) item_routing_rules (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<Nanoc::Rule>)

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



21
22
23
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 21

def item_routing_rules
  @item_routing_rules
end

- (Hash) layout_filter_mapping (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



27
28
29
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 27

def layout_filter_mapping
  @layout_filter_mapping
end

- (Hash) preprocessors

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 preprocessor code blocks that will be executed after all data is loaded but before the site is compiled.

Returns:

  • (Hash)

    The hash containing the preprocessor code blocks that will be executed after all data is loaded but before the site is compiled



34
35
36
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 34

def preprocessors
  @preprocessors
end

Instance Method Details

- (void) add_item_compilation_rule(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.

This method returns an undefined value.

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

Parameters:

  • rule (Nanoc::Rule)

    The item compilation rule to add



51
52
53
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 51

def add_item_compilation_rule(rule)
  @item_compilation_rules << rule
end

- (void) add_item_routing_rule(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.

This method returns an undefined value.

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

Parameters:



60
61
62
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 60

def add_item_routing_rule(rule)
  @item_routing_rules << rule
end

- (String) checksum

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.



179
180
181
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 179

def checksum
  Nanoc::Checksummer.calc(self)
end

- (Nanoc::Rule?) compilation_rule_for(rep)

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:

  • (Nanoc::Rule, nil)

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



113
114
115
116
117
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 113

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

- (Object) dsl

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 Nanoc::CompilerDSL that should be used for this site.



165
166
167
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 165

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

- (Array?) filter_for_layout(layout)

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 (Nanoc::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.



157
158
159
160
161
162
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 157

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

- (Object) inspect

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.



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

def inspect
  "<#{self.class}>"
end

- (Array) item_compilation_rules_for(item)

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 (Nanoc::Item)

    The item for which the compilation rules should be retrieved

Returns:

  • (Array)

    The list of item compilation rules for the given item



68
69
70
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 68

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

- (void) load

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.



75
76
77
78
79
80
81
82
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 75

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

  parse(rules_filename)
end

- (Array) make_rule_memory_serializable(rs)

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.

Makes the given rule memory serializable by calling Nanoc::Checksummer#calc on the filter arguments, so that objects such as classes and filenames can be serialized.

Parameters:

  • rs (Array)

    The rule memory for a certain item rep

Returns:

  • (Array)

    The serializable rule memory



206
207
208
209
210
211
212
213
214
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 206

def make_rule_memory_serializable(rs)
  rs.map do |r|
    if r[0] == :filter
      [r[0], r[1], r[2].to_a.map { |a| Nanoc::Checksummer.calc(a) }]
    else
      r
    end
  end
end

- (Array) new_rule_memory_for_layout(layout)

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:

  • layout (Nanoc::Layout)

    The layout to get the rule memory for

Returns:

  • (Array)

    The rule memory for the given layout



219
220
221
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 219

def new_rule_memory_for_layout(layout)
  filter_for_layout(layout)
end

- (Array) new_rule_memory_for_rep(rep)

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 (Nanoc::ItemRep)

    The item representation to get the rule memory for

Returns:

  • (Array)

    The rule memory for the given item representation



191
192
193
194
195
196
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 191

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 << [:write, rep.path]
  make_rule_memory_serializable(recording_proxy.rule_memory)
end

- (Object) parse(rules_filename)

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.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 84

def parse(rules_filename)
  rules_filename = File.absolute_path(rules_filename)

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

  old_rules_filename = dsl.rules_filename
  dsl.rules_filename = rules_filename
  dsl.instance_eval(@data, rules_filename)
  dsl.rules_filename = old_rules_filename
end

- (Object) reference

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



173
174
175
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 173

def reference
  :rules
end

- (Nanoc::Rule?) routing_rule_for(rep)

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:

  • (Nanoc::Rule, nil)

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



125
126
127
128
129
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 125

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

- (Hash<Symbol, Nanoc::Rule>) routing_rules_for(rep)

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:



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 139

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

- (Nanoc::RuleMemoryCalculator) rule_memory_calculator

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:



251
252
253
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 251

def rule_memory_calculator
  @compiler.rule_memory_calculator
end

- (Boolean) rule_memory_differs_for(obj)

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 (Nanoc::Item)

    The object for which to check the rule memory

Returns:

  • (Boolean)

    true if the rule memory for the given object has



240
241
242
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 240

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

- (Nanoc::RuleMemoryStore) rule_memory_store

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:



246
247
248
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 246

def rule_memory_store
  @compiler.rule_memory_store
end

- (Array) snapshots_for(rep)

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 list of snapshots, represented as arrays where the first element is the snapshot name (a Symbol) and the last element is a Boolean indicating whether the snapshot is final or not

Parameters:

  • rep (Nanoc::ItemRep)

    The item representation for which to fetch the list of snapshots

Returns:

  • (Array)

    A list of snapshots, represented as arrays where the first element is the snapshot name (a Symbol) and the last element is a Boolean indicating whether the snapshot is final or not



230
231
232
233
234
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 230

def snapshots_for(rep)
  new_rule_memory_for_rep(rep).select { |e| e[0] == :snapshot }.map do |e|
    [e[1], e[2].fetch(:final) { true }]
  end
end

- (void) unload

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.



99
100
101
102
103
104
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 99

def unload
  @item_compilation_rules = []
  @item_routing_rules     = []
  @layout_filter_mapping  = {}
  @preprocessors          = {}
end