class Crubyflie::TOCCache

Table of contents can be saved to disk and re-read from there based on the CRC that they have attached. This class is used for that

Public Class Methods

new(folder=nil) click to toggle source

Initializes the cache directory @param folder [String] a path to the folder

# File lib/crubyflie/crazyflie/toc_cache.rb, line 33
def initialize(folder=nil)
    @folder = folder
    return if !@folder
    if !File.exist?(folder)
        begin
            FileUtils.mkdir_p(folder)
        rescue Errno::EACCES
            warn "Deactivating cache. Cannot create folder"
            @folder = nil
        end
    elsif !File.directory?(folder)
        @folder = nil
        warn "Deactivating cache. Folder is not a directory"
        return
    else
        begin
            test_f = File.join(folder, 'test')
            FileUtils.touch(test_f)
            FileUtils.rm(test_f)
        rescue Errno::EACCES
            @folder = nil
            warn "Deactivating cache. Cannot write to folder"
        end
    end
end

Public Instance Methods

fetch(crc) click to toggle source

Fetches a record from the cache @param crc [String] the CRC of the TOC @return [TOC,nil] A TOC if found

# File lib/crubyflie/crazyflie/toc_cache.rb, line 62
def fetch(crc)
    return nil if !@folder
    begin
        File.open(File.join(@folder, crc), 'r') do |f|
            Marshal.load(f.read)
        end
    rescue Errno::ENOENT, Errno::EACCES
        nil
    end
end
insert(crc, toc) click to toggle source

Saves a record to the cache @param crc [String] the CRC of the TOC @param toc [TOC] A TOC

# File lib/crubyflie/crazyflie/toc_cache.rb, line 76
def insert(crc, toc)
    return if !@folder
    begin
        File.open(File.join(@folder, crc), 'w') do |f|
            f.write(Marshal.dump(toc))
        end
    rescue Errno::ENOENT, Errno::EACCES
    end
end