Class: Vfs::Drivers::Local
- Inherits:
-
Object
- Object
- Vfs::Drivers::Local
- Defined in:
- lib/vfs/drivers/local.rb
Defined Under Namespace
Classes: Writer
Constant Summary collapse
- DEFAULT_BUFFER =
1000 * 1024
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#attributes(path) ⇒ Object
Attributes.
- #close ⇒ Object
-
#create_dir(path) ⇒ Object
Dir.
- #delete_dir(original_path) ⇒ Object
- #delete_file(path) ⇒ Object
- #each_entry(path, query, &block) ⇒ Object
-
#initialize(options = {}) ⇒ Local
constructor
A new instance of Local.
-
#local? ⇒ Boolean
Other.
- #open(&block) ⇒ Object
-
#read_file(path, &block) ⇒ Object
File.
- #set_attributes(path, attrs) ⇒ Object
- #tmp(&block) ⇒ Object
- #to_s ⇒ Object
- #write_file(original_path, append, &block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Local
Returns a new instance of Local.
17 18 19 20 21 |
# File 'lib/vfs/drivers/local.rb', line 17 def initialize = {} = .clone @root = .delete(:root) || '' raise "invalid options #{}" unless .empty? end |
Instance Attribute Details
#buffer ⇒ Object
29 30 31 |
# File 'lib/vfs/drivers/local.rb', line 29 def buffer @buffer || DEFAULT_BUFFER end |
Instance Method Details
#attributes(path) ⇒ Object
Attributes.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/vfs/drivers/local.rb', line 35 def attributes path path = with_root path stat = ::File.stat path attrs = {} attrs[:file] = !!stat.file? attrs[:dir] = !!stat.directory? # attributes special for file system attrs[:created_at] = stat.ctime attrs[:updated_at] = stat.mtime attrs[:size] = stat.size if attrs[:file] attrs rescue Errno::ENOTDIR nil rescue Errno::ENOENT nil end |
#close ⇒ Object
26 |
# File 'lib/vfs/drivers/local.rb', line 26 def close; end |
#create_dir(path) ⇒ Object
Dir.
89 90 91 92 |
# File 'lib/vfs/drivers/local.rb', line 89 def create_dir path path = with_root path ::Dir.mkdir path end |
#delete_dir(original_path) ⇒ Object
94 95 96 97 |
# File 'lib/vfs/drivers/local.rb', line 94 def delete_dir original_path path = with_root original_path ::FileUtils.rm_r path end |
#delete_file(path) ⇒ Object
79 80 81 82 |
# File 'lib/vfs/drivers/local.rb', line 79 def delete_file path path = with_root path ::File.delete path end |
#each_entry(path, query, &block) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/vfs/drivers/local.rb', line 99 def each_entry path, query, &block path = with_root path if query path_with_trailing_slash = path == '/' ? path : "#{path}/" ::Dir["#{path_with_trailing_slash}#{query}"].each do |absolute_path| name = absolute_path.sub path_with_trailing_slash, '' block.call name, ->{::File.directory?(absolute_path) ? :dir : :file} end else ::Dir.foreach path do |name| next if name == '.' or name == '..' block.call name, ->{::File.directory?("#{path}/#{name}") ? :dir : :file} end end end |
#local? ⇒ Boolean
Other.
133 |
# File 'lib/vfs/drivers/local.rb', line 133 def local?; true end |
#open(&block) ⇒ Object
23 24 25 |
# File 'lib/vfs/drivers/local.rb', line 23 def open &block block.call self if block end |
#read_file(path, &block) ⇒ Object
File.
61 62 63 64 65 66 67 68 |
# File 'lib/vfs/drivers/local.rb', line 61 def read_file path, &block path = with_root path ::File.open path, 'r' do |is| while buff = is.gets(self.buffer || DEFAULT_BUFFER) block.call buff end end end |
#set_attributes(path, attrs) ⇒ Object
54 55 56 57 |
# File 'lib/vfs/drivers/local.rb', line 54 def set_attributes path, attrs # TODO2 set attributes. not_implemented end |
#tmp(&block) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/vfs/drivers/local.rb', line 135 def tmp &block path = "/tmp/#{rand(10**6)}" if block begin ::FileUtils.mkdir_p with_root(path) block.call path ensure ::FileUtils.rm_r with_root(path) if ::File.exist? with_root(path) end else ::FileUtils.mkdir_p with_root(path) path end end |
#to_s ⇒ Object
150 |
# File 'lib/vfs/drivers/local.rb', line 150 def to_s; '' end |