Class: Webgen::Destination::FileSystem
- Inherits:
-
Object
- Object
- Webgen::Destination::FileSystem
- Defined in:
- lib/webgen/destination/file_system.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(website, root) ⇒ FileSystem
constructor
Create a new FileSystem 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) ⇒ Object
Write the
data
to the givenpath
.
Constructor Details
#initialize(website, root) ⇒ FileSystem
Create a new FileSystem object with the given root
path. If root
is not absolute, it is taken relative to the website directory.
18 19 20 |
# File 'lib/webgen/destination/file_system.rb', line 18 def initialize(website, root) @root = File.absolute_path(root, website.directory) 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/destination/file_system.rb', line 14 def root @root end |
Instance Method Details
#delete(path) ⇒ Object
Delete the given path
28 29 30 31 32 33 34 35 |
# File 'lib/webgen/destination/file_system.rb', line 28 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.
23 24 25 |
# File 'lib/webgen/destination/file_system.rb', line 23 def exists?(path) File.exist?(File.join(@root, path)) end |
#read(path, mode = 'rb') ⇒ Object
Return the content of the given path
which is opened in mode
.
56 57 58 |
# File 'lib/webgen/destination/file_system.rb', line 56 def read(path, mode = 'rb') File.open(File.join(@root, path), mode) {|f| f.read} end |
#write(path, data) ⇒ Object
Write the data
to the given path
.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/webgen/destination/file_system.rb', line 38 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 |