class PersistentHash

Public Class Methods

new(file_path, commit_interval = 1) click to toggle source
# File lib/meimei/persistent_hash.rb, line 2
def initialize(file_path, commit_interval = 1)
        @commit_interval = commit_interval
        @commit_count = 0
        @file_path = file_path
        restore!
        unless @hash.is_a?(Hash)
                @hash = {}
                commit!
        end
end

Public Instance Methods

[]=(key, value) click to toggle source
# File lib/meimei/persistent_hash.rb, line 31
def []=(key, value)
        @hash[key] = value
        @commit_count += 1
        if @commit_count >= @commit_interval
                @commit_count = 0
                commit!
        end
end
commit!() click to toggle source
# File lib/meimei/persistent_hash.rb, line 25
def commit!
        File.open(@file_path, File::WRONLY | File::CREAT) do |f|
                Marshal.dump(@hash, f)
        end
end
method_missing(name, *params, &block) click to toggle source
# File lib/meimei/persistent_hash.rb, line 40
def method_missing(name, *params, &block)
        @hash.send(name, *params, &block)
end
restore!() click to toggle source
# File lib/meimei/persistent_hash.rb, line 13
def restore!
        if File.exist?(@file_path)
                mode = File::RDONLY
        else
                return
        end

        File.open(@file_path, mode) do |f|
                @hash = Marshal.restore(f)
        end
end