simple_record 1.1.63 → 1.1.64

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -184,14 +184,22 @@ module SimpleRecord
184
184
  domain_name_for_class
185
185
  end
186
186
 
187
- def get_attribute_sdb(arg)
187
+ def get_attribute_sdb(name)
188
188
  # arg = arg.to_s
189
189
  # puts "get_attribute_sdb(#{arg}) - #{arg.class.name}"
190
190
  # puts 'self[]=' + self.inspect
191
- ret = strip_array(self[arg])
191
+ ret = strip_array(@attributes[sdb_att_name(name)])
192
192
  return ret
193
193
  end
194
194
 
195
+ def sdb_att_name(name)
196
+ att_meta = defined_attributes_local[name.to_sym]
197
+ if att_meta.type == :belongs_to
198
+ return "#{name}_id"
199
+ end
200
+ name.to_s
201
+ end
202
+
195
203
  def strip_array(arg)
196
204
  if arg.class==Array
197
205
  if arg.length==1
@@ -207,16 +215,18 @@ module SimpleRecord
207
215
 
208
216
 
209
217
  def make_dirty(arg, value)
218
+ sdb_att_name = sdb_att_name(arg)
210
219
  arg = arg.to_s
211
- # puts "Marking #{arg} dirty with #{value}"
212
- if @dirty.include?(arg)
213
- old = @dirty[arg]
220
+
221
+ puts "Marking #{arg} dirty with #{value}"
222
+ if @dirty.include?(sdb_att_name)
223
+ old = @dirty[sdb_att_name]
214
224
  # puts "Was already dirty #{old}"
215
- @dirty.delete(arg) if value == old
225
+ @dirty.delete(sdb_att_name) if value == old
216
226
  else
217
- old = get_attribute(arg)
218
- # puts "dirtifying #{old} to #{value}"
219
- @dirty[arg] = old if value != old
227
+ old = get_attribute_sdb(arg)
228
+ puts "dirtifying old=#{old} to new=#{value}"
229
+ @dirty[sdb_att_name] = old if value != old
220
230
  end
221
231
  end
222
232
 
@@ -314,7 +324,7 @@ module SimpleRecord
314
324
  clear_errors
315
325
  # todo: decide whether this should go before pre_save or after pre_save? pre_save dirties "updated" and perhaps other items due to callbacks
316
326
  if options[:dirty]
317
- # puts '@dirty=' + @dirty.inspect
327
+ puts '@dirtyA=' + @dirty.inspect
318
328
  return true if @dirty.size == 0 # Nothing to save so skip it
319
329
  end
320
330
  is_create = self[:id].nil?
@@ -322,7 +332,7 @@ module SimpleRecord
322
332
  if ok
323
333
  begin
324
334
  if options[:dirty]
325
- # puts '@dirty=' + @dirty.inspect
335
+ puts '@dirty=' + @dirty.inspect
326
336
  return true if @dirty.size == 0 # This should probably never happen because after pre_save, created/updated dates are changed
327
337
  options[:dirty_atts] = @dirty
328
338
  end
@@ -512,6 +522,7 @@ module SimpleRecord
512
522
  # Since SimpleDB supports multiple attributes per value, the values are an array.
513
523
  # This method will return the value unwrapped if it's the only, otherwise it will return the array.
514
524
  def get_attribute(arg)
525
+ puts "GET #{arg}"
515
526
  # Check if this arg is already converted
516
527
  arg_s = arg.to_s
517
528
  # instance_var = ("@" + arg_s)
@@ -520,11 +531,13 @@ module SimpleRecord
520
531
  # puts "attribute #{instance_var} is defined"
521
532
  @attributes_rb = {} unless @attributes_rb # was getting errors after upgrade.
522
533
  ret = @attributes_rb[arg_s] # instance_variable_get(instance_var)
523
- # puts 'ret=' + ret.to_s
534
+ puts 'ret from rb=' + ret.inspect
524
535
  return ret if !ret.nil?
525
536
  # end
526
537
  ret = get_attribute_sdb(arg)
538
+ puts 'ret from atts=' + ret.inspect
527
539
  ret = sdb_to_ruby(arg, ret)
540
+ puts 'ret from atts to rb=' + ret.inspect
528
541
  # puts "Setting instance var #{arg_s} to #{ret}"
529
542
  # instance_variable_set(instance_var, ret)
530
543
  @attributes_rb[arg_s] = ret
@@ -532,7 +545,8 @@ module SimpleRecord
532
545
  end
533
546
 
534
547
  def set(name, value, dirtify=true)
535
-
548
+ puts "SET #{name}=#{value.inspect}"
549
+ puts "self=" + self.inspect
536
550
  att_meta = defined_attributes_local[name.to_sym]
537
551
  if att_meta.nil?
538
552
  # check if it ends with id and see if att_meta is there
@@ -559,13 +573,14 @@ module SimpleRecord
559
573
  end
560
574
  attvalue = strip_array(attvalue)
