Skip to content

Commit

Permalink
Allow Snapshot action to have multiple paths
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Feb 17, 2017
1 parent 178c58e commit 3df9ade
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 55 deletions.
18 changes: 9 additions & 9 deletions lib/nanoc/base/entities/processing_actions/snapshot.rb
Expand Up @@ -6,29 +6,29 @@ class Snapshot < Nanoc::Int::ProcessingAction
include Nanoc::Int::ContractsSupport

attr_reader :snapshot_names
attr_reader :path
attr_reader :paths

contract C::IterOf[Symbol], C::Maybe[String] => C::Any
def initialize(snapshot_names, path)
contract C::IterOf[Symbol], C::IterOf[String] => C::Any
def initialize(snapshot_names, paths)
@snapshot_names = snapshot_names
@path = path
@paths = paths
end

contract C::None => Array
def serialize
[:snapshot, @snapshot_names, true, @path]
[:snapshot, @snapshot_names, true, @paths]
end

NONE = Object.new

contract C::KeywordArgs[path: C::Optional[C::Any]] => self
def copy(path: NONE)
self.class.new(@snapshot_names, path.equal?(NONE) ? @path : path)
contract String => self
def add_path(path)
self.class.new(@snapshot_names, @paths + [path])
end

contract C::None => String
def to_s
"snapshot #{@snapshot_names.inspect}, path: #{@path.inspect}"
"snapshot #{@snapshot_names.inspect}, paths: #{@paths.inspect}"
end
end
end
10 changes: 6 additions & 4 deletions lib/nanoc/base/entities/rule_memory.rb
Expand Up @@ -33,7 +33,7 @@ def add_layout(layout_identifier, params)
contract Symbol, C::Maybe[String] => self
def add_snapshot(snapshot_name, path)
will_add_snapshot(snapshot_name)
@actions << Nanoc::Int::ProcessingActions::Snapshot.new([snapshot_name], path)
@actions << Nanoc::Int::ProcessingActions::Snapshot.new([snapshot_name], path ? [path] : [])
self
end

Expand All @@ -47,11 +47,13 @@ def any_layouts?
@actions.any? { |a| a.is_a?(Nanoc::Int::ProcessingActions::Layout) }
end

contract C::None => Hash
contract C::None => Array
def paths
snapshot_actions.each_with_object({}) do |action, paths|
snapshot_actions.each_with_object([]) do |action, pairs|
action.snapshot_names.each do |snapshot_name|
paths[snapshot_name] = action.path
action.paths.each do |path|
pairs << [snapshot_name, path]
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/base/services/item_rep_router.rb
Expand Up @@ -24,7 +24,7 @@ def initialize(reps, action_provider, site)
def run
paths_to_reps = {}
@reps.each do |rep|
@action_provider.paths_for(rep).each do |snapshot_name, path|
@action_provider.paths_for(rep).each do |(snapshot_name, path)|
route_rep(rep, path, snapshot_name, paths_to_reps)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/nanoc/rule_dsl/rule_memory_calculator.rb
Expand Up @@ -120,7 +120,7 @@ def new_rule_memory_for_layout(layout)

def copy_paths_from_routing_rules(mem, rep:)
mem.map do |action|
if action.is_a?(Nanoc::Int::ProcessingActions::Snapshot) && action.path.nil?
if action.is_a?(Nanoc::Int::ProcessingActions::Snapshot) && action.paths.empty?
copy_path_from_routing_rule(action, rep: rep)
else
action
Expand All @@ -136,7 +136,7 @@ def copy_path_from_routing_rule(action, rep:)

path_from_rules = paths_from_rules.find(&:itself)
if path_from_rules
action.copy(path: path_from_rules.to_s)
action.add_path(path_from_rules.to_s)
else
action
end
Expand Down
2 changes: 1 addition & 1 deletion spec/nanoc/base/compiler_spec.rb
Expand Up @@ -51,7 +51,7 @@
actions =
[
Nanoc::Int::ProcessingActions::Filter.new(:erb, {}),
Nanoc::Int::ProcessingActions::Snapshot.new([:last], nil),
Nanoc::Int::ProcessingActions::Snapshot.new([:last], []),
]

