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:



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

def initialize(compiler)
  @compiler = compiler

  @item_compilation_rules = []
  @item_routing_rules     = []
  @layout_filter_mapping  = OrderedHash.new
  @preprocessors          = OrderedHash.new
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



13
14
15
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 13

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.



19
20
21
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 19

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.



23
24
25
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 23

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



29
30
31
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 29

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



36
37
38
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 36

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



53
54
55
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 53

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:



62
63
64
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 62

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.



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

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



115
116
117
118
119
# File 'lib/nanoc/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

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



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

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.



159
160
161
162
163
164
# File 'lib/nanoc/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

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



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

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



70
71
72
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 70

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.



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

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 #inspect 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



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

def make_rule_memory_serializable(rs)
  rs.map do |r|
    if r[0] == :filter
      [ r[0], r[1], r[2].to_a.map { |a| a.inspect }  ]
    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



221
222
223
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 221

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



193
194
195
196
197
198
# File 'lib/nanoc/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 << [ :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.



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

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



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

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



127
128
129
130
131
# File 'lib/nanoc/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

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



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nanoc/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.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:



253
254
255
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 253

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



242
243
244
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 242

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:



248
249
250
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 248

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



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

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.



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

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