Skip to content

Commit

Permalink
Make item rep views return item views of proper class
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Oct 8, 2017
1 parent 5103601 commit c922ef2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/nanoc/base/views/basic_item_rep_view.rb
Expand Up @@ -8,6 +8,11 @@ def initialize(item_rep, context)
@item_rep = item_rep
end

# @abstract
def item_view_class
Nanoc::BasicItemView
end

# @api private
def unwrap
@item_rep
Expand Down Expand Up @@ -58,7 +63,7 @@ def path(snapshot: :last)
#
# @return [Nanoc::CompilationItemView]
def item
Nanoc::CompilationItemView.new(@item_rep.item, @context)
item_view_class.new(@item_rep.item, @context)
end

# @api private
Expand Down
5 changes: 5 additions & 0 deletions lib/nanoc/base/views/compilation_item_rep_view.rb
Expand Up @@ -2,6 +2,11 @@

module Nanoc
class CompilationItemRepView < ::Nanoc::BasicItemRepView
# @abstract
def item_view_class
Nanoc::CompilationItemView
end

# Returns the item rep’s raw path. It includes the path to the output
# directory and the full filename.
#
Expand Down
2 changes: 0 additions & 2 deletions lib/nanoc/base/views/compilation_item_view.rb
Expand Up @@ -39,8 +39,6 @@ def path(rep: :default, snapshot: :last)
#
# @return [Nanoc::BasicItemRepCollectionView]
def reps
# FIXME: dedup into BasicCompilationItemView and CompilationCompilationItemView

Nanoc::CompilationItemRepCollectionView.new(@context.reps[unwrap], @context)
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/nanoc/base/views/post_compile_item_rep_view.rb
Expand Up @@ -2,6 +2,10 @@

module Nanoc
class PostCompileItemRepView < ::Nanoc::BasicItemRepView
def item_view_class
Nanoc::PostCompileItemView
end

def compiled_content(snapshot: nil)
compilation_context = @context.compilation_context
snapshot_contents = compilation_context.compiled_content_cache[unwrap]
Expand Down
2 changes: 2 additions & 0 deletions spec/nanoc/base/views/basic_item_rep_view_spec.rb
Expand Up @@ -3,5 +3,7 @@
require_relative 'support/item_rep_view_examples'

describe Nanoc::BasicItemRepView do
let(:expected_item_view_class) { Nanoc::BasicItemView }

it_behaves_like 'an item rep view'
end
2 changes: 2 additions & 0 deletions spec/nanoc/base/views/compilation_item_rep_view_spec.rb
Expand Up @@ -3,6 +3,8 @@
require_relative 'support/item_rep_view_examples'

describe Nanoc::CompilationItemRepView do
let(:expected_item_view_class) { Nanoc::CompilationItemView }

it_behaves_like 'an item rep view'

let(:view_context) do
Expand Down
2 changes: 2 additions & 0 deletions spec/nanoc/base/views/post_compile_item_rep_view_spec.rb
Expand Up @@ -3,6 +3,8 @@
require_relative 'support/item_rep_view_examples'

describe Nanoc::PostCompileItemRepView do
let(:expected_item_view_class) { Nanoc::PostCompileItemView }

it_behaves_like 'an item rep view'

let(:item_rep) { Nanoc::Int::ItemRep.new(item, :jacques) }
Expand Down
4 changes: 3 additions & 1 deletion spec/nanoc/base/views/support/item_rep_view_examples.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true

shared_examples 'an item rep view' do
# needs expected_item_view_class

let(:view_context) do
Nanoc::ViewContextForCompilation.new(
reps: Nanoc::Int::ItemRepRepo.new,
Expand Down Expand Up @@ -286,7 +288,7 @@
subject { view.item }

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

it 'returns an item view with the right context' do
Expand Down

0 comments on commit c922ef2

Please sign in to comment.