-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cache.rb
71 lines (59 loc) · 1.51 KB
/
Cache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
59
60
61
62
63
64
65
66
67
68
69
70
71
#
# Cache.rb
# Tractor
#
# Created by Charlie Morss on 9/10/08.
# Copyright (c) 2008 AdReady. All rights reserved.
#
class Cache
class << self
def ensure_cache_directory
storeFolder = applicationSupportDirectory
mkdir(storeFolder)
end
def applicationSupportDirectory
OSX.NSHomeDirectory.stringByAppendingPathComponent(File.join('Library', 'Application Support', 'Tractor'))
end
def ticket_dir(id)
File.join(applicationSupportDirectory, "00#{id}"[-2..-1])
end
def ticket_file_name(id)
File.join(ticket_dir(id), id.to_s)
end
def mkdir(directory)
fileManager = OSX::NSFileManager.defaultManager
unless fileManager.fileExistsAtPath_isDirectory(directory, nil)
fileManager.createDirectoryAtPath_attributes(directory, nil)
end
end
def put(id, obj)
ensure_directory(id)
File.open(ticket_file_name(id),'w') do |f|
Marshal.dump(obj, f)
end
end
def get(id)
obj = nil
if cached(id)
File.open(ticket_file_name(id),'r') do |f|
obj = Marshal.load(f)
end
end
obj
end
def cached(id)
ensure_directory(id)
path = ticket_file_name(id)
fileManager = OSX::NSFileManager.defaultManager
fileManager.fileExistsAtPath(path)
end
def ensure_directory(id)
dir = ticket_dir(id)
mkdir(dir)
dir
end
def log(msg)
$stderr.puts msg
end
end
end