Class: TdCLI::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/todoist/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



6
7
8
9
10
11
12
13
14
# File 'lib/todoist/cache.rb', line 6

def initialize
    @_cache = {}
    unless File.directory?(DATA_DIR)
        FileUtils.mkdir(DATA_DIR)
    end

    @file_path = File.join(DATA_DIR, 'cache.json')
    load()
end

Instance Method Details

#get(item) ⇒ Object



20
21
22
# File 'lib/todoist/cache.rb', line 20

def get(item)
    return @_cache[item]
end

#loadObject

load the cache from the data dir



25
26
27
28
29
30
31
32
# File 'lib/todoist/cache.rb', line 25

def load
    if File.exist?(@file_path)

      @_cache = JSON File.open(@file_path, &:read).strip
    else
        $stderr.puts "#{@file_path} does not exist"
    end
end

#saveObject

save the cache to the data dir



35
36
37
38
# File 'lib/todoist/cache.rb', line 35

def save
    cache_json = JSON.generate @_cache
    File.open(@file_path, 'w') { |file| file.write(cache_json) }
end

#set(data) ⇒ Object



16
17
18
# File 'lib/todoist/cache.rb', line 16

def set(data)
    @_cache = data
end