Sha256: c5dffbbca881c1594fbede134ede8f87ad44046c6e5f5de0b84530f0957d4b28

Contents?: true

Size: 1.87 KB

Versions: 18

Compression:

Stored size: 1.87 KB

Contents

# -*- encoding: utf-8 -*-

require 'webgen/source'
require 'set'

module Webgen
  class Source

    # This source class is used to stack several sources together.
    #
    # It serves two purposes:
    #
    # * First, it can be used to access more than one source. This is useful when your website
    #   consists of more than one source directory and you want to use all of them.
    #
    # * Second, sources can be mounted on specific directories. For example, a folder with images
    #   that you don't want to copy to the main website source directory can be mounted under
    #   '/images' so that they are available nonetheless.
    #
    # Also be aware that when a path is returned by a source that has already be returned by a prior
    # source, it is discarded and not used.
    class Stacked

      # Return the stack of [mount point, source object] entries.
      attr_reader :stack

      # Create a new stack.
      #
      # The optional +map+ parameter is used to provide mappings of mount points to source objects.
      # It should be an array of two-element arrays which contain an absolute directory (ie.
      # starting and ending with a slash) and a source object.
      def initialize(website, map = {})
        @stack = []
        map.each do |mp, source|
          raise "Invalid mount point specified: #{mp}" unless mp =~ /^\//
          @stack << [mp, source]
        end
      end

      # Return all paths returned by the sources in the stack.
      #
      # Since the stack is ordered, paths returned by later source objects are not used if a prior
      # source object has returned the same path.
      def paths
        if !defined?(@paths)
          @paths = Set.new
          @stack.each do |mp, source|
            source.paths.each do |path|
              @paths.add?(path.mount_at(mp))
            end
          end
        end
        @paths
      end

    end

  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
webgen-1.7.3 lib/webgen/source/stacked.rb
webgen-1.7.2 lib/webgen/source/stacked.rb
webgen-1.7.1 lib/webgen/source/stacked.rb
webgen-1.7.0 lib/webgen/source/stacked.rb
webgen-1.6.0 lib/webgen/source/stacked.rb
webgen-1.5.2 lib/webgen/source/stacked.rb
webgen-1.5.1 lib/webgen/source/stacked.rb
webgen-1.5.0 lib/webgen/source/stacked.rb
webgen-1.4.1 lib/webgen/source/stacked.rb
webgen-1.4.0 lib/webgen/source/stacked.rb
webgen-1.3.0 lib/webgen/source/stacked.rb
webgen-1.2.1 lib/webgen/source/stacked.rb
webgen-1.2.0 lib/webgen/source/stacked.rb
webgen-1.1.0 lib/webgen/source/stacked.rb
webgen-1.0.0 lib/webgen/source/stacked.rb
webgen-1.0.0.beta3 lib/webgen/source/stacked.rb
webgen-1.0.0.beta2 lib/webgen/source/stacked.rb
webgen-1.0.0.beta1 lib/webgen/source/stacked.rb