class Moneta::BasicFile
Public Class Methods
new(options = {})
click to toggle source
# File lib/moneta/basic_file.rb, line 24 def initialize(options = {}) @namespace = options[:namespace] @directory = ::File.join(options[:path], @namespace.to_s) @expires = !options[:skip_expires] ensure_directory_created(@directory) end
Public Instance Methods
[](key)
click to toggle source
# File lib/moneta/basic_file.rb, line 39 def [](key) if ::File.exist?(path(key)) data = raw_get(key) if @expires if data[:expires_at].nil? || data[:expires_at] > Time.now data[:value] else delete!(key) end end end end
[]=(key, value)
click to toggle source
# File lib/moneta/basic_file.rb, line 56 def []=(key, value) store(key, value) end
clear()
click to toggle source
# File lib/moneta/basic_file.rb, line 92 def clear FileUtils.rm_rf(@directory) FileUtils.mkdir(@directory) end
delete(key)
click to toggle source
# File lib/moneta/basic_file.rb, line 86 def delete(key) value = self[key] delete!(key) value end
delete!(key)
click to toggle source
# File lib/moneta/basic_file.rb, line 80 def delete!(key) FileUtils.rm(path(key)) nil rescue Errno::ENOENT end
key?(key)
click to toggle source
# File lib/moneta/basic_file.rb, line 33 def key?(key) !self[key].nil? end
Also aliased as: has_key?
raw_get(key)
click to toggle source
# File lib/moneta/basic_file.rb, line 52 def raw_get(key) Marshal.load(::File.read(path(key))) end
store(key, value, options = {})
click to toggle source
# File lib/moneta/basic_file.rb, line 60 def store(key, value, options = {}) ensure_directory_created(::File.dirname(path(key))) ::File.open(path(key), "w") do |file| if @expires data = {:value => value} if options[:expires_in] data[:expires_at] = Time.now + options[:expires_in] end contents = Marshal.dump(data) else contents = Marshal.dump(value) end file.puts(contents) end end
update_key(key, options)
click to toggle source
# File lib/moneta/basic_file.rb, line 76 def update_key(key, options) store(key, self[key], options) end
Private Instance Methods
ensure_directory_created(directory_path)
click to toggle source
# File lib/moneta/basic_file.rb, line 102 def ensure_directory_created(directory_path) if ::File.file?(directory_path) raise StandardError, "The path you supplied #{directory_path} is a file" elsif !::File.exists?(directory_path) FileUtils.mkdir_p(directory_path) end end
path(key)
click to toggle source
# File lib/moneta/basic_file.rb, line 98 def path(key) ::File.join(@directory, key.to_s) end