Class: Nanoc::RuleDSL::RuleMemoryCalculator Private

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

Calculates rule memories for objects that can be run through a rule (item representations and layouts).

Defined Under Namespace

Classes: NoRuleMemoryForItemRepException, NoRuleMemoryForLayoutException, UnsupportedObjectTypeException

Constant Summary

Constants included from Int::Memoization

Int::Memoization::NONE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Int::Memoization

memoize

Constructor Details

#initialize(site:, rules_collection:) ⇒ 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 a new instance of RuleMemoryCalculator.

Parameters:



32
33
34
35
# File 'lib/nanoc/rule_dsl/rule_memory_calculator.rb', line 32

def initialize(site:, rules_collection:)
  @site = site
  @rules_collection = rules_collection
end

Instance Attribute Details

#rules_collectionObject

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.



28
29
30
# File 'lib/nanoc/rule_dsl/rule_memory_calculator.rb', line 28

def rules_collection
  @rules_collection
end

Instance Method Details

#[](obj) ⇒ Nanoc::Int::RuleMemory

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.

Parameters:

  • obj (#reference)

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nanoc/rule_dsl/rule_memory_calculator.rb', line 40

def [](obj)
  # FIXME: Remove this
  obj = obj.unwrap if obj.respond_to?(:unwrap)

  case obj
  when Nanoc::Int::ItemRep
    new_rule_memory_for_rep(obj)
  when Nanoc::Int::Layout
    new_rule_memory_for_layout(obj)
  else
    raise UnsupportedObjectTypeException.new(obj)
  end
end

#new_rule_memory_for_layout(layout) ⇒ Nanoc::Int::RuleMemory

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.

Parameters:

Returns:



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nanoc/rule_dsl/rule_memory_calculator.rb', line 98

def new_rule_memory_for_layout(layout)
  res = @rules_collection.filter_for_layout(layout)

  unless res
    raise NoRuleMemoryForLayoutException.new(layout)
  end

  Nanoc::Int::RuleMemory.new(layout).tap do |rm|
    rm.add_filter(res[0], res[1])
  end
end

#new_rule_memory_for_rep(rep) ⇒ Nanoc::Int::RuleMemory

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.

Parameters:

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nanoc/rule_dsl/rule_memory_calculator.rb', line 71

def new_rule_memory_for_rep(rep)
  dependency_tracker = Nanoc::Int::DependencyTracker::Null.new
  view_context = @site.compiler.create_view_context(dependency_tracker)

  executor = Nanoc::RuleDSL::RecordingExecutor.new(rep, @rules_collection, @site)
  rule = @rules_collection.compilation_rule_for(rep)

  unless rule
    raise NoRuleMemoryForItemRepException.new(rep)
  end

  executor.snapshot(rep, :raw)
  executor.snapshot(rep, :pre, final: false)
  rule.apply_to(rep, executor: executor, site: @site, view_context: view_context)
  if executor.rule_memory.any_layouts?
    executor.snapshot(rep, :post)
  end
  unless executor.rule_memory.snapshot_actions.any? { |sa| sa.snapshot_name == :last }
    executor.snapshot(rep, :last)
  end

  executor.rule_memory
end

#snapshots_defs_for(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 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::Int::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



61
62
63
64
65
# File 'lib/nanoc/rule_dsl/rule_memory_calculator.rb', line 61

def snapshots_defs_for(rep)
  self[rep].snapshot_actions.map do |a|
    Nanoc::Int::SnapshotDef.new(a.snapshot_name, a.final?)
  end
end