Sha256: d895e16d8d92de5dbb7bc96645b3315c51411d9715689655bb5be32d4ab6c6ff

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require 'webgen/websiteaccess'
require 'webgen/path'

module Webgen

  # This class is used to read source paths from a directory in the file system.
  class Source::FileSystem

    # A special Webgen::Path class for handling with file system paths.
    class Path < Webgen::Path

      # Create a new object with absolute path +path+ for the file system path +fs_path+.
      def initialize(path, fs_path)
        super(path) { File.open(fs_path, 'r') }
        @fs_path = fs_path
        WebsiteAccess.website.cache[[:fs_path, @fs_path]] = File.mtime(@fs_path)
      end

      # Return +true+ if the file system path used by the object has been modified.
      def changed?
        data = WebsiteAccess.website.cache[[:fs_path, @fs_path]]
        File.mtime(@fs_path) > data
      end

    end

    # The root path from which paths read.
    attr_reader :root

    # The glob (see Dir.glob for details) that is used to specify which paths under the root path
    # should be returned by #paths.
    attr_reader :glob

    # Create a new file system source for the root path +root+ using the provided +glob+.
    def initialize(root, glob = '**/*')
      if root =~ /^([a-zA-Z]:|\/)/
        @root = root
      else
        @root = File.join(WebsiteAccess.website.directory, root)
      end
      @glob = glob
    end

    # Return all paths under #root which match #glob.
    def paths
      @paths ||= Dir.glob(File.join(@root, @glob), File::FNM_DOTMATCH|File::FNM_CASEFOLD).to_set.collect! do |f|
        temp = File.expand_path(f.sub(/^#{Regexp.escape(@root)}\/?/, '/'))
        temp += '/' if File.directory?(f) && temp[-1] != ?/
        path = Path.new(temp, f)
        path
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webgen-0.5.0 lib/webgen/source/filesystem.rb
webgen-0.5.1 lib/webgen/source/filesystem.rb