Class: Webgen::Output::FileSystem
- Inherits:
-
Object
- Object
- Webgen::Output::FileSystem
- 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
-
#root ⇒ Object
readonly
The root path, ie.
Instance Method Summary collapse
-
#delete(path) ⇒ Object
Delete the given
path
. -
#exists?(path) ⇒ Boolean
Return
true
if the given path exists. -
#initialize(root) ⇒ FileSystem
constructor
Create a new object with the given
root
path. -
#read(path, mode = 'rb') ⇒ Object
Return the content of the given
path
which is opened inmode
. -
#write(path, data, type = :file) ⇒ Object
Write the
data
to the givenpath
.
Methods included from WebsiteAccess
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.
18 19 20 21 22 23 24 25 |
# File 'lib/webgen/output/filesystem.rb', line 18 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
#root ⇒ Object (readonly)
The root path, ie. the path to which the root node gets rendered.
14 15 16 |
# File 'lib/webgen/output/filesystem.rb', line 14 def root @root end |
Instance Method Details
#delete(path) ⇒ Object
Delete the given path
33 34 35 36 37 38 39 40 |
# File 'lib/webgen/output/filesystem.rb', line 33 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.
28 29 30 |
# File 'lib/webgen/output/filesystem.rb', line 28 def exists?(path) File.exists?(File.join(@root, path)) end |
#read(path, mode = 'rb') ⇒ Object
Return the content of the given path
which is opened in mode
.
63 64 65 |
# File 'lib/webgen/output/filesystem.rb', line 63 def read(path, mode = 'rb') File.open(File.join(@root, path), mode) {|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
.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/webgen/output/filesystem.rb', line 44 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('rb') do |source| File.open(dest, 'wb') {|f| FileUtils.copy_stream(source, f) } end end else raise "Unsupported path type '#{type}' for #{path}" end end |