Class: Nanoc::RuleDSL::ActionSequenceCalculator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/rule_dsl/action_sequence_calculator.rb

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.

Defined Under Namespace

Classes: NoActionSequenceForItemRepException, NoActionSequenceForLayoutException, PathWithoutInitialSlashError, UnsupportedObjectTypeException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site:, rules_collection:) ⇒ ActionSequenceCalculator

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

Parameters:



38
39
40
41
# File 'lib/nanoc/rule_dsl/action_sequence_calculator.rb', line 38

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.



34
35
36
# File 'lib/nanoc/rule_dsl/action_sequence_calculator.rb', line 34

def rules_collection
  @rules_collection
end

Instance Method Details

#[](obj) ⇒ Nanoc::Core::ActionSequence

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:

  • (Nanoc::Core::ActionSequence)


46
47
48
49
50
51
52
53
54
55
# File 'lib/nanoc/rule_dsl/action_sequence_calculator.rb', line 46

def [](obj)
  case obj
  when Nanoc::Core::ItemRep
    new_action_sequence_for_rep(obj)
  when Nanoc::Core::Layout
    new_action_sequence_for_layout(obj)
  else
    raise UnsupportedObjectTypeException.new(obj)
  end
end

#basic_path_from_rules_for(rep, snapshot_name) ⇒ Object

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.

FIXME: ugly



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nanoc/rule_dsl/action_sequence_calculator.rb', line 131

def basic_path_from_rules_for(rep, snapshot_name)
  routing_rules = @rules_collection.routing_rules_for(rep)
  routing_rule = routing_rules[snapshot_name]
  return nil if routing_rule.nil?

  view_context =
    Nanoc::Core::ViewContextForPreCompilation.new(items: @site.items)

  basic_path =
    routing_rule.apply_to(
      rep,
      site: @site,
      view_context:,
    )

  if basic_path && !basic_path.start_with?('/')
    raise PathWithoutInitialSlashError.new(rep, basic_path)
  end

  basic_path
end

#compact_snapshots(seq) ⇒ Object

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.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/nanoc/rule_dsl/action_sequence_calculator.rb', line 96

def compact_snapshots(seq)
  actions = []
  seq.actions.each do |action|
    if [actions.last, action].all?(Nanoc::Core::ProcessingActions::Snapshot)
      actions[-1] = actions.last.update(snapshot_names: action.snapshot_names, paths: action.paths)
    else
      actions << action
    end
  end
  Nanoc::Core::ActionSequence.new(actions:)
end

#copy_paths_from_routing_rules(seq, snapshots_for_which_to_skip_routing_rule, rep:) ⇒ Object

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/nanoc/rule_dsl/action_sequence_calculator.rb', line 108

def copy_paths_from_routing_rules(seq, snapshots_for_which_to_skip_routing_rule, rep:)
  # NOTE: This assumes that `seq` is compacted, i.e. there are no two consecutive snapshot actions.

  seq.map do |action|
    # Only potentially modify snapshot actions
    next action unless action.is_a?(Nanoc::Core::ProcessingActions::Snapshot)

    # If any of the action’s snapshot are explicitly marked as excluded from
    # getting a path from a routing rule, then ignore routing rules.
    next action if snapshots_for_which_to_skip_routing_rule.intersect?(Set.new(action.snapshot_names))

    # If this action already has paths that don’t come from routing rules,
    # then don’t add more to them.
    next action unless action.paths.empty?

    # For each snapshot name, find a path from a routing rule. The routing
    # rule might return nil, so we need #compact.
    paths = action.snapshot_names.map { |sn| basic_path_from_rules_for(rep, sn) }.compact
    action.update(snapshot_names: [], paths:)
  end
end

#new_action_sequence_for_layout(layout) ⇒ Nanoc::Core::ActionSequence

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:

  • layout (Nanoc::Core::Layout)

Returns:

  • (Nanoc::Core::ActionSequence)


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

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

  unless res
    raise NoActionSequenceForLayoutException.new(layout)
  end

  Nanoc::Core::ActionSequenceBuilder.build do |b|
    b.add_filter(res[0], res[1])
  end
end

#new_action_sequence_for_rep(rep) ⇒ Object

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nanoc/rule_dsl/action_sequence_calculator.rb', line 57

def new_action_sequence_for_rep(rep)
  view_context =
    Nanoc::Core::ViewContextForPreCompilation.new(items: @site.items)

  recorder = Nanoc::RuleDSL::ActionRecorder.new(rep)
  rule = @rules_collection.compilation_rule_for(rep)

  unless rule
    raise NoActionSequenceForItemRepException.new(rep)
  end

  recorder.snapshot(:raw)
  rule.apply_to(rep, recorder:, site: @site, view_context:)
  recorder.snapshot(:post) if recorder.any_layouts?
  recorder.snapshot(:last)
  recorder.snapshot(:pre) unless recorder.pre_snapshot?

  copy_paths_from_routing_rules(
    compact_snapshots(recorder.action_sequence),
    recorder.snapshots_for_which_to_skip_routing_rule,
    rep:,
  )
end