dm-xml-adapter 0.52 → 0.53
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dm-xml-adapter.rb +23 -17
- data/spec/dm-xml-adapter_spec.rb +125 -106
- metadata +2 -2
data/lib/dm-xml-adapter.rb
CHANGED
@@ -15,19 +15,19 @@ module DataMapper::Adapters
|
|
15
15
|
class XmlAdapterCache
|
16
16
|
|
17
17
|
def initialize
|
18
|
-
|
19
|
-
|
18
|
+
#@@logger = Log4r::Logger.new 'cache'
|
19
|
+
#@@logger.outputters = Log4r::Outputter.stdout
|
20
20
|
@mtimes = Hash.new()
|
21
21
|
@classes = Hash.new()
|
22
22
|
end
|
23
23
|
|
24
24
|
def set_mtime(file, mtime)
|
25
|
-
|
25
|
+
#@@logger.info("Saved mtime, #{file} -> #{mtime}")
|
26
26
|
@mtimes[file] = mtime
|
27
27
|
end
|
28
28
|
|
29
29
|
def delete_mtime(file)
|
30
|
-
|
30
|
+
#@@logger.info("deleting the following mtime: #{file}")
|
31
31
|
@mtimes.delete(file)
|
32
32
|
end
|
33
33
|
|
@@ -36,8 +36,8 @@ module DataMapper::Adapters
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def put(classname, id, object)
|
39
|
-
|
40
|
-
|
39
|
+
# @@logger.info("first one: #{@classes.inspect}")
|
40
|
+
# @@logger.info("CACHE INSERT: #{classname} #{id} #{object.inspect}")
|
41
41
|
|
42
42
|
# check to see if we have a classname entry
|
43
43
|
if (@classes[classname] == nil)
|
@@ -52,20 +52,20 @@ module DataMapper::Adapters
|
|
52
52
|
def delete(classname, id)
|
53
53
|
# @@logger.info("deleted the following from cache: #{classname} #{id.to_s}")
|
54
54
|
hash = @classes[classname]
|
55
|
-
|
55
|
+
#@@logger.info("the hash is: #{hash.inspect}")
|
56
56
|
unless hash == nil
|
57
57
|
hash.delete(id.to_s)
|
58
58
|
end
|
59
|
-
|
59
|
+
#@@logger.info("the hash is: #{hash.inspect}")
|
60
60
|
end
|
61
61
|
|
62
62
|
def get(classname, id)
|
63
63
|
# @@logger.info("CACHE FETCH: #{classname} #{id}")
|
64
|
-
|
64
|
+
#@@logger.info("Master classes: #{@classes.inspect}")
|
65
65
|
hash = @classes[classname]
|
66
|
-
|
66
|
+
#@@logger.info("the hash is: #{hash.inspect}")
|
67
67
|
object = hash[id]
|
68
|
-
|
68
|
+
#@@logger.info("The object is: #{object}")
|
69
69
|
return object
|
70
70
|
end
|
71
71
|
end
|
@@ -84,7 +84,7 @@ module DataMapper::Adapters
|
|
84
84
|
@cache = XmlAdapterCache.new
|
85
85
|
|
86
86
|
# @@logger = Log4r::Logger.new 'adapter'
|
87
|
-
|
87
|
+
#@@logger.outputters = Log4r::Outputter.stdout
|
88
88
|
|
89
89
|
end
|
90
90
|
|
@@ -172,16 +172,16 @@ module DataMapper::Adapters
|
|
172
172
|
file = File.new(filename)
|
173
173
|
# see if we have a nice cached version
|
174
174
|
if (@cache.get_mtime(filename) == nil or file.mtime > @cache.get_mtime(filename))
|
175
|
-
|
175
|
+
# @@logger.info("CACHE MISS")
|
176
176
|
# the file's newer, so we gotta load it up
|
177
177
|
object = file_to_object(filename, model)
|
178
178
|
objects << object
|
179
179
|
@cache.put(model.to_s, object.key.first, object)
|
180
180
|
@cache.set_mtime(filename, file.mtime)
|
181
181
|
else
|
182
|
-
|
183
|
-
|
184
|
-
|
182
|
+
# @@logger.info("YES !!!")
|
183
|
+
# @@logger.info("CACHE HIT")
|
184
|
+
# @@logger.info("the file is: #{file}")
|
185
185
|
# the file was older than what we had so cache is fine
|
186
186
|
objects << @cache.get(model.to_s, file_to_id(filename))
|
187
187
|
end
|
@@ -263,6 +263,11 @@ module DataMapper::Adapters
|
|
263
263
|
end
|
264
264
|
|
265
265
|
def save(resource)
|
266
|
+
|
267
|
+
# since we're saving, purge the cache
|
268
|
+
@cache.delete(resource.class.to_s, resource.key.first)
|
269
|
+
@cache.delete_mtime(class_name_to_file(resource.class.to_s, resource.key.first))
|
270
|
+
|
266
271
|
# file = File.join(class_name_to_file(resource.class.to_s, resource.key.first))
|
267
272
|
# see if the directory exists, if it doesn't, we need to create it
|
268
273
|
if ! File.exists?(classname_to_dir(resource.class.to_s))
|
@@ -292,13 +297,14 @@ module DataMapper::Adapters
|
|
292
297
|
end
|
293
298
|
end
|
294
299
|
end
|
295
|
-
xmlfile = File.new(class_name_to_file(resource.class.to_s, resource.key.first), "w")
|
296
300
|
|
301
|
+
xmlfile = File.new(class_name_to_file(resource.class.to_s, resource.key.first), "w")
|
297
302
|
tempfile = Tempfile.new("dm-xml-adapter", "/tmp")
|
298
303
|
tempfile.puts out_string
|
299
304
|
tempfile.close
|
300
305
|
FileUtils.mv(tempfile.path,xmlfile.path)
|
301
306
|
return xmlfile
|
307
|
+
|
302
308
|
end
|
303
309
|
|
304
310
|
def classname_to_dir(class_name)
|
data/spec/dm-xml-adapter_spec.rb
CHANGED
@@ -5,131 +5,150 @@ require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
|
|
5
5
|
|
6
6
|
describe DataMapper::Adapters::XmlAdapter do
|
7
7
|
before(:each) do
|
8
|
-
|
8
|
+
@adapter = DataMapper.setup(:default, {:adapter => 'xml', :directory => 'db'})
|
9
|
+
delete_all
|
10
|
+
end
|
11
|
+
|
12
|
+
def delete_all()
|
13
|
+
XMLTest::User.all.each {|u| u.destroy }
|
14
|
+
XMLTest::Post.all.each {|p| p.destroy }
|
15
|
+
AutoQuote.all.each {|aq| aq.destroy }
|
16
|
+
Food.all.each {|f| f.destroy }
|
17
|
+
Drink.all.each {|d| d.destroy }
|
9
18
|
end
|
10
19
|
|
11
20
|
after(:each) do
|
12
|
-
|
13
|
-
XMLTest::Post.all.each {|p| p.destroy }
|
14
|
-
AutoQuote.all.each {|aq| aq.destroy }
|
21
|
+
delete_all
|
15
22
|
end
|
16
23
|
|
17
24
|
|
18
25
|
describe "CRUD" do
|
19
26
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
describe "non-namedspaced class" do
|
28
|
+
it "should be able to persist" do
|
29
|
+
AutoQuote.create(:fullname => "Joshua Harding")
|
30
|
+
AutoQuote.all.size.should == 1
|
31
|
+
end
|
32
|
+
end
|
26
33
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
describe "content attribute" do
|
35
|
+
it "should be able to handle a content attribute" do
|
36
|
+
XMLTest::User.create(:name => "contentking", :content => "cool")
|
37
|
+
XMLTest::User.all.size.should == 1
|
38
|
+
end
|
39
|
+
end
|
33
40
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
describe "datetime" do
|
42
|
+
it "should do datetime" do
|
43
|
+
u1 = XMLTest::User.new
|
44
|
+
u1.name = "cool!"
|
45
|
+
u1.created = DateTime.now
|
46
|
+
u1.save
|
47
|
+
u2 = XMLTest::User.first(:name => "cool!")
|
48
|
+
u2.created.class.should == DateTime
|
49
|
+
end
|
50
|
+
end
|
44
51
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
52
|
+
describe "boolean" do
|
53
|
+
it "should be boolean" do
|
54
|
+
u1 = XMLTest::User.new
|
55
|
+
u1.name = "yes"
|
56
|
+
u1.cool = true
|
57
|
+
u1.save
|
58
|
+
u2 = XMLTest::User.first(:name => "yes")
|
59
|
+
u2.cool.class.should == TrueClass
|
60
|
+
end
|
61
|
+
it "falso thing" do
|
62
|
+
u1 = XMLTest::User.new
|
63
|
+
u1.name = "yes2"
|
64
|
+
u1.cool = false
|
65
|
+
u1.save
|
66
|
+
u2 = XMLTest::User.first(:name => "yes2")
|
67
|
+
u2.cool.class.should == FalseClass
|
68
|
+
end
|
69
|
+
end
|
63
70
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
71
|
+
describe "associations" do
|
72
|
+
it "should have associations" do
|
73
|
+
u1 = XMLTest::User.new
|
74
|
+
u1.name = "bob"
|
75
|
+
u1.save
|
76
|
+
p1 = XMLTest::Post.new
|
77
|
+
p1.title = "title o' post"
|
78
|
+
u1.posts << p1
|
79
|
+
p1.save
|
80
|
+
p2 = XMLTest::Post.new
|
81
|
+
p2.title = "title of the second post"
|
82
|
+
u1.posts << p2
|
83
|
+
p2.save
|
84
|
+
u1.posts.size.should == 2
|
85
|
+
u3 = XMLTest::User.first(:name => "bob")
|
86
|
+
u3.posts.size.should == 2
|
87
|
+
end
|
88
|
+
end
|
82
89
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
90
|
+
describe "get all" do
|
91
|
+
it "should get all" do
|
92
|
+
XMLTest::User.create(:name => :yeah)
|
93
|
+
XMLTest::User.all.should_not == nil
|
94
|
+
end
|
95
|
+
end
|
89
96
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
97
|
+
describe "update" do
|
98
|
+
it "should update" do
|
99
|
+
u1 = XMLTest::User.new
|
100
|
+
u1.name = "bob"
|
101
|
+
u1.save
|
102
|
+
u1.name = "steve"
|
103
|
+
u1.save
|
104
|
+
u1.name.should_not == "bob"
|
105
|
+
end
|
106
|
+
end
|
100
107
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
+
describe "create" do
|
109
|
+
it "should create" do
|
110
|
+
u = XMLTest::User.new
|
111
|
+
u.save
|
112
|
+
u.user_id.should_not == nil
|
113
|
+
end
|
114
|
+
end
|
108
115
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
116
|
+
describe "delete" do
|
117
|
+
it "should delete someone" do
|
118
|
+
user = XMLTest::User.create
|
119
|
+
user.destroy.should == true
|
120
|
+
end
|
121
|
+
end
|
115
122
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
+
describe "BigDecimal Support" do
|
124
|
+
it "should support bigdecimals" do
|
125
|
+
user1 = XMLTest::User.create(:name => "Richie Rich", :money => BigDecimal.new("100000000000000.51"))
|
126
|
+
user1.save
|
127
|
+
user1.reload
|
128
|
+
user1.money.class.should == BigDecimal
|
129
|
+
end
|
123
130
|
end
|
124
131
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
132
|
+
describe "find not" do
|
133
|
+
it "should find someone with a not clause" do
|
134
|
+
user1 = XMLTest::User.create(:name => "joe")
|
135
|
+
user2 = XMLTest::User.create(:name => "bob")
|
136
|
+
users = XMLTest::User.all(:user_id.not => user1.user_id)
|
137
|
+
users.first.user_id.should_not == user1.user_id
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe "more updating" do
|
142
|
+
it "should do a really quick update" do
|
143
|
+
100.times {|n| Drink.create(:name => "Drink#{n}") }
|
144
|
+
# pick a random one
|
145
|
+
ids = Drink.all.map {|d| d.drink_id }
|
146
|
+
drink = Drink.get(ids[rand(100).to_i])
|
147
|
+
drink.name = "Drinky drink!"
|
148
|
+
drink.save
|
149
|
+
Drink.first(:name => "Drinky drink!").should_not == nil
|
150
|
+
end
|
151
|
+
end
|
133
152
|
|
134
153
|
end
|
135
154
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-xml-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.53"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Harding
|
@@ -9,7 +9,7 @@ autorequire: dm-xml-adapter
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-21 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|