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 17 def initialize(website, root) 18 @root = File.absolute_path(root, website.directory) 19 end
Public Instance Methods
delete(path)
click to toggle source
Delete the given path
# File lib/webgen/destination/file_system.rb 27 def delete(path) 28 dest = File.join(@root, path) 29 if File.directory?(dest) 30 FileUtils.rm_rf(dest) 31 else 32 FileUtils.rm(dest) 33 end 34 end
exists?(path)
click to toggle source
Return true
if the given path exists.
# File lib/webgen/destination/file_system.rb 22 def exists?(path) 23 File.exist?(File.join(@root, path)) 24 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 55 def read(path, mode = 'rb') 56 File.open(File.join(@root, path), mode) {|f| f.read} 57 end
write(path, data)
click to toggle source
Write the data
to the given path
.
# File lib/webgen/destination/file_system.rb 37 def write(path, data) 38 dest = File.join(@root, path) 39 parent_dir = File.dirname(dest) 40 FileUtils.makedirs(parent_dir) unless File.directory?(parent_dir) 41 if path[-1] == ?/ 42 FileUtils.makedirs(dest) 43 else 44 if data.kind_of?(String) 45 File.open(dest, 'wb') {|f| f.write(data) } 46 else 47 data.io('rb') do |source| 48 File.open(dest, 'wb') {|f| FileUtils.copy_stream(source, f) } 49 end 50 end 51 end 52 end