dm-xml-adapter 0.52 → 0.53

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,19 +15,19 @@ module DataMapper::Adapters
15
15
  class XmlAdapterCache
16
16
 
17
17
  def initialize
18
- # @@logger = Log4r::Logger.new 'cache'
19
- # @@logger.outputters = Log4r::Outputter.stdout
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
- # @@logger.info("Saved mtime, #{file} -> #{mtime}")
25
+ #@@logger.info("Saved mtime, #{file} -> #{mtime}")
26
26
  @mtimes[file] = mtime
27
27
  end
28
28
 
29
29
  def delete_mtime(file)
30
- # @@logger.info("deleting the following mtime: #{file}")
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
- # @@logger.info("first one: #{@classes.inspect}")
40
- # @@logger.info("CACHE INSERT: #{classname} #{id} #{object.inspect}")
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
- # @@logger.info("the hash is: #{hash.inspect}")
55
+ #@@logger.info("the hash is: #{hash.inspect}")
56
56
  unless hash == nil
57
57
  hash.delete(id.to_s)
58
58
  end
59
- # @@logger.info("the hash is: #{hash.inspect}")
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
- # @@logger.info("Master classes: #{@classes.inspect}")
64
+ #@@logger.info("Master classes: #{@classes.inspect}")
65
65
  hash = @classes[classname]
66
- # @@logger.info("the hash is: #{hash.inspect}")
66
+ #@@logger.info("the hash is: #{hash.inspect}")
67
67
  object = hash[id]
68
- # @@logger.info("The object is: #{object}")
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
- # @@logger.outputters = Log4r::Outputter.stdout
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
- #@@logger.info("CACHE MISS")
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
- # @@logger.info("YES !!!")
183
- #@@logger.info("CACHE HIT")
184
- #@@logger.info("the file is: #{file}")
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)
@@ -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
- @adapter = DataMapper.setup(:default, {:adapter => 'xml', :directory => 'db'})
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
- XMLTest::User.all.each {|u| u.destroy }
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
- describe "non-namedspaced class" do
21
- it "should be able to persist" do
22
- AutoQuote.create(:fullname => "Joshua Harding")
23
- AutoQuote.all.size.should == 1
24
- end
25
- end
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
- describe "content attribute" do
28
- it "should be able to handle a content attribute" do
29
- XMLTest::User.create(:name => "contentking", :content => "cool")
30
- XMLTest::User.all.size.should == 1
31
- end
32
- end
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
- describe "datetime" do
35
- it "should do datetime" do
36
- u1 = XMLTest::User.new
37
- u1.name = "cool!"
38
- u1.created = DateTime.now
39
- u1.save
40
- u2 = XMLTest::User.first(:name => "cool!")
41
- u2.created.class.should == DateTime
42
- end
43
- end
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
- describe "boolean" do
46
- it "should be boolean" do
47
- u1 = XMLTest::User.new
48
- u1.name = "yes"
49
- u1.cool = true
50
- u1.save
51
- u2 = XMLTest::User.first(:name => "yes")
52
- u2.cool.class.should == TrueClass
53
- end
54
- it "falso thing" do
55
- u1 = XMLTest::User.new
56
- u1.name = "yes2"
57
- u1.cool = false
58
- u1.save
59
- u2 = XMLTest::User.first(:name => "yes2")
60
- u2.cool.class.should == FalseClass
61
- end
62
- end
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
- describe "associations" do
65
- it "should have associations" do
66
- u1 = XMLTest::User.new
67
- u1.name = "bob"
68
- u1.save
69
- p1 = XMLTest::Post.new
70
- p1.title = "title o' post"
71
- u1.posts << p1
72
- p1.save
73
- p2 = XMLTest::Post.new
74
- p2.title = "title of the second post"
75
- u1.posts << p2
76
- p2.save
77
- u1.posts.size.should == 2
78
- u3 = XMLTest::User.first(:name => "bob")
79
- u3.posts.size.should == 2
80
- end
81
- end
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
- describe "get all" do
84
- it "should get all" do
85
- XMLTest::User.create(:name => :yeah)
86
- XMLTest::User.all.should_not == nil
87
- end
88
- end
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
- describe "update" do
91
- it "should update" do
92
- u1 = XMLTest::User.new
93
- u1.name = "bob"
94
- u1.save
95
- u1.name = "steve"
96
- u1.save
97
- u1.name.should_not == "bob"
98
- end
99
- end
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
- describe "create" do
102
- it "should create" do
103
- u = XMLTest::User.new
104
- u.save
105
- u.user_id.should_not == nil
106
- end
107
- end
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
- describe "delete" do
110
- it "should delete someone" do
111
- user = XMLTest::User.create
112
- user.destroy.should == true
113
- end
114
- end
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
- describe "BigDecimal Support" do
117
- it "should support bigdecimals" do
118
- user1 = XMLTest::User.create(:name => "Richie Rich", :money => BigDecimal.new("100000000000000.51"))
119
- user1.save
120
- user1.reload
121
- user1.money.class.should == BigDecimal
122
- end
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
- describe "find not" do
126
- it "should find someone with a not clause" do
127
- user1 = XMLTest::User.create(:name => "joe")
128
- user2 = XMLTest::User.create(:name => "bob")
129
- users = XMLTest::User.all(:user_id.not => user1.user_id)
130
- users.first.user_id.should_not == user1.user_id
131
- end
132
- end
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.52"
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-14 00:00:00 -05:00
12
+ date: 2009-12-21 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency