Sha256: c29b340f1f96ea9999c1aea93cca59af926d7d22280093d63840a9dbfbcacc60

Contents?: true

Size: 1.51 KB

Versions: 48

Compression:

Stored size: 1.51 KB

Contents

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('/') + '/'
      parent_identifier = '/' if parent_identifier == '//'

      parent = @context.items[parent_identifier]

      parent && self.class.new(parent, @context)
    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

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
nanoc-4.4.7 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.4.6 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.4.5 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.4.4 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.4.3 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.4.2 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.4.1 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.4.0 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.8 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.7 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.6 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.5 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.4 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.3 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.2 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.1 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.3.0 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.2.4 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.2.3 lib/nanoc/base/views/item_without_reps_view.rb
nanoc-4.2.2 lib/nanoc/base/views/item_without_reps_view.rb