Class: ConsoleAgent::Storage::FileStorage
- Defined in:
- lib/console_agent/storage/file_storage.rb
Instance Attribute Summary collapse
-
#root_path ⇒ Object
readonly
Returns the value of attribute root_path.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #exists?(key) ⇒ Boolean
-
#initialize(root_path = nil) ⇒ FileStorage
constructor
A new instance of FileStorage.
- #list(pattern) ⇒ Object
- #read(key) ⇒ Object
- #write(key, content) ⇒ Object
Constructor Details
#initialize(root_path = nil) ⇒ FileStorage
Returns a new instance of FileStorage.
9 10 11 |
# File 'lib/console_agent/storage/file_storage.rb', line 9 def initialize(root_path = nil) @root_path = root_path || default_root end |
Instance Attribute Details
#root_path ⇒ Object (readonly)
Returns the value of attribute root_path.
7 8 9 |
# File 'lib/console_agent/storage/file_storage.rb', line 7 def root_path @root_path end |
Instance Method Details
#delete(key) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/console_agent/storage/file_storage.rb', line 38 def delete(key) path = full_path(key) return false unless File.exist?(path) File.delete(path) true rescue Errno::EACCES, Errno::EROFS, IOError => e raise StorageError, "Cannot delete #{key}: #{e.}" end |
#exists?(key) ⇒ Boolean
34 35 36 |
# File 'lib/console_agent/storage/file_storage.rb', line 34 def exists?(key) File.exist?(full_path(key)) end |
#list(pattern) ⇒ Object
28 29 30 31 32 |
# File 'lib/console_agent/storage/file_storage.rb', line 28 def list(pattern) Dir.glob(File.join(@root_path, pattern)).sort.map do |path| path.sub("#{@root_path}/", '') end end |
#read(key) ⇒ Object
13 14 15 16 17 |
# File 'lib/console_agent/storage/file_storage.rb', line 13 def read(key) path = full_path(key) return nil unless File.exist?(path) File.read(path) end |
#write(key, content) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/console_agent/storage/file_storage.rb', line 19 def write(key, content) path = full_path(key) FileUtils.mkdir_p(File.dirname(path)) File.write(path, content) true rescue Errno::EACCES, Errno::EROFS, IOError => e raise StorageError, "Cannot write #{key}: #{e.}" end |