Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let #parent/#children raise for non-legacy identifiers #710

Merged
merged 1 commit into from
Oct 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/nanoc/base/entities/identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def full?
@type == :full
end

# @return [Boolean] True if this is a legacy identifier (i.e. does not
# include the extension), false otherwise
def legacy?
@type == :legacy
end

# @return [String]
def chop
to_s.chop
Expand Down
7 changes: 7 additions & 0 deletions lib/nanoc/base/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,12 @@ def initialize(identifier, type)
super("There are multiple #{type}s with the #{identifier} identifier.")
end
end

# Error that is raised when attempting to call #parent or #children on an item with a legacy identifier.
class CannotGetParentOrChildrenOfNonLegacyItem < Generic
def initialize(identifier)
super("You cannot get the parent or children of an item that has a “full” identifier (#{identifier}). Getting the parent or children of an item is only possible for items that have a legacy identifier.")
end
end
end
end
8 changes: 8 additions & 0 deletions lib/nanoc/base/views/item_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def path(params = {})
#
# @return [Enumerable<Nanoc::ItemView>]
def children
unless unwrap.identifier.legacy?
raise Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem.new(unwrap.identifier)
end

unwrap.children.map { |i| Nanoc::ItemView.new(i) }
end

Expand All @@ -50,6 +54,10 @@ def children
#
# @return [nil] if the item has no parent
def parent
unless unwrap.identifier.legacy?
raise Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem.new(unwrap.identifier)
end

unwrap.parent && Nanoc::ItemView.new(unwrap.parent)
end

Expand Down
28 changes: 28 additions & 0 deletions spec/nanoc/base/entities/identifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,32 @@
it { is_expected.to eql(['html', 'md']) }
end
end

describe '#legacy?' do
subject { identifier.legacy? }

context 'legacy type' do
let(:identifier) { described_class.new('/foo/', type: :legacy) }
it { is_expected.to eql(true) }
end

context 'full type' do
let(:identifier) { described_class.new('/foo/', type: :full) }
it { is_expected.to eql(false) }
end
end

describe '#full?' do
subject { identifier.full? }

context 'legacy type' do
let(:identifier) { described_class.new('/foo/', type: :legacy) }
it { is_expected.to eql(false) }
end

context 'full type' do
let(:identifier) { described_class.new('/foo/', type: :full) }
it { is_expected.to eql(true) }
end
end
end
78 changes: 71 additions & 7 deletions spec/nanoc/base/views/item_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

describe '#parent' do
let(:item) do
Nanoc::Int::Item.new('me', {}, '/me/').tap { |i| i.parent = parent_item }
Nanoc::Int::Item.new('me', {}, identifier).tap { |i| i.parent = parent_item }
end

let(:view) { described_class.new(item) }
Expand All @@ -25,9 +25,25 @@
Nanoc::Int::Item.new('parent', {}, '/parent/')
end

it 'returns a view for the parent' do
expect(subject.class).to eql(Nanoc::ItemView)
expect(subject.unwrap).to eql(parent_item)
context 'full identifier' do
let(:identifier) do
Nanoc::Identifier.new('/me.md')
end

it 'raises' do
expect { subject }.to raise_error(Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem)
end
end

context 'legacy identifier' do
let(:identifier) do
Nanoc::Identifier.new('/me/', type: :legacy)
end

it 'returns a view for the parent' do
expect(subject.class).to eql(Nanoc::ItemView)
expect(subject.unwrap).to eql(parent_item)
end
end
end

Expand All @@ -36,14 +52,62 @@
nil
end

it 'returns nil' do
expect(subject).to be_nil
context 'full identifier' do
let(:identifier) do
Nanoc::Identifier.new('/me.md')
end

it 'raises' do
expect { subject }.to raise_error(Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem)
end
end

context 'legacy identifier' do
let(:identifier) do
Nanoc::Identifier.new('/me/', type: :legacy)
end

it 'returns nil' do
expect(subject).to be_nil
end
end
end
end

describe '#children' do
# TODO: implement
let(:item) do
Nanoc::Int::Item.new('me', {}, identifier).tap { |i| i.children = children }
end

let(:children) do
[Nanoc::Int::Item.new('child', {}, '/child/')]
end

let(:view) { described_class.new(item) }

subject { view.children }

context 'full identifier' do
let(:identifier) do
Nanoc::Identifier.new('/me.md')
end

it 'raises' do
expect { subject }.to raise_error(Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem)
end
end

context 'legacy identifier' do
let(:identifier) do
Nanoc::Identifier.new('/me/', type: :legacy)
end

it 'returns views for the children' do
expect(subject.size).to eql(1)
expect(subject[0].class).to eql(Nanoc::ItemView)
expect(subject[0].unwrap).to eql(children[0])
end
end
end

describe '#reps' do
Expand Down