Nanoc::Int::RuleMemory.new(nil, actions: actions)
Expand Down
24 changes: 8 additions & 16 deletions spec/nanoc/base/entities/processing_actions/snapshot_spec.rb
@@ -1,32 +1,24 @@
describe Nanoc::Int::ProcessingActions::Snapshot do
let(:action) { described_class.new([:before_layout], '/foo.md') }
let(:action) { described_class.new([:before_layout], ['/foo.md']) }

describe '#serialize' do
subject { action.serialize }
it { is_expected.to eql([:snapshot, [:before_layout], true, '/foo.md']) }
it { is_expected.to eql([:snapshot, [:before_layout], true, ['/foo.md']]) }
end

describe '#to_s' do
subject { action.to_s }
it { is_expected.to eql('snapshot [:before_layout], path: "/foo.md"') }
it { is_expected.to eql('snapshot [:before_layout], paths: ["/foo.md"]') }
end

describe '#inspect' do
subject { action.inspect }
it { is_expected.to eql('<Nanoc::Int::ProcessingActions::Snapshot [:before_layout], true, "/foo.md">') }
it { is_expected.to eql('<Nanoc::Int::ProcessingActions::Snapshot [:before_layout], true, ["/foo.md"]>') }
end

describe '#copy' do
context 'without path' do
subject { action.copy }
its(:snapshot_names) { is_expected.to eql([:before_layout]) }
its(:path) { is_expected.to eql('/foo.md') }
end

