Class: OmniStore::Storage::Local::Mountpoint
- Inherits:
-
Object
- Object
- OmniStore::Storage::Local::Mountpoint
- Defined in:
- lib/omnistore/storage/local/mountpoint.rb
Constant Summary collapse
- MEGABYTE =
1024*1024
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
Instance Method Summary collapse
- #copy(src, dest, other = self, options = {}) ⇒ Object
- #delete(path) ⇒ Object
- #delete_if(dir = '', &block) ⇒ Object
- #exist?(path) ⇒ Boolean
-
#initialize(name, dir) ⇒ Mountpoint
constructor
A new instance of Mountpoint.
- #move(src, dest, other = self, options = {}) ⇒ Object
- #name ⇒ Object
- #read(path, options = {}, &block) ⇒ Object
- #url(key = nil, options = {}) ⇒ Object
- #write(path, options_or_data = nil, options = {}) ⇒ Object
Constructor Details
#initialize(name, dir) ⇒ Mountpoint
Returns a new instance of Mountpoint.
8 9 10 11 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 8 def initialize(name, dir) @name = name @dir = dir end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
6 7 8 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 6 def dir @dir end |
Instance Method Details
#copy(src, dest, other = self, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 77 def copy(src, dest, other = self, = {}) force = [:force] opts = .dup opts.delete_if {|k, v| !FileUtils.have_option?(:copy, k)} src_path = (src) dest_path = (dest, other.dir) FileUtils.mkdir_p(File.dirname(dest_path)) if force FileUtils.copy(src_path, dest_path, opts) end |
#delete(path) ⇒ Object
25 26 27 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 25 def delete(path) FileUtils.rm((path)) end |
#delete_if(dir = '', &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 29 def delete_if(dir = '', &block) Dir.glob("#{(dir)}/**/*").reverse.each do |path| if yield(path.sub(/^#{@dir}\//, '')) if File.directory?(path) FileUtils.rmdir(path) else FileUtils.rm(path) end end end end |
#exist?(path) ⇒ Boolean
21 22 23 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 21 def exist?(path) File.exist?((path)) end |
#move(src, dest, other = self, options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 67 def move(src, dest, other = self, = {}) force = [:force] opts = .dup opts.delete_if {|k, v| !FileUtils.have_option?(:mv, k)} src_path = (src) dest_path = (dest, other.dir) FileUtils.mkdir_p(File.dirname(dest_path)) if force FileUtils.mv(src_path, dest_path, opts) end |
#name ⇒ Object
13 14 15 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 13 def name @name end |
#read(path, options = {}, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 41 def read(path, = {}, &block) size = [:chunk_size] || MEGABYTE open((path), 'rb') do |f| if block_given? block.call(f.read(size)) until f.eof? else f.read end end end |
#url(key = nil, options = {}) ⇒ Object
17 18 19 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 17 def url(key = nil, = {}) "file://#{dir}/#{key}" end |
#write(path, options_or_data = nil, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/omnistore/storage/local/mountpoint.rb', line 52 def write(path, = nil, = {}) opts = (, ) size = opts[:chunk_size] || MEGABYTE data = convert_data_to_io_obj(opts) = (path) FileUtils.mkdir_p(File.dirname()) if [:force] begin open(, 'wb') do |f| f.write(data.read(size)) until data.eof? end ensure data.close unless data.closed? end end |