Class: Webgen::Output::FileSystem

Inherits:
Object
  • Object
show all
Includes:
WebsiteAccess
Defined in:
lib/webgen/output/filesystem.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WebsiteAccess

included, website

Constructor Details

#initialize(root) ⇒ FileSystem

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



16
17
18
19
20
21
22
23
# File 'lib/webgen/output/filesystem.rb', line 16

def initialize(root)
  #TODO: copied from source/filesystem.rb
  if root =~ /^([a-zA-Z]:|\/)/
    @root = root
  else
    @root = File.join(website.directory, root)
  end
end

Instance Attribute Details

#rootObject (readonly)

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



12
13
14
# File 'lib/webgen/output/filesystem.rb', line 12

def root
  @root
end

Instance Method Details

#delete(path) ⇒ Object

Delete the given path



31
32
33
34
35
36
37
38
# File 'lib/webgen/output/filesystem.rb', line 31

def delete(path)
  dest = File.join(@root, path)
  if File.directory?(dest)
    FileUtils.rm_rf(dest)
  else
    FileUtils.rm(dest)
  end
end

#exists?(path) ⇒ Boolean

Return true if the given path exists.

Returns:

  • (Boolean)


26
27
28
# File 'lib/webgen/output/filesystem.rb', line 26

def exists?(path)
  File.exists?(File.join(@root, path))
end

#read(path) ⇒ Object

Return the content of the given path.



61
62
63
# File 'lib/webgen/output/filesystem.rb', line 61

def read(path)
  File.open(File.join(@root, path), 'rb') {|f| f.read}
end

#write(path, data, type = :file) ⇒ Object

Write the data to the given path. The type parameter specifies the type of the path to be created which can either be :file or :directory.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/webgen/output/filesystem.rb', line 42

def write(path, data, type = :file)
  dest = File.join(@root, path)
  FileUtils.makedirs(File.dirname(dest))
  if type == :directory
    FileUtils.makedirs(dest)
  elsif type == :file
    if data.kind_of?(String)
      File.open(dest, 'wb') {|f| f.write(data) }
    else
      data.stream do |source|
        File.open(dest, 'wb') {|f| FileUtils.copy_stream(source, f) }
      end
    end
  else
    raise "Unsupported path type '#{type}' for #{path}"
  end
end