Class: Attached::Storage::Local

Inherits:
Base
  • Object
show all
Defined in:
lib/attached/storage/local.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#options, #parse

Constructor Details

#initializeLocal

Create a new interface supporting save and destroy operations.

Usage:

Attached::Storage::Local.new()


15
16
17
# File 'lib/attached/storage/local.rb', line 15

def initialize()
  @mode = 0644
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



7
8
9
# File 'lib/attached/storage/local.rb', line 7

def mode
  @mode
end

Instance Method Details

#destroy(path) ⇒ Object

Destroy a file at a given path.

Parameters:

  • path - The path to destroy.



65
66
67
68
69
70
71
72
# File 'lib/attached/storage/local.rb', line 65

def destroy(path)
  path = "#{Rails.root}/public/system/#{path}"
  dirname, basename = File.split(path)
  begin
    FileUtils.rm(path)
  rescue Errno::ENOENT
  end
end

#hostObject

Access the host (e.g. /system/) for a storage service.

Usage:

storage.host


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

def host()
  "/system/"
end

#retrieve(path) ⇒ Object

Retrieve a file from a given path.

Parameters:

  • path - The path to retrieve.



54
55
56
57
# File 'lib/attached/storage/local.rb', line 54

def retrieve(path)
  path = "#{Rails.root}/public/system/#{path}"
  File.open(path)
end

#save(file, path) ⇒ Object

Save a file to a given path.

Parameters:

  • file - The file to save.

  • path - The path to save.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/attached/storage/local.rb', line 36

def save(file, path)
  path = "#{Rails.root}/public/system/#{path}"
  dirname, basename = File.split(path)
  return if file.path == path
  begin
    FileUtils.mkdir_p(dirname)
    FileUtils.cp(file.path, path)
    FileUtils.chmod(self.mode, path)
  rescue Errno::ENOENT
  end
end