class Webgen::Destination::FileSystem

This class uses the file systems as output device. On initialization a root path is set and all other operations are taken relative to this root path.

Attributes

root[R]

The root path, ie. the path to which the root node gets rendered.

Public Class Methods

new(website, root) click to toggle source

Create a new FileSystem object with the given root path. If root is not absolute, it is taken relative to the website directory.

# File lib/webgen/destination/file_system.rb, line 17
def initialize(website, root)
  @root = File.absolute_path(root, website.directory)
end

Public Instance Methods

delete(path) click to toggle source

Delete the given path

# File lib/webgen/destination/file_system.rb, line 27
def delete(path)
  dest = File.join(@root, path)
  if File.directory?(dest)
    FileUtils.rm_rf(dest)
  else
    FileUtils.rm(dest)
  end
end
exists?(path) click to toggle source

Return true if the given path exists.

# File lib/webgen/destination/file_system.rb, line 22
def exists?(path)
  File.exist?(File.join(@root, path))
end
read(path, mode = 'rb') click to toggle source

Return the content of the given path which is opened in mode.

# File lib/webgen/destination/file_system.rb, line 55
def read(path, mode = 'rb')
  File.open(File.join(@root, path), mode) {|f| f.read}
end
write(path, data) click to toggle source

Write the data to the given path.

# File lib/webgen/destination/file_system.rb, line 37
def write(path, data)
  dest = File.join(@root, path)
  parent_dir = File.dirname(dest)
  FileUtils.makedirs(parent_dir) unless File.directory?(parent_dir)
  if path[-1] == ?/
    FileUtils.makedirs(dest)
  else
    if data.kind_of?(String)
      File.open(dest, 'wb') {|f| f.write(data) }
    else
      data.io('rb') do |source|
        File.open(dest, 'wb') {|f| FileUtils.copy_stream(source, f) }
      end
    end
  end
end