context 'with path' do
subject { action.copy(path: '/donkey.md') }
its(:snapshot_names) { is_expected.to eql([:before_layout]) }
its(:path) { is_expected.to eql('/donkey.md') }
end
describe '#add_path' do
subject { action.add_path('/donkey.md') }
its(:snapshot_names) { is_expected.to eql([:before_layout]) }
its(:paths) { is_expected.to eql(['/foo.md', '/donkey.md']) }
end
end
4 changes: 2 additions & 2 deletions spec/nanoc/base/entities/rule_memory_spec.rb
Expand Up @@ -65,7 +65,7 @@
expect(rule_memory.size).to eql(1)
expect(rule_memory[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(rule_memory[0].snapshot_names).to eql([:before_layout])
expect(rule_memory[0].path).to eql('/foo.md')
expect(rule_memory[0].paths).to eql(['/foo.md'])
end
end

Expand Down Expand Up @@ -122,7 +122,7 @@
expect(subject).to eql(
[
[:filter, :erb, 'PeWUm2PtXYtqeHJdTqnY7kkwAow='],
[:snapshot, [:bar], true, '/foo.md'],
[:snapshot, [:bar], true, ['/foo.md']],
[:layout, '/default.erb', '97LAe1pYTLKczxBsu+x4MmvqdkU='],
],
)
Expand Down
Expand Up @@ -56,7 +56,7 @@
actions =
[
Nanoc::Int::ProcessingActions::Filter.new(:erb, {}),
Nanoc::Int::ProcessingActions::Snapshot.new([:last], nil),
Nanoc::Int::ProcessingActions::Snapshot.new([:last], []),
]

Nanoc::Int::RuleMemory.new(nil, actions: actions)
Expand Down
12 changes: 6 additions & 6 deletions spec/nanoc/rule_dsl/recording_executor_spec.rb
Expand Up @@ -32,7 +32,7 @@

expect(rule_memory[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(rule_memory[0].snapshot_names).to eql([:pre])
expect(rule_memory[0].path).to be_nil
expect(rule_memory[0].paths).to be_empty

expect(rule_memory[1]).to be_a(Nanoc::Int::ProcessingActions::Layout)
expect(rule_memory[1].layout_identifier).to eql('/default.*')
Expand All @@ -46,7 +46,7 @@

expect(rule_memory[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(rule_memory[0].snapshot_names).to eql([:pre])
expect(rule_memory[0].path).to be_nil
expect(rule_memory[0].paths).to be_empty

expect(rule_memory[1]).to be_a(Nanoc::Int::ProcessingActions::Layout)
expect(rule_memory[1].layout_identifier).to eql('/default.*')
Expand Down Expand Up @@ -78,7 +78,7 @@
expect(rule_memory.size).to eql(1)
expect(rule_memory[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(rule_memory[0].snapshot_names).to eql([:foo])
expect(rule_memory[0].path).to be_nil
expect(rule_memory[0].paths).to be_empty
end
end

Expand All @@ -93,7 +93,7 @@
expect(rule_memory.size).to eql(1)
expect(rule_memory[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(rule_memory[0].snapshot_names).to eql([:foo])
expect(rule_memory[0].path).to be_nil
expect(rule_memory[0].paths).to be_empty
end
end

Expand All @@ -105,7 +105,7 @@
expect(rule_memory.size).to eql(1)
expect(rule_memory[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(rule_memory[0].snapshot_names).to eql([:foo])
expect(rule_memory[0].path).to eql('/routed-foo.html')
expect(rule_memory[0].paths).to eql(['/routed-foo.html'])
end
end

Expand All @@ -117,7 +117,7 @@
expect(rule_memory.size).to eql(1)
expect(rule_memory[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(rule_memory[0].snapshot_names).to eql([:foo])
expect(rule_memory[0].path).to eql('/routed-foo.html')
expect(rule_memory[0].paths).to eql(['/routed-foo.html'])
end
end
end
Expand Down
26 changes: 13 additions & 13 deletions spec/nanoc/rule_dsl/rule_memory_calculator_spec.rb
Expand Up @@ -48,15 +48,15 @@

expect(subject[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[0].snapshot_names).to eql([:raw])
expect(subject[0].path).to be_nil
expect(subject[0].paths).to be_empty

expect(subject[1]).to be_a(Nanoc::Int::ProcessingActions::Filter)
expect(subject[1].filter_name).to eql(:erb)
expect(subject[1].params).to eql(speed: :over_9000)

expect(subject[2]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[2].snapshot_names).to eql([:pre])
expect(subject[2].path).to be_nil
expect(subject[2].paths).to be_empty

expect(subject[3]).to be_a(Nanoc::Int::ProcessingActions::Layout)
expect(subject[3].layout_identifier).to eql('/default.*')
Expand All @@ -68,11 +68,11 @@

expect(subject[5]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[5].snapshot_names).to eql([:post])
expect(subject[5].path).to be_nil
expect(subject[5].paths).to be_empty

expect(subject[6]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[6].snapshot_names).to eql([:last])
expect(subject[6].path).to be_nil
expect(subject[6].paths).to be_empty

expect(subject.size).to eql(7)
end
Expand All @@ -90,15 +90,15 @@

expect(subject[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[0].snapshot_names).to eql([:raw])
expect(subject[0].path).to be_nil
expect(subject[0].paths).to be_empty

expect(subject[1]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[1].snapshot_names).to eql([:last])
expect(subject[1].path).to be_nil
expect(subject[1].paths).to be_empty

expect(subject[2]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[2].snapshot_names).to eql([:pre])
expect(subject[2].path).to be_nil
expect(subject[2].paths).to be_empty

expect(subject.size).to eql(3)
end
Expand All @@ -120,15 +120,15 @@

expect(subject[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[0].snapshot_names).to eql([:raw])
expect(subject[0].path).to be_nil
expect(subject[0].paths).to be_empty

expect(subject[1]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[1].snapshot_names).to eql([:last])
expect(subject[1].path).to eq('/foo.md')
expect(subject[1].paths).to eq(['/foo.md'])

expect(subject[2]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[2].snapshot_names).to eql([:pre])
expect(subject[2].path).to be_nil
expect(subject[2].paths).to be_empty

expect(subject.size).to eql(3)
end
Expand All @@ -150,15 +150,15 @@

expect(subject[0]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[0].snapshot_names).to eql([:raw])
expect(subject[0].path).to be_nil
expect(subject[0].paths).to be_empty

expect(subject[1]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[1].snapshot_names).to eql([:last])
expect(subject[1].path).to be_nil
expect(subject[1].paths).to be_empty

expect(subject[2]).to be_a(Nanoc::Int::ProcessingActions::Snapshot)
expect(subject[2].snapshot_names).to eql([:pre])
expect(subject[2].path).to be_nil
expect(subject[2].paths).to be_empty

expect(subject.size).to eql(3)
end
Expand Down

0 comments on commit 3df9ade

Please sign in to comment.