Sha256: db051ee59b71d06f1148f5d3bba48114b8de0af657dfff499a18c5d73b7df1b5

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

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

require 'webgen/websiteaccess'
require 'webgen/source'
require 'webgen/common'

module Webgen::Source

  # This class is used to provide access to sources provided by resources.
  class Resource

    include Webgen::WebsiteAccess

    # The glob (see File.fnmatch) specifying the resources.
    attr_reader :glob

    # The glob (see File.fnmatch) specifying the paths that should be used from the resources.
    attr_reader :paths_glob

    # The prefix that should optionally be stripped from the paths.
    attr_reader :strip_prefix

    # Create a new resource source for the the +glob+ and use only those paths matching +paths_glob+
    # while stripping +strip_prefix+ off the path.
    def initialize(glob, paths_glob = nil, strip_prefix = nil)
      @glob, @paths_glob, @strip_prefix = glob, paths_glob, strip_prefix
    end

    # Return all paths associated with the resources identified by #glob.
    def paths
      if !defined?(@paths)
        stack = Stacked.new
        website.config['resources'].select {|name, infos| File.fnmatch(@glob, name)}.sort.each do |name, infos|
          stack.add([['/', Webgen::Common.const_for_name(infos.first).new(*infos[1..-1])]])
        end
        @paths = stack.paths
        @paths = @paths.select {|p| File.fnmatch(@paths_glob, p)} if @paths_glob
        @paths.collect! {|p| p.mount_at('/', @strip_prefix)} if @strip_prefix
      end
      @paths
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
webgen-0.5.17 lib/webgen/source/resource.rb
webgen-0.5.15 lib/webgen/source/resource.rb
webgen-0.5.14 lib/webgen/source/resource.rb
webgen-0.5.13 lib/webgen/source/resource.rb
webgen-0.5.12 lib/webgen/source/resource.rb
webgen-0.5.11 lib/webgen/source/resource.rb