Class: Webgen::Source::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/webgen/source/filesystem.rb

Overview

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

Defined Under Namespace

Classes: Path

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, glob = '**/*') ⇒ FileSystem

Create a new file system source for the root path root using the provided glob.



39
40
41
42
43
44
45
46
# File 'lib/webgen/source/filesystem.rb', line 39

def initialize(root, glob = '**/*')
  if root =~ /^([a-zA-Z]:|\/)/
    @root = root
  else
    @root = File.join(WebsiteAccess.website.directory, root)
  end
  @glob = glob
end

Instance Attribute Details

#globObject (readonly)

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



36
37
38
# File 'lib/webgen/source/filesystem.rb', line 36

def glob
  @glob
end

#rootObject (readonly)

The root path from which paths read.



32
33
34
# File 'lib/webgen/source/filesystem.rb', line 32

def root
  @root
end

Instance Method Details

#pathsObject

Return all paths under #root which match #glob.



49
50
51
52
53
54
55
56
57
# File 'lib/webgen/source/filesystem.rb', line 49

def paths
  @paths ||= Dir.glob(File.join(@root, @glob), File::FNM_DOTMATCH|File::FNM_CASEFOLD).to_set.collect do |f|
    next unless File.exists?(f) # handle invalid links
    temp = Pathname.new(f.sub(/^#{Regexp.escape(@root)}\/?/, '/')).cleanpath.to_s
    temp += '/' if File.directory?(f) && temp[-1] != ?/
    path = Path.new(temp, f)
    path
  end.compact
end