Class: Skeevy::Engines::DirectoryFile
- Inherits:
-
Object
- Object
- Skeevy::Engines::DirectoryFile
- Includes:
- Skeevy::Engine
- Defined in:
- lib/skeevy/engines/directory_file.rb
Instance Attribute Summary
Attributes included from Skeevy::Engine
Instance Method Summary collapse
- #delete!(key:) ⇒ Object
- #exist?(key:, path: nil) ⇒ Boolean
-
#initialize(instance:, base_dir:, delimiter:, encoding: 'UTF-8') ⇒ DirectoryFile
constructor
A new instance of DirectoryFile.
- #path_for(key:) ⇒ Object
- #read(key:) ⇒ Object
- #write!(key:, content:) ⇒ Object
Constructor Details
#initialize(instance:, base_dir:, delimiter:, encoding: 'UTF-8') ⇒ DirectoryFile
Returns a new instance of DirectoryFile.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/skeevy/engines/directory_file.rb', line 7 def initialize(instance:, base_dir:, delimiter:, encoding: 'UTF-8') raise ArgumentError, "Instance passed was not a Skeevy::Instance" unless instance.is_a?(Skeevy::Instance) || instance.nil? @base_dir = base_dir @encoding = encoding @instance = instance @delimiter = delimiter @exists_cache = {} ensure_base_dir_exists end |
Instance Method Details
#delete!(key:) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/skeevy/engines/directory_file.rb', line 53 def delete!(key:) path = path_for(key: key) if exist?(path: path, key: nil) File.unlink(path) return true end false end |
#exist?(key:, path: nil) ⇒ Boolean
27 28 29 30 31 32 33 |
# File 'lib/skeevy/engines/directory_file.rb', line 27 def exist?(key:, path: nil) if path.nil? File.exist? path_for(key: key) else File.exist? path end end |
#path_for(key:) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/skeevy/engines/directory_file.rb', line 19 def path_for(key:) directory = key.split(@delimiter) filename = directory.pop # naive path = File.join(@base_dir, directory) ensure_exists(path: path) File.join(path, filename) end |
#read(key:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/skeevy/engines/directory_file.rb', line 35 def read(key:) path = path_for(key: key) if exist?(path: path, key: nil) File.open(path, "r:#{@encoding}") { |f| f.read } else nil end end |
#write!(key:, content:) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/skeevy/engines/directory_file.rb', line 46 def write!(key:, content:) path = path_for(key: key) File.open(path, "w:#{@encoding}") { |f| f.write content } end |