class OmniStore::Storage::Local::Mountpoint
Constants
- MEGABYTE
Attributes
dir[R]
Public Class Methods
new(name, dir)
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 8 def initialize(name, dir) @name = name @dir = dir end
Public Instance Methods
copy(src, dest, other = self, options = {})
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 77 def copy(src, dest, other = self, options = {}) force = options[:force] opts = options.dup opts.delete_if {|k, v| !FileUtils.have_option?(:copy, k)} src_path = expand(src) dest_path = expand(dest, other.dir) FileUtils.mkdir_p(File.dirname(dest_path)) if force FileUtils.copy(src_path, dest_path, opts) end
delete(path)
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 25 def delete(path) FileUtils.rm(expand(path)) end
delete_if(dir = '') { |sub(/^#{dir}\//, '')| ... }
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 29 def delete_if(dir = '', &block) Dir.glob("#{expand(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)
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 21 def exist?(path) File.exist?(expand(path)) end
move(src, dest, other = self, options = {})
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 67 def move(src, dest, other = self, options = {}) force = options[:force] opts = options.dup opts.delete_if {|k, v| !FileUtils.have_option?(:mv, k)} src_path = expand(src) dest_path = expand(dest, other.dir) FileUtils.mkdir_p(File.dirname(dest_path)) if force FileUtils.mv(src_path, dest_path, opts) end
name()
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 13 def name @name end
read(path, options = {}, &block)
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 41 def read(path, options = {}, &block) size = options[:chunk_size] || MEGABYTE open(expand(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 = {})
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 17 def url(key = nil, options = {}) "file://#{dir}/#{key}" end
write(path, options_or_data = nil, options = {})
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 52 def write(path, options_or_data = nil, options = {}) opts = convert_args_to_options_hash(options_or_data, options) size = opts[:chunk_size] || MEGABYTE data = convert_data_to_io_obj(opts) expanded_path = expand(path) FileUtils.mkdir_p(File.dirname(expanded_path)) if options[:force] begin open(expanded_path, 'wb') do |f| f.write(data.read(size)) until data.eof? end ensure data.close unless data.closed? end end
Private Instance Methods
convert_args_to_options_hash(*args)
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 93 def convert_args_to_options_hash(*args) case args.count when 0 then {} when 1 then args[0].is_a?(Hash) ? args[0] : { :data => args[0] } when 2 then args[1].merge(:data => args[0]) else msg = "expected 0, 1 or 2 arguments, got #{args.count}" raise ArgumentError, msg end end
convert_data_to_io_obj(options)
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 104 def convert_data_to_io_obj(options) data = options.delete(:data) if data.is_a?(String) data.force_encoding("BINARY") if data.respond_to?(:force_encoding) StringIO.new(data) elsif data.is_a?(Pathname) open_file(data.to_s) elsif data.respond_to?(:read) and data.respond_to?(:eof?) data else msg = "invalid :data option, expected a String, Pathname or " msg << "an object that responds to #read and #eof?" raise ArgumentError, msg end end
expand(path, dir = @dir)
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 89 def expand(path, dir = @dir) File.expand_path(path, dir) end
open_file(path)
click to toggle source
# File lib/omnistore/storage/local/mountpoint.rb, line 120 def open_file(path) file_opts = ['rb'] file_opts << { :encoding => "BINARY" } if Object.const_defined?(:Encoding) File.open(path, *file_opts) end