soveran-drawer 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/drawer.rb +31 -3
- data/test/drawer_test.rb +49 -0
- metadata +1 -1
data/lib/drawer.rb
CHANGED
@@ -2,11 +2,11 @@ require 'yaml'
|
|
2
2
|
|
3
3
|
class DrawerStore
|
4
4
|
def self.load(file)
|
5
|
-
YAML.load_file(file)
|
5
|
+
YAML.load_file(File.expand_path(file))
|
6
6
|
end
|
7
7
|
|
8
8
|
def self.save(object, file)
|
9
|
-
File.open(file, 'w') do |f|
|
9
|
+
File.open(File.expand_path(file), 'w') do |f|
|
10
10
|
YAML.dump(object, f)
|
11
11
|
end
|
12
12
|
end
|
@@ -14,12 +14,14 @@ end
|
|
14
14
|
|
15
15
|
class Drawer
|
16
16
|
attr :cache
|
17
|
+
attr :store
|
17
18
|
|
18
19
|
def initialize(file, store = DrawerStore)
|
20
|
+
@store = store
|
19
21
|
@cache = store.load(file) || {}
|
20
22
|
|
21
23
|
at_exit do
|
22
|
-
|
24
|
+
save(file)
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
@@ -50,4 +52,30 @@ class Drawer
|
|
50
52
|
def inspect
|
51
53
|
"Drawer count: #{cache.size}. Type 'cache' to view the content."
|
52
54
|
end
|
55
|
+
|
56
|
+
def save(file)
|
57
|
+
store.save(@cache, file)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.open(file, &block)
|
61
|
+
drawer = Drawer.new(file)
|
62
|
+
yield drawer if block_given?
|
63
|
+
drawer
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.open!(file)
|
67
|
+
create(file)
|
68
|
+
open(file)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.create(file)
|
72
|
+
unless File.exists?(file)
|
73
|
+
FileUtils.mkdir_p File.dirname(file)
|
74
|
+
FileUtils.touch(file)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.remove(file)
|
79
|
+
FileUtils.rm(file)
|
80
|
+
end
|
53
81
|
end
|
data/test/drawer_test.rb
CHANGED
@@ -11,6 +11,7 @@ end
|
|
11
11
|
class TestDrawer < Test::Unit::TestCase
|
12
12
|
context "drawer" do
|
13
13
|
setup do
|
14
|
+
Drawer.create(cache_file)
|
14
15
|
@drawer = Drawer.new(cache_file)
|
15
16
|
@drawer.flush_all
|
16
17
|
end
|
@@ -52,5 +53,53 @@ class TestDrawer < Test::Unit::TestCase
|
|
52
53
|
assert_equal [9, 25, 49], @drawer.get_multi(3, 5, 7)
|
53
54
|
end
|
54
55
|
end
|
56
|
+
|
57
|
+
context "class methods" do
|
58
|
+
setup do
|
59
|
+
Drawer.remove(cache_file)
|
60
|
+
Drawer.create(cache_file)
|
61
|
+
end
|
62
|
+
|
63
|
+
context "open" do
|
64
|
+
should "return an instance of Drawer" do
|
65
|
+
assert_kind_of Drawer, Drawer.open(cache_file)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "open with block" do
|
70
|
+
should "call the passed block" do
|
71
|
+
drawer = Drawer.open(cache_file) do |cache|
|
72
|
+
cache.set("foo", 123)
|
73
|
+
end
|
74
|
+
assert_equal 123, drawer.get('foo')
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "class methods with unexistent file" do
|
80
|
+
setup do
|
81
|
+
Drawer.remove(cache_file)
|
82
|
+
end
|
83
|
+
|
84
|
+
context "open" do
|
85
|
+
should "raise if the file doesn't exist" do
|
86
|
+
assert_raise Errno::ENOENT do
|
87
|
+
Drawer.open(cache_file)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
should "not create the file" do
|
92
|
+
Drawer.open(cache_file) rescue Errno::ENOENT
|
93
|
+
assert_equal false, File.exists?(cache_file)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "open!" do
|
98
|
+
should "create the file" do
|
99
|
+
Drawer.open!(cache_file)
|
100
|
+
assert_equal true, File.exists?(cache_file)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
55
104
|
end
|
56
105
|
end
|