Sha256: 52aacf1451f1cc245656e6241789dd7f8bdeb1f8f877d9834ddcb7ebdaa9016f

Contents?: true

Size: 1.72 KB

Versions: 49

Compression:

Stored size: 1.72 KB

Contents

module Nanoc
  class IdentifiableCollectionView < ::Nanoc::View
    include Enumerable

    # @api private
    def initialize(objects, context)
      super(context)
      @objects = objects
    end

    # @api private
    def unwrap
      @objects
    end

    # @abstract
    #
    # @api private
    def view_class
      raise NotImplementedError
    end

    # Calls the given block once for each object, passing that object as a parameter.
    #
    # @yieldparam [#identifier] object
    #
    # @yieldreturn [void]
    #
    # @return [self]
    def each
      @objects.each { |i| yield view_class.new(i, @context) }
      self
    end

    # @return [Integer]
    def size
      @objects.size
    end

    # Finds all objects whose identifier matches the given argument.
    #
    # @param [String, Regex] arg
    #
    # @return [Enumerable]
    def find_all(arg)
      pat = Nanoc::Int::Pattern.from(arg)
      select { |i| pat.match?(i.identifier) }
    end

    # @overload [](string)
    #
    #   Finds the object whose identifier matches the given string.
    #
    #   If the glob syntax is enabled, the string can be a glob, in which case
    #   this method finds the first object that matches the given glob.
    #
    #   @param [String] string
    #
    #   @return [nil] if no object matches the string
    #
    #   @return [#identifier] if an object was found
    #
    # @overload [](regex)
    #
    #   Finds the object whose identifier matches the given regular expression.
    #
    #   @param [Regex] regex
    #
    #   @return [nil] if no object matches the regex
    #
    #   @return [#identifier] if an object was found
    def [](arg)
      res = @objects[arg]
      res && view_class.new(res, @context)
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
nanoc-4.7.4 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.7.3 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.7.2 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.7.1 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.7.0 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.6.4 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.6.3 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.6.2 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.6.1 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.6.0 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.5.4 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.5.3 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.5.2 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.5.1 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.5.0 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.4.7 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.4.6 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.4.5 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.4.4 lib/nanoc/base/views/identifiable_collection_view.rb
nanoc-4.4.3 lib/nanoc/base/views/identifiable_collection_view.rb