Skip to content
Permalink
Browse files

Remove rep methods from preprocess/compile contexts

  • Loading branch information...
ddfreyne committed Dec 13, 2015
1 parent 1551035 commit 4547c593c6d4ef343866227b116c03175e13cf8f
Showing with 282 additions and 124 deletions.
  1. +1 −1 lib/nanoc/base/checksummer.rb
  2. +2 −2 lib/nanoc/base/compilation/compiler.rb
  3. +1 −1 lib/nanoc/base/compilation/filter.rb
  4. +5 −2 lib/nanoc/base/views.rb
  5. +0 −8 lib/nanoc/base/views/item_collection_view.rb
  6. +8 −0 lib/nanoc/base/views/item_collection_with_reps_view.rb
  7. +8 −0 lib/nanoc/base/views/item_collection_without_reps_view.rb
  8. +2 −2 lib/nanoc/base/views/item_rep_view.rb
  9. +5 −0 lib/nanoc/base/views/item_with_reps_view.rb
  10. +51 −0 lib/nanoc/base/views/item_without_reps_view.rb
  11. +1 −1 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
  12. +1 −45 lib/nanoc/base/views/{item_view.rb → mixins/with_reps_view_mixin.rb}
  13. +1 −1 lib/nanoc/base/views/mutable_item_view.rb
  14. +1 −1 lib/nanoc/base/views/post_compile_item_view.rb
  15. +1 −1 lib/nanoc/cli/commands/shell.rb
  16. +1 −1 lib/nanoc/extra/checking/check.rb
  17. +1 −1 lib/nanoc/helpers/capturing.rb
  18. +2 −2 lib/nanoc/rule_dsl/rule_context.rb
  19. +28 −6 spec/nanoc/base/checksummer_spec.rb
  20. +2 −2 spec/nanoc/base/views/item_collection_view_spec.rb
  21. +1 −1 spec/nanoc/base/views/item_rep_view_spec.rb
  22. +3 −3 spec/nanoc/base/views/item_view_spec.rb
  23. +1 −1 spec/nanoc/base/views/mutable_document_view_spec.rb
  24. +9 −0 spec/nanoc/base/views/mutable_item_view_spec.rb
  25. +1 −1 spec/nanoc/cli/commands/shell_spec.rb
  26. +5 −5 spec/nanoc/helpers/blogging_spec.rb
  27. +19 −0 spec/nanoc/regressions/gh_767_spec.rb
  28. +89 −4 spec/nanoc/rule_dsl/rule_context_spec.rb
  29. +4 −4 test/filters/test_less.rb
  30. +1 −1 test/filters/test_sass.rb
  31. +3 −3 test/filters/test_xsl.rb
  32. +5 −5 test/helpers/test_capturing.rb
  33. +1 −1 test/helpers/test_filtering.rb
  34. +6 −6 test/helpers/test_tagging.rb
  35. +12 −12 test/helpers/test_xml_sitemap.rb
@@ -99,7 +99,7 @@ def update(obj, digest, visited = Set.new)

digest.update(',identifier=')
update(obj.identifier, digest)
when Nanoc::ItemView, Nanoc::LayoutView, Nanoc::ConfigView, Nanoc::IdentifiableCollectionView
when Nanoc::ItemWithRepsView, Nanoc::ItemWithoutRepsView, Nanoc::LayoutView, Nanoc::ConfigView, Nanoc::IdentifiableCollectionView
update(obj.unwrap, digest)
else
data = begin
@@ -158,10 +158,10 @@ def assigns_for(rep)