561
575
  make_dirty(name, attvalue) if dirtify
562
- # puts "ARG=#{attname.to_s} setting to #{attvalue}"
576
+ puts "ARG=#{attname.to_s} setting to #{attvalue}"
563
577
  sdb_val = ruby_to_sdb(name, attvalue)
564
578
  # puts "sdb_val=" + sdb_val.to_s
565
579
  @attributes[attname] = sdb_val
566
- attvalue = wrap_if_required(name, attvalue, sdb_val)
580
+ # attvalue = wrap_if_required(name, attvalue, sdb_val)
567
581
  # puts 'attvalue2=' + attvalue.to_s
568
- @attributes_rb[attname] = attvalue
582
+ @attributes_rb.delete(name.to_s) # todo: we should set the value here so it doesn't reget anything
583
+
569
584
 
570
585
  # instance_var = "@" + attname.to_s
571
586
  # instance_variable_set(instance_var, attvalue)
@@ -575,6 +590,7 @@ module SimpleRecord
575
590
  # puts 'to_delete=' + to_delete.inspect
576
591
  if to_delete.size > 0
577
592
  # puts 'Deleting attributes=' + to_delete.inspect
593
+ SimpleRecord.stats.deletes += 1
578
594
  delete_attributes to_delete
579
595
  end
580
596
  end
@@ -49,18 +49,18 @@ module SimpleRecord
49
49
  # Now for dirty methods: http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html
50
50
  # define changed? method
51
51
  send(:define_method, arg_s + "_changed?") do
52
- @dirty.has_key?(arg_s)
52
+ @dirty.has_key?(sdb_att_name(arg_s))
53
53
  end
54
54
 
55
55
  # define change method
56
56
  send(:define_method, arg_s + "_change") do
57
- old_val = @dirty[arg_s]
57
+ old_val = @dirty[sdb_att_name(arg_s)]
58
58
  [old_val, get_attribute(arg_s)]
59
59
  end
60
60
 
61
61
  # define was method
62
62
  send(:define_method, arg_s + "_was") do
63
- old_val = @dirty[arg_s]
63
+ old_val = @dirty[sdb_att_name(arg_s)]
64
64
  old_val
65
65
  end
66
66
  end
@@ -132,44 +132,7 @@ module SimpleRecord
132
132
 
133
133
  # Define reader method
134
134
  send(:define_method, arg) do
135
- # puts 'GETTING ' + arg.to_s
136
- attribute = defined_attributes_local[arg]
137
- options2 = attribute.options # @@belongs_to_map[arg]
138
- class_name = options2[:class_name] || arg.to_s[0...1].capitalize + arg.to_s[1...arg.to_s.length]
139
-
140
- # Camelize classnames with underscores (ie my_model.rb --> MyModel)
141
- class_name = class_name.camelize
142
-
143
- # puts "attr=" + @attributes[arg_id].inspect
144
- # puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil?
145
- ret = nil
146
- arg_id = arg.to_s + '_id'
147
- arg_id_val = send("#{arg_id}")
148
- if arg_id_val
149
- if !cache_store.nil?
150
- # arg_id_val = @attributes[arg_id][0]
151
- cache_key = self.class.cache_key(class_name, arg_id_val)
152
- # puts 'cache_key=' + cache_key
153
- ret = cache_store.read(cache_key)
154
- # puts 'belongs_to incache=' + ret.inspect
155
- end
156
- if ret.nil?
157
- to_eval = "#{class_name}.find('#{arg_id_val}')"
158
- # puts 'to eval=' + to_eval
159
- begin
160
- ret = eval(to_eval) # (defined? #{arg}_id)
161
- rescue Aws::ActiveSdb::ActiveSdbError
162
- if $!.message.include? "Couldn't find"
163
- ret = nil
164
- else
165
- raise $!
166
- end
167
- end
168
-
169
- end
170
- end
171
- # puts 'ret=' + ret.inspect
172
- return ret
135
+ return get_attribute(arg)
173
136
  end
174
137
 
175
138
 
@@ -1,15 +1,17 @@
1
1
  module SimpleRecord
2
2
  class Stats
3
- attr_accessor :selects, :puts
3
+ attr_accessor :selects, :puts, :deletes
4
4
 
5
5
  def initialize
6
6
  @selects = 0
7
7
  @puts = 0
8
+ @deletes = 0
8
9
  end
9
10
 
10
11
  def clear
11
12
  self.selects = 0
12
13
  self.puts = 0
14
+ self.deletes = 0
13
15
  end
14
16
  end
15
17
  end
@@ -44,11 +44,11 @@ module SimpleRecord
44
44
 
45
45
  # Convert value from SimpleDB String version to real ruby value.
46
46
  def sdb_to_ruby(name, value)
47
- # puts 'sdb_to_ruby arg=' + name.inspect + ' - ' + name.class.name + ' - value=' + value.to_s
47
+ puts 'sdb_to_ruby arg=' + name.inspect + ' - ' + name.class.name + ' - value=' + value.to_s
48
48
  return nil if value.nil?
