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

Make #raw_path public #1231

Merged
merged 10 commits into from
Oct 8, 2017
6 changes: 3 additions & 3 deletions lib/nanoc/base/services/compilation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def assigns_for(rep, dependency_tracker)
view_context = create_view_context(dependency_tracker)

content_or_filename_assigns.merge(
item: Nanoc::ItemWithRepsView.new(rep.item, view_context),
rep: Nanoc::ItemRepView.new(rep, view_context),
item_rep: Nanoc::ItemRepView.new(rep, view_context),
item: Nanoc::CompilationItemView.new(rep.item, view_context),
rep: Nanoc::CompilationItemRepView.new(rep, view_context),
item_rep: Nanoc::CompilationItemRepView.new(rep, 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),
Expand Down
14 changes: 10 additions & 4 deletions lib/nanoc/base/views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@
require_relative 'views/view_context_for_shell'

require_relative 'views/view'

require_relative 'views/basic_item_rep_view'
require_relative 'views/basic_item_rep_collection_view'
require_relative 'views/basic_item_view'

require_relative 'views/compilation_item_rep_view'
require_relative 'views/compilation_item_rep_collection_view'
require_relative 'views/compilation_item_view'

require_relative 'views/config_view'
require_relative 'views/identifiable_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'
require_relative 'views/layout_collection_view'

require_relative 'views/mutable_config_view'
require_relative 'views/mutable_identifiable_collection_view'
require_relative 'views/mutable_item_view'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Nanoc
class ItemRepCollectionView < ::Nanoc::View
class BasicItemRepCollectionView < ::Nanoc::View
include Enumerable

class NoSuchItemRepError < ::Nanoc::Error
Expand All @@ -23,7 +23,7 @@ def unwrap

# @api private
def view_class
Nanoc::ItemRepView
Nanoc::BasicItemRepView
end

def to_ary
Expand Down Expand Up @@ -53,16 +53,16 @@ def size
#
# @return [nil] if no item rep with the given name was found
#
# @return [Nanoc::ItemRepView] if an item rep with the given name was found
# @return [Nanoc::BasicItemRepView] if an item rep with the given name was found
def [](rep_name)
case rep_name
when Symbol
res = @item_reps.find { |ir| ir.name == rep_name }
res && view_class.new(res, @context)
when Integer
raise ArgumentError, "expected ItemRepCollectionView#[] to be called with a symbol (you likely want `.reps[:default]` rather than `.reps[#{rep_name}]`)"
raise ArgumentError, "expected BasicItemRepCollectionView#[] to be called with a symbol (you likely want `.reps[:default]` rather than `.reps[#{rep_name}]`)"
else
raise ArgumentError, 'expected ItemRepCollectionView#[] to be called with a symbol'
raise ArgumentError, 'expected BasicItemRepCollectionView#[] to be called with a symbol'
end
end

Expand All @@ -71,7 +71,7 @@ def [](rep_name)
#
# @param [Symbol] rep_name
#
# @return [Nanoc::ItemRepView]
# @return [Nanoc::BasicItemRepView]
#
# @raise if no rep was found
def fetch(rep_name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# frozen_string_literal: true

module Nanoc
class ItemRepView < ::Nanoc::View
class BasicItemRepView < ::Nanoc::View
# @api private
def initialize(item_rep, context)
super(context)
@item_rep = item_rep
end

# @abstract
def item_view_class
Nanoc::BasicItemView
end

# @api private
def unwrap
@item_rep
Expand Down Expand Up @@ -35,19 +40,6 @@ def name
@item_rep.name
end

# Returns the compiled content.
#
# @param [String] snapshot The name of the snapshot from which to
# fetch the compiled content. By default, the returned compiled content
# will be the content compiled right before the first layout call (if
# any).
#
# @return [String] The content at the given snapshot.
def compiled_content(snapshot: nil)
@context.dependency_tracker.bounce(unwrap.item, compiled_content: true)
@context.snapshot_repo.compiled_content(rep: unwrap, snapshot: snapshot)
end

def snapshot?(name)
@context.dependency_tracker.bounce(unwrap.item, compiled_content: true)
@item_rep.snapshot?(name)
Expand All @@ -69,22 +61,9 @@ def path(snapshot: :last)

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

# @api private
def raw_path(snapshot: :last)
@context.dependency_tracker.bounce(unwrap.item, compiled_content: true)

res = @item_rep.raw_path(snapshot: snapshot)

unless @item_rep.compiled?
Fiber.yield(Nanoc::Int::Errors::UnmetDependency.new(@item_rep))
end

res
item_view_class.new(@item_rep.item, @context)
end

# @api private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

module Nanoc
class ItemWithoutRepsView < ::Nanoc::View
class BasicItemView < ::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>]
# @return [Enumerable<Nanoc::CompilationItemView>]
def children
unless unwrap.identifier.legacy?
raise Nanoc::Int::Errors::CannotGetParentOrChildrenOfNonLegacyItem.new(unwrap.identifier)
Expand All @@ -22,7 +22,7 @@ def children
# 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 [Nanoc::CompilationItemView] if the item has a parent
#
# @return [nil] if the item has no parent
def parent
Expand Down
10 changes: 10 additions & 0 deletions lib/nanoc/base/views/compilation_item_rep_collection_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Nanoc
class CompilationItemRepCollectionView < ::Nanoc::BasicItemRepCollectionView
# @api private
def view_class
Nanoc::CompilationItemRepView
end
end
end
42 changes: 42 additions & 0 deletions lib/nanoc/base/views/compilation_item_rep_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

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.
#
# @param [Symbol] snapshot The snapshot for which the path should be
# returned.
#
# @return [String] The item rep’s raw path.
def raw_path(snapshot: :last)
@context.dependency_tracker.bounce(unwrap.item, compiled_content: true)

res = @item_rep.raw_path(snapshot: snapshot)

unless @item_rep.compiled?
Fiber.yield(Nanoc::Int::Errors::UnmetDependency.new(@item_rep))
end

res
end

# Returns the compiled content.
#
# @param [String] snapshot The name of the snapshot from which to
# fetch the compiled content. By default, the returned compiled content
# will be the content compiled right before the first layout call (if
# any).
#
# @return [String] The content at the given snapshot.
def compiled_content(snapshot: nil)
@context.dependency_tracker.bounce(unwrap.item, compiled_content: true)
@context.snapshot_repo.compiled_content(rep: unwrap, snapshot: snapshot)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Nanoc
class ItemWithRepsView < ::Nanoc::ItemWithoutRepsView
class CompilationItemView < ::Nanoc::BasicItemView
# Returns the compiled content.
#
# @param [String] rep The name of the representation
Expand Down Expand Up @@ -37,9 +37,9 @@ def path(rep: :default, snapshot: :last)

# Returns the representations of this item.
#
# @return [Nanoc::ItemRepCollectionView]
# @return [Nanoc::BasicItemRepCollectionView]
def reps
Nanoc::ItemRepCollectionView.new(@context.reps[unwrap], @context)
Nanoc::CompilationItemRepCollectionView.new(@context.reps[unwrap], @context)
end
end
end
2 changes: 1 addition & 1 deletion lib/nanoc/base/views/item_collection_with_reps_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Nanoc
class ItemCollectionWithRepsView < ::Nanoc::IdentifiableCollectionView
# @api private
def view_class
Nanoc::ItemWithRepsView
Nanoc::CompilationItemView
end
end
end
2 changes: 1 addition & 1 deletion lib/nanoc/base/views/item_collection_without_reps_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Nanoc
class ItemCollectionWithoutRepsView < ::Nanoc::IdentifiableCollectionView
# @api private
def view_class
Nanoc::ItemWithoutRepsView
Nanoc::BasicItemView
end
end
end
2 changes: 1 addition & 1 deletion lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def []=(key, value)
disallowed_value_classes = Set.new([
Nanoc::Int::Item,
Nanoc::Int::Layout,
Nanoc::ItemWithRepsView,
Nanoc::CompilationItemView,
Nanoc::LayoutView,
])
if disallowed_value_classes.include?(value.class)
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/base/views/mutable_item_view.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Nanoc
class MutableItemView < Nanoc::ItemWithoutRepsView
class MutableItemView < Nanoc::BasicItemView
include Nanoc::MutableDocumentViewMixin
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Nanoc
class PostCompileItemRepCollectionView < Nanoc::ItemRepCollectionView
class PostCompileItemRepCollectionView < Nanoc::BasicItemRepCollectionView
# @api private
def view_class
Nanoc::PostCompileItemRepView
Expand Down
6 changes: 5 additions & 1 deletion lib/nanoc/base/views/post_compile_item_rep_view.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

module Nanoc
class PostCompileItemRepView < ::Nanoc::ItemRepView
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: 1 addition & 1 deletion lib/nanoc/base/views/post_compile_item_view.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Nanoc
class PostCompileItemView < Nanoc::ItemWithRepsView
class PostCompileItemView < Nanoc::CompilationItemView
def reps
Nanoc::PostCompileItemRepCollectionView.new(@context.reps[unwrap], @context)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/helpers/link_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def link_to(text, target, attributes = {})
case target
when String
target
when Nanoc::ItemWithRepsView, Nanoc::ItemWithoutRepsView, Nanoc::ItemRepView
when Nanoc::CompilationItemView, Nanoc::BasicItemView, Nanoc::BasicItemRepView
raise "Cannot create a link to #{target.inspect} because this target is not outputted (its routing rule returns nil)" if target.path.nil?
target.path
else
Expand Down
6 changes: 3 additions & 3 deletions lib/nanoc/rule_dsl/rule_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def initialize(rep:, site:, executor:, view_context:)
@_executor = executor

super({
item: Nanoc::ItemWithoutRepsView.new(rep.item, view_context),
rep: Nanoc::ItemRepView.new(rep, view_context),
item_rep: Nanoc::ItemRepView.new(rep, view_context),
item: Nanoc::BasicItemView.new(rep.item, view_context),
rep: Nanoc::BasicItemRepView.new(rep, view_context),
item_rep: Nanoc::BasicItemRepView.new(rep, 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),
Expand Down
14 changes: 7 additions & 7 deletions lib/nanoc/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(mod)
#
# @param [Nanoc::Identifier, String] identifier This item's identifier
#
# @return [Nanoc::ItemWithRepsView] A view for the newly created item
# @return [Nanoc::CompilationItemView] A view for the newly created item
def create_item(content, attributes, identifier)
item = Nanoc::Int::Item.new(content, attributes, identifier)
@items = @items.add(item)
Expand All @@ -57,7 +57,7 @@ def create_item(content, attributes, identifier)
#
# @param [Nanoc::Identifier, String] identifier This layout's identifier
#
# @return [Nanoc::ItemWithRepsView] A view for the newly created layout
# @return [Nanoc::CompilationItemView] A view for the newly created layout
def create_layout(content, attributes, identifier)
layout = Nanoc::Int::Layout.new(content, attributes, identifier)
@layouts = @layouts.add(layout)
Expand All @@ -66,7 +66,7 @@ def create_layout(content, attributes, identifier)

# Creates a new representation for the given item.
#
# @param [Nanoc::ItemWithRepsView] item The item to create a represetation for
# @param [Nanoc::CompilationItemView] item The item to create a represetation for
#
# @param [String] path The path of the `:last` snapshot of this item representation
# @param [Symbol] rep The rep name to create
Expand Down Expand Up @@ -97,12 +97,12 @@ def config
assigns[:config]
end

# @return [Nanoc::ItemWithRepsView, nil]
# @return [Nanoc::CompilationItemView, nil]
def item
assigns[:item]
end

# @return [Nanoc::ItemRepView, nil]
# @return [Nanoc::BasicItemRepView, nil]
def item_rep
assigns[:item_rep]
end
Expand Down Expand Up @@ -190,8 +190,8 @@ def site
def assigns
{
config: Nanoc::MutableConfigView.new(@config, view_context),
item_rep: @item_rep ? Nanoc::ItemRepView.new(@item_rep, view_context) : nil,
item: @item ? Nanoc::ItemWithRepsView.new(@item, view_context) : nil,
item_rep: @item_rep ? Nanoc::CompilationItemRepView.new(@item_rep, view_context) : nil,
item: @item ? Nanoc::CompilationItemView.new(@item, view_context) : nil,
items: Nanoc::ItemCollectionWithRepsView.new(@items, view_context),
layouts: Nanoc::LayoutCollectionView.new(@layouts, view_context),
_erbout: @erbout,
Expand Down
Loading