# TODO: Do not expose @site (necessary for captures store though…)
content_or_filename_assigns.merge(
item: Nanoc::ItemView.new(rep.item, view_context),
item: Nanoc::ItemWithRepsView.new(rep.item, view_context),
rep: Nanoc::ItemRepView.new(rep, view_context),
item_rep: Nanoc::ItemRepView.new(rep, view_context),
items: Nanoc::ItemCollectionView.new(site.items, view_context),
items: Nanoc::ItemCollectionWithRepsView.new(site.items, view_context),
layouts: Nanoc::LayoutCollectionView.new(site.layouts, view_context),
config: Nanoc::ConfigView.new(site.config, view_context),
site: Nanoc::SiteView.new(site, view_context),
@@ -184,7 +184,7 @@ def filename
# @return [void]
def depend_on(items)
orig_items = items
items = items.map { |i| i.is_a?(Nanoc::ItemView) ? i.unwrap : i }
items = items.map { |i| i.is_a?(Nanoc::ItemWithRepsView) ? i.unwrap : i }

# Notify
items.each do |item|
@@ -1,13 +1,16 @@
require_relative 'views/mixins/document_view_mixin'
require_relative 'views/mixins/mutable_document_view_mixin'
require_relative 'views/mixins/with_reps_view_mixin'

require_relative 'views/view'
require_relative 'views/view_context'

require_relative 'views/config_view'
require_relative 'views/identifiable_collection_view'
require_relative 'views/item_view'
require_relative 'views/item_collection_view'
require_relative 'views/item_without_reps_view'
require_relative 'views/item_with_reps_view'
require_relative 'views/item_collection_with_reps_view'
require_relative 'views/item_collection_without_reps_view'
require_relative 'views/item_rep_view'
require_relative 'views/item_rep_collection_view'
require_relative 'views/layout_view'

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,8 @@
module Nanoc
class ItemCollectionWithRepsView < ::Nanoc::IdentifiableCollectionView
# @api private
def view_class
Nanoc::ItemWithRepsView
end
end
end
@@ -0,0 +1,8 @@
module Nanoc
class ItemCollectionWithoutRepsView < ::Nanoc::IdentifiableCollectionView
# @api private
def view_class
Nanoc::ItemWithoutRepsView
end
end
end
@@ -60,9 +60,9 @@ def path(snapshot: :last)

# Returns the item that this item rep belongs to.
#
# @return [Nanoc::ItemView]
# @return [Nanoc::ItemWithRepsView]
def item
Nanoc::ItemView.new(@item_rep.item, @context)
Nanoc::ItemWithRepsView.new(@item_rep.item, @context)
end

# @api private
@@ -0,0 +1,5 @@
module Nanoc
class ItemWithRepsView < ::Nanoc::ItemWithoutRepsView
include Nanoc::WithRepsViewMixin
end
end
@@ -0,0 +1,51 @@
module Nanoc
class ItemWithoutRepsView < ::Nanoc::View
include Nanoc::DocumentViewMixin

# Returns the children of this item. For items with identifiers that have
# extensions, returns an empty collection.
#
# @return [Enumerable<Nanoc::ItemWithRepsView>]
def children
unless unwrap.identifier.legacy?
raise Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem.new(unwrap.identifier)
end

children_pattern = Nanoc::Int::Pattern.from(unwrap.identifier.to_s + '*/')
children = @context.items.select { |i| children_pattern.match?(i.identifier) }

children.map { |i| self.class.new(i, @context) }.freeze
end

# Returns the parent of this item, if one exists. For items with identifiers
# that have extensions, returns nil.
#
# @return [Nanoc::ItemWithRepsView] if the item has a parent
#
# @return [nil] if the item has no parent
def parent
unless unwrap.identifier.legacy?
raise Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem.new(unwrap.identifier)
end

parent_identifier = '/' + unwrap.identifier.components[0..-2].join('/') + '/'

# FIXME: Fix this elsewhere
parent_identifier.sub!(/\/\/$/, '/')

parent = @context.items[parent_identifier]

parent && self.class.new(parent, @context).freeze
end

# @return [Boolean] True if the item is binary, false otherwise
def binary?
unwrap.content.binary?
end

# @api private
def raw_filename
unwrap.content.filename
end
end
end
@@ -22,7 +22,7 @@ def []=(key, value)
disallowed_value_classes = Set.new([
Nanoc::Int::Item,
Nanoc::Int::Layout,
Nanoc::ItemView,
Nanoc::ItemWithRepsView,
Nanoc::LayoutView,
])
if disallowed_value_classes.include?(value.class)
@@ -1,7 +1,5 @@
module Nanoc
class ItemView < ::Nanoc::View
include Nanoc::DocumentViewMixin

module WithRepsViewMixin
# Returns the compiled content.
#
# @param [String] rep The name of the representation
@@ -35,53 +33,11 @@ def path(rep: :default, snapshot: :last)
reps.fetch(rep).path(snapshot: snapshot)
end

# Returns the children of this item. For items with identifiers that have
# extensions, returns an empty collection.
#
# @return [Enumerable<Nanoc::ItemView>]
def children
unless unwrap.identifier.legacy?
raise Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem.new(unwrap.identifier)
end

children_pattern = Nanoc::Int::Pattern.from(unwrap.identifier.to_s + '*/')
children = @context.items.select { |i| children_pattern.match?(i.identifier) }

children.map { |i| Nanoc::ItemView.new(i, @context) }.freeze
end

# Returns the parent of this item, if one exists. For items with identifiers
# that have extensions, returns nil.
#
# @return [Nanoc::ItemView] if the item has a parent
#
# @return [nil] if the item has no parent
def parent
unless unwrap.identifier.legacy?
raise Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem.new(unwrap.identifier)
end

parent_identifier = '/' + unwrap.identifier.components[0..-2].join('/') + '/'
parent = @context.items[parent_identifier]

parent && Nanoc::ItemView.new(parent, @context).freeze
end

# @return [Boolean] True if the item is binary, false otherwise
def binary?
unwrap.content.binary?
end

# Returns the representations of this item.
#
# @return [Nanoc::ItemRepCollectionView]
def reps
Nanoc::ItemRepCollectionView.new(@context.reps[unwrap], @context)
end

# @api private
def raw_filename
unwrap.content.filename
end
end
end
@@ -1,5 +1,5 @@
module Nanoc
class MutableItemView < Nanoc::ItemView
class MutableItemView < Nanoc::ItemWithoutRepsView
include Nanoc::MutableDocumentViewMixin
end
end
@@ -1,5 +1,5 @@
module Nanoc
class PostCompileItemView < Nanoc::ItemView
class PostCompileItemView < Nanoc::ItemWithRepsView
def modified
reps.select { |rep| rep.unwrap.modified }
end
@@ -23,7 +23,7 @@ def env

def self.env_for_site(site)
{
items: Nanoc::ItemCollectionView.new(site.items, nil),
items: Nanoc::ItemCollectionWithRepsView.new(site.items, nil),
layouts: Nanoc::LayoutCollectionView.new(site.layouts, nil),
config: Nanoc::ConfigView.new(site.config, nil),
}
@@ -23,7 +23,7 @@ def self.create(site)
view_context = site.compiler.create_view_context

context = {
items: Nanoc::ItemCollectionView.new(site.items, view_context),
items: Nanoc::ItemCollectionWithRepsView.new(site.items, view_context),
layouts: Nanoc::LayoutCollectionView.new(site.layouts, view_context),
config: Nanoc::ConfigView.new(site.config, view_context),
site: Nanoc::SiteView.new(site, view_context), # TODO: remove me
@@ -140,7 +140,7 @@ def content_for(*args, &block)
raise ArgumentError, 'expected 2 arguments (the item ' \
"and the name of the capture) but got #{args.size} instead"
end
item = args[0].is_a?(Nanoc::ItemView) ? args[0].unwrap : args[0]
item = args[0].is_a?(Nanoc::ItemWithRepsView) ? args[0].unwrap : args[0]
name = args[1]

# Create dependency
@@ -13,10 +13,10 @@ def initialize(rep:, site:, executor:, view_context:)
@_executor = executor

super({
item: Nanoc::ItemView.new(rep.item, view_context),
item: Nanoc::ItemWithoutRepsView.new(rep.item, view_context),
rep: Nanoc::ItemRepView.new(rep, view_context),
item_rep: Nanoc::ItemRepView.new(rep, view_context),
items: Nanoc::ItemCollectionView.new(site.items, view_context),
items: Nanoc::ItemCollectionWithoutRepsView.new(site.items, view_context),
layouts: Nanoc::LayoutCollectionView.new(site.layouts, view_context),
config: Nanoc::ConfigView.new(site.config, view_context),
site: Nanoc::SiteView.new(site, view_context),
@@ -206,11 +206,18 @@
end
end

context 'Nanoc::ItemView' do
let(:obj) { Nanoc::ItemView.new(item, nil) }
context 'Nanoc::ItemWithRepsView' do
let(:obj) { Nanoc::ItemWithRepsView.new(item, nil) }
let(:item) { Nanoc::Int::Item.new('asdf', {}, '/foo.md') }

it { is_expected.to eql('Nanoc::ItemView<Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<asdf>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>>') }
it { is_expected.to eql('Nanoc::ItemWithRepsView<Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<asdf>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>>') }
end

context 'Nanoc::ItemWithoutRepsView' do
let(:obj) { Nanoc::ItemWithoutRepsView.new(item, nil) }
let(:item) { Nanoc::Int::Item.new('asdf', {}, '/foo.md') }

it { is_expected.to eql('Nanoc::ItemWithoutRepsView<Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<asdf>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>>') }
end

context 'Nanoc::LayoutView' do
@@ -227,8 +234,23 @@
it { is_expected.to eql('Nanoc::ConfigView<Nanoc::Int::Configuration<Symbol<foo>=String<bar>,>>') }
end

context 'Nanoc::ItemCollectionView' do
let(:obj) { Nanoc::ItemCollectionView.new(wrapped, nil) }
context 'Nanoc::ItemCollectionWithRepsView' do
let(:obj) { Nanoc::ItemCollectionWithRepsView.new(wrapped, nil) }

let(:config) { Nanoc::Int::Configuration.new({ 'foo' => 'bar' }) }

let(:wrapped) do
Nanoc::Int::IdentifiableCollection.new(config).tap do |arr|
arr << Nanoc::Int::Item.new('foo', {}, '/foo.md')
arr << Nanoc::Int::Item.new('bar', {}, '/foo.md')
end
end

it { is_expected.to eql('Nanoc::ItemCollectionWithRepsView<Nanoc::Int::IdentifiableCollection<Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<foo>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>,Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<bar>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>,>>') }
end

context 'Nanoc::ItemCollectionWithoutRepsView' do
let(:obj) { Nanoc::ItemCollectionWithoutRepsView.new(wrapped, nil) }

let(:config) { Nanoc::Int::Configuration.new({ 'foo' => 'bar' }) }

@@ -239,7 +261,7 @@
end
end

it { is_expected.to eql('Nanoc::ItemCollectionView<Nanoc::Int::IdentifiableCollection<Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<foo>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>,Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<bar>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>,>>') }
it { is_expected.to eql('Nanoc::ItemCollectionWithoutRepsView<Nanoc::Int::IdentifiableCollection<Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<foo>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>,Nanoc::Int::Item<content=Nanoc::Int::TextualContent<String<bar>>,attributes=Hash<>,identifier=Nanoc::Identifier<String</foo.md>>>,>>') }
end

context 'other marshal-able classes' do
@@ -1,4 +1,4 @@
describe Nanoc::ItemCollectionView do
let(:view_class) { Nanoc::ItemView }
describe Nanoc::ItemCollectionWithRepsView do
let(:view_class) { Nanoc::ItemWithRepsView }
it_behaves_like 'an identifiable collection'
end
@@ -177,7 +177,7 @@
subject { view.item }

it 'returns an item view' do
expect(subject).to be_a(Nanoc::ItemView)
expect(subject).to be_a(Nanoc::ItemWithRepsView)
end

it 'returns an item view with the right context' do
@@ -1,4 +1,4 @@
describe Nanoc::ItemView do
describe Nanoc::ItemWithRepsView do
let(:entity_class) { Nanoc::Int::Item }
it_behaves_like 'a document view'

@@ -50,7 +50,7 @@
end

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

@@ -127,7 +127,7 @@

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

@@ -19,7 +19,7 @@
end

it 'disallows item views' do
item = Nanoc::ItemView.new(Nanoc::Int::Item.new('content', {}, '/foo.md'), nil)
item = Nanoc::ItemWithRepsView.new(Nanoc::Int::Item.new('content', {}, '/foo.md'), nil)
expect { view[:item] = item }.to raise_error(Nanoc::MutableDocumentViewMixin::DisallowedAttributeValueError)
end

@@ -1,4 +1,13 @@
describe Nanoc::MutableItemView do
let(:entity_class) { Nanoc::Int::Item }
it_behaves_like 'a mutable document view'

let(:item) { entity_class.new('content', {}, '/asdf/') }
let(:view) { described_class.new(item, nil) }

it 'does have rep access' do
expect(view).not_to respond_to(:compiled_content)
expect(view).not_to respond_to(:path)
expect(view).not_to respond_to(:reps)
end
end
Oops, something went wrong.

0 comments on commit 4547c59

Please sign in to comment.
You can’t perform that action at this time.