Class: OmniStore::Storage::Local::Mountpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/omnistore/storage/local/mountpoint.rb

Constant Summary collapse

MEGABYTE =
1024*1024

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dirObject (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, 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) ⇒ Object



25
26
27
# File 'lib/omnistore/storage/local/mountpoint.rb', line 25

def delete(path)
  FileUtils.rm(expand(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("#{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) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/omnistore/storage/local/mountpoint.rb', line 21

def exist?(path)
  File.exist?(expand(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, 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

#nameObject



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, 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 = {}) ⇒ Object



17
18
19
# 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 = {}) ⇒ 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, 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