Class: Crubyflie::TOCCache

Inherits:
Object
  • Object
show all
Defined in:
lib/crubyflie/crazyflie/toc_cache.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(folder = nil) ⇒ TOCCache

Initializes the cache directory

Parameters:

  • folder (String) (defaults to: nil)

    a path to the folder


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/crubyflie/crazyflie/toc_cache.rb', line 34

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

Instance Method Details

#fetch(crc) ⇒ TOC?

Fetches a record from the cache

Parameters:

  • crc (String)

    the CRC of the TOC

Returns:

  • (TOC, nil)

    A TOC if found


63
64
65
66
67
68
69
70
71
72
# File 'lib/crubyflie/crazyflie/toc_cache.rb', line 63

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) ⇒ Object

Saves a record to the cache

Parameters:

  • crc (String)

    the CRC of the TOC

  • toc (TOC)

    A TOC


77
78
79
80
81
82
83
84
85
# File 'lib/crubyflie/crazyflie/toc_cache.rb', line 77

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