Class: Saviour::LocalStorage
- Inherits:
-
Object
- Object
- Saviour::LocalStorage
- Defined in:
- lib/saviour/local_storage.rb
Instance Method Summary collapse
- #delete(path) ⇒ Object
- #exists?(path) ⇒ Boolean
-
#initialize(opts = {}) ⇒ LocalStorage
constructor
A new instance of LocalStorage.
- #public_url(path) ⇒ Object
- #read(path) ⇒ Object
- #write(contents, path) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ LocalStorage
Returns a new instance of LocalStorage.
5 6 7 8 9 |
# File 'lib/saviour/local_storage.rb', line 5 def initialize(opts = {}) @local_prefix = opts[:local_prefix] @public_url_prefix = opts[:public_url_prefix] @overwrite_protection = opts.fetch(:overwrite_protection, true) end |
Instance Method Details
#delete(path) ⇒ Object
28 29 30 31 32 |
# File 'lib/saviour/local_storage.rb', line 28 def delete(path) assert_exists(path) ::File.delete(real_path(path)) ensure_removed_empty_dir(path) end |
#exists?(path) ⇒ Boolean
34 35 36 |
# File 'lib/saviour/local_storage.rb', line 34 def exists?(path) ::File.file?(real_path(path)) end |
#public_url(path) ⇒ Object
38 39 40 41 |
# File 'lib/saviour/local_storage.rb', line 38 def public_url(path) raise(RuntimeError, "You must provide a `public_url_prefix`") unless public_url_prefix ::File.join(public_url_prefix, path) end |
#read(path) ⇒ Object
23 24 25 26 |
# File 'lib/saviour/local_storage.rb', line 23 def read(path) assert_exists(path) ::File.open(real_path(path)).read end |
#write(contents, path) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/saviour/local_storage.rb', line 11 def write(contents, path) raise(RuntimeError, "The path you're trying to write already exists!") if @overwrite_protection && exists?(path) dir = ::File.dirname(real_path(path)) FileUtils.mkdir_p(dir) unless ::File.directory?(dir) ::File.open(real_path(path), "w") do |f| f.binmode f.write(contents) end end |