49
49
  att_meta = defined_attributes_local[name.to_sym]
50
50
 
51
- if att_meta.options
51
+ if att_meta.options
52
52
  if att_meta.options[:encrypted]
53
53
  value = Translations.decrypt(value, att_meta.options[:encrypted])
54
54
  end
@@ -57,7 +57,41 @@ module SimpleRecord
57
57
  end
58
58
  end
59
59
 
60
- if att_meta.type == :int
60
+
61
+ if att_meta.type == :belongs_to
62
+ class_name = att_meta.options[:class_name] || name.to_s[0...1].capitalize + name.to_s[1...name.to_s.length]
63
+ # Camelize classnames with underscores (ie my_model.rb --> MyModel)
64
+ class_name = class_name.camelize
65
+ # puts "attr=" + @attributes[arg_id].inspect
66
+ # puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil?
67
+ ret = nil
68
+ arg_id = name.to_s + '_id'
69
+ arg_id_val = send("#{arg_id}")
70
+ if arg_id_val
71
+ if !cache_store.nil?
72
+ # arg_id_val = @attributes[arg_id][0]
73
+ cache_key = self.class.cache_key(class_name, arg_id_val)
74
+ # puts 'cache_key=' + cache_key
75
+ ret = cache_store.read(cache_key)
76
+ # puts 'belongs_to incache=' + ret.inspect
77
+ end
78
+ if ret.nil?
79
+ to_eval = "#{class_name}.find('#{arg_id_val}')"
80
+ # puts 'to eval=' + to_eval
81
+ begin
82
+ ret = eval(to_eval) # (defined? #{arg}_id)
83
+ rescue Aws::ActiveSdb::ActiveSdbError
84
+ if $!.message.include? "Couldn't find"
85
+ ret = nil
86
+ else
87
+ raise $!
88
+ end
89
+ end
90
+
91
+ end
92
+ end
93
+ value = ret
94
+ elsif att_meta.type == :int
61
95
  value = Translations.un_offset_int(value)
62
96
  elsif att_meta.type == :date
63
97
  value = to_date(value)
@@ -148,7 +148,7 @@ class TestSimpleRecord < TestBase
148
148
  # Get the object back
149
149
  mm2 = MyModel.find(id)
150
150
  puts 'mm2=' + mm2.inspect
151
- puts 'got=' + mm2.name + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
151
+ puts 'got=' + mm2.name.to_s + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
152
152
  assert mm2.id == mm.id
153
153
  assert mm2.age == mm.age
154
154
  assert mm2.cool == mm.cool
@@ -171,12 +171,39 @@ class TestSimpleRecord < TestBase
171
171
  mmc.x = mm
172
172
  mmc.save
173
173
 
174
- mmc.my_model = nil
175
- mmc.x = nil
174
+ mmc2 = MyChildModel.find(mmc.id)
175
+ assert mmc2.my_model_id == mmc.my_model_id, "mm2.my_model_id=#{mmc2.my_model_id} mmc.my_model_id=#{mmc.my_model_id}"
176
+ puts 'setting my_model to nil'
177
+ mmc2.my_model = nil
178
+ mmc2.x = nil
179
+ puts 'saving my_model to nil'
180
+ SimpleRecord.stats.clear
181
+ assert mmc2.save(:dirty=>true)
182
+ puts SimpleRecord.stats.puts.to_s
183
+ assert SimpleRecord.stats.puts == 1 # 1 put only for updated, should have a count of attributes saved in stats
184
+ assert SimpleRecord.stats.deletes == 1
185
+ assert mmc2.id == mmc.id
186
+ assert mmc2.my_model_id == nil
187
+ assert mmc2.my_model == nil, "my_model not nil? #{mmc2.my_model.inspect}"
188
+
189
+ mmc3 = MyChildModel.find(mmc.id)
190
+ assert mmc3.my_model_id == nil, "my_model_id not nil? #{mmc3.my_model_id.inspect}"
191
+ assert mmc3.my_model == nil
192
+
193
+ mm3 = MyModel.new(:name=>"test")
194
+ assert mm3.save
195
+
196
+ mmc3.my_model = mm3
197
+ assert mmc3.save(:dirty=>true)
198
+
199
+ assert mmc3.my_model_id == mm3.id
200
+ assert mmc3.my_model.id == mm3.id
201
+
202
+ mmc3 = MyChildModel.find(mmc3.id)
203
+ puts "mmc3=" + mmc3.inspect
204
+ assert mmc3.my_model_id == mm3.id, "my_model_id=#{mmc3.my_model_id.inspect} mm3.id=#{mm3.id.inspect}"
205
+ assert mmc3.my_model.id == mm3.id
176
206
 
177
- mmc = MyChildModel.find(mmc.id)
178
- mmc.my_model = nil
179
- mmc.x = nil
180
207
 
181
208
  end
182
209
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.63
4
+ version: 1.1.64
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder