class Skeevy::Engines::DirectoryFile
Public Class Methods
new(instance: nil, base_dir:, delimiter:, encoding: 'UTF-8')
click to toggle source
# File lib/skeevy/engines/directory_file.rb, line 7 def initialize(instance: nil, 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
Public Instance Methods
delete!(key:)
click to toggle source
# File lib/skeevy/engines/directory_file.rb, line 49 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)
click to toggle source
# 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
keys()
click to toggle source
# File lib/skeevy/engines/directory_file.rb, line 58 def keys @files = [] check_directory(dir: "#{@base_dir}/*") @files end
path_for(key:)
click to toggle source
# 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:)
click to toggle source
# File lib/skeevy/engines/directory_file.rb, line 35 def read(key:) path = path_for(key: key) if exist?(path: path, key: nil) IO.binread(path) else nil end end
write!(key:, content:)
click to toggle source
# File lib/skeevy/engines/directory_file.rb, line 44 def write!(key:, content:) path = path_for(key: key) IO.binwrite(path, content) end
Private Instance Methods
check_directory(dir:, prefix: '')
click to toggle source
# File lib/skeevy/engines/directory_file.rb, line 75 def check_directory(dir:, prefix: '') Dir.glob(dir).each do |f| if(File.directory? f) check_directory(dir: "#{f}/*", prefix: f) else @files << f.gsub("#{@base_dir}/", '').gsub('/', @delimiter) end end end
ensure_base_dir_exists()
click to toggle source
# File lib/skeevy/engines/directory_file.rb, line 71 def ensure_base_dir_exists ensure_exists(path: @base_dir) end
ensure_exists(path:)
click to toggle source
# File lib/skeevy/engines/directory_file.rb, line 66 def ensure_exists(path:) FileUtils.mkdir_p(path) if @exists_cache[path].nil? @exists_cache[path] = true end