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:



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

def initialize(compiler)
  @compiler = compiler

  @item_compilation_rules  = []
  @item_routing_rules      = []
  @layout_filter_mapping   = 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

- (Proc) preprocessor

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



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

def preprocessor
  @preprocessor
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



49
50
51
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 49

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:



58
59
60
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 58

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.



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

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



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

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.



154
155
156
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 154

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.



146
147
148
149
150
151
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 146

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.



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

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



66
67
68
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 66

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.



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

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?

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

  # Load DSL
  dsl.instance_eval(@data, "./#{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



195
196
197
198
199
200
201
202
203
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 195

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



208
209
210
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 208

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



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

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) 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



162
163
164
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 162

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



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

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:



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 128

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:



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

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



229
230
231
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 229

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:



235
236
237
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 235

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



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

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.



89
90
91
92
93
# File 'lib/nanoc/base/compilation/rules_collection.rb', line 89

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