simple_record 1.1.64 → 1.1.65

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,23 +46,27 @@ module SimpleRecord
46
46
  set(arg, value)
47
47
  end
48
48
 
49
- # Now for dirty methods: http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html
50
- # define changed? method
51
- send(:define_method, arg_s + "_changed?") do
52
- @dirty.has_key?(sdb_att_name(arg_s))
53
- end
49
+ define_dirty_methods(arg_s)
50
+ end
51
+ end
54
52
 
55
- # define change method
56
- send(:define_method, arg_s + "_change") do
57
- old_val = @dirty[sdb_att_name(arg_s)]
58
- [old_val, get_attribute(arg_s)]
59
- end
53
+ def define_dirty_methods(arg_s)
54
+ # Now for dirty methods: http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html
55
+ # define changed? method
56
+ send(:define_method, arg_s + "_changed?") do
57
+ @dirty.has_key?(sdb_att_name(arg_s))
58
+ end
60
59
 
61
- # define was method
62
- send(:define_method, arg_s + "_was") do
63
- old_val = @dirty[sdb_att_name(arg_s)]
64
- old_val
65
- end
60
+ # define change method
61
+ send(:define_method, arg_s + "_change") do
62
+ old_val = @dirty[sdb_att_name(arg_s)]
63
+ [old_val, get_attribute(arg_s)]
64
+ end
65
+
66
+ # define was method
67
+ send(:define_method, arg_s + "_was") do
68
+ old_val = @dirty[sdb_att_name(arg_s)]
69
+ old_val
66
70
  end
67
71
  end
68
72
 
@@ -132,7 +136,7 @@ module SimpleRecord
132
136
 
133
137
  # Define reader method
134
138
  send(:define_method, arg) do
135
- return get_attribute(arg)
139
+ return get_attribute(arg)
136
140
  end
137
141
 
138
142
 
@@ -144,27 +148,18 @@ module SimpleRecord
144
148
 
145
149
  # Define ID reader method for reading the associated objects id without getting the entire object
146
150
  send(:define_method, arg_id) do
147
- val1 = @attributes[arg_id]
148
- if val1
149
- if val1.is_a?(Array)
150
- if val1.size > 0 && val1[0].present?
151
- return val1[0]
152
- end
153
-
154
- else
155
- return val1
156
- end
157
- end
158
- return nil
151
+ get_attribute_sdb(arg_s)
159
152
  end
160
153
 
161
154
  # Define writer method for setting the _id directly without the associated object
162
155
  send(:define_method, arg_id + "=") do |value|
163
- if value.nil?
164
- self[arg_id] = nil unless self[arg_id].nil? # if it went from something to nil, then we have to remember and remove attribute on save
165
- else
166
- self[arg_id] = value
167
- end
156
+ # rb_att_name = arg_s # n2 = name.to_s[0, name.length-3]
157
+ set(arg_id, value)
158
+ # if value.nil?
159
+ # self[arg_id] = nil unless self[arg_id].nil? # if it went from something to nil, then we have to remember and remove attribute on save
160
+ # else
161
+ # self[arg_id] = value
162
+ # end
168
163
  end
169
164
 
170
165
  send(:define_method, "create_"+arg.to_s) do |*params|
@@ -173,6 +168,8 @@ module SimpleRecord
173
168
  arg_id = arg.to_s + '_id'
174
169
  self[arg_id]=newsubrecord.id
175
170
  end
171
+
172
+ define_dirty_methods(arg_s)
176
173
  end
177
174
 
178
175
  def has_many(*args)
@@ -44,7 +44,7 @@ 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
 
data/lib/simple_record.rb CHANGED
@@ -218,14 +218,14 @@ module SimpleRecord
218
218
  sdb_att_name = sdb_att_name(arg)
219
219
  arg = arg.to_s
220
220
 
221
- puts "Marking #{arg} dirty with #{value}"
221
+ # puts "Marking #{arg} dirty with #{value}"
222
222
  if @dirty.include?(sdb_att_name)
223
223
  old = @dirty[sdb_att_name]
224
224
  # puts "Was already dirty #{old}"
225
225
  @dirty.delete(sdb_att_name) if value == old
226
226
  else
227
227
  old = get_attribute_sdb(arg)
228
- puts "dirtifying old=#{old} to new=#{value}"
228
+ # puts "dirtifying old=#{old} to new=#{value}"
229
229
  @dirty[sdb_att_name] = old if value != old
230
230
  end
231
231
  end
@@ -324,7 +324,7 @@ module SimpleRecord
324
324
  clear_errors
325
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
326
326
  if options[:dirty]
327
- puts '@dirtyA=' + @dirty.inspect
327
+ # puts '@dirtyA=' + @dirty.inspect
328
328
  return true if @dirty.size == 0 # Nothing to save so skip it
329
329
  end
330
330
  is_create = self[:id].nil?
@@ -332,7 +332,7 @@ module SimpleRecord
332
332
  if ok
333
333
  begin
334
334
  if options[:dirty]
335
- puts '@dirty=' + @dirty.inspect
335
+ # puts '@dirty=' + @dirty.inspect
336
336
  return true if @dirty.size == 0 # This should probably never happen because after pre_save, created/updated dates are changed
337
337
  options[:dirty_atts] = @dirty
338
338
  end
@@ -522,7 +522,7 @@ module SimpleRecord
522
522
  # Since SimpleDB supports multiple attributes per value, the values are an array.
523
523
  # This method will return the value unwrapped if it's the only, otherwise it will return the array.
524
524
  def get_attribute(arg)
525
- puts "GET #{arg}"
525
+ # puts "GET #{arg}"
526
526
  # Check if this arg is already converted
527
527
  arg_s = arg.to_s
528
528
  # instance_var = ("@" + arg_s)
@@ -531,13 +531,13 @@ module SimpleRecord
531
531
  # puts "attribute #{instance_var} is defined"
532
532
  @attributes_rb = {} unless @attributes_rb # was getting errors after upgrade.
533
533
  ret = @attributes_rb[arg_s] # instance_variable_get(instance_var)
534
- puts 'ret from rb=' + ret.inspect
534
+ # puts 'ret from rb=' + ret.inspect
535
535
  return ret if !ret.nil?
536
536
  # end
537
537
  ret = get_attribute_sdb(arg)
538
- puts 'ret from atts=' + ret.inspect
538
+ # puts 'ret from atts=' + ret.inspect
539
539
  ret = sdb_to_ruby(arg, ret)
540
- puts 'ret from atts to rb=' + ret.inspect
540
+ # puts 'ret from atts to rb=' + ret.inspect
541
541
  # puts "Setting instance var #{arg_s} to #{ret}"
542
542
  # instance_variable_set(instance_var, ret)
543
543
  @attributes_rb[arg_s] = ret
@@ -545,8 +545,8 @@ module SimpleRecord
545
545
  end
546
546
 
547
547
  def set(name, value, dirtify=true)
548
- puts "SET #{name}=#{value.inspect}"
549
- puts "self=" + self.inspect
548
+ # puts "SET #{name}=#{value.inspect}"
549
+ # puts "self=" + self.inspect
550
550
  att_meta = defined_attributes_local[name.to_sym]
551
551
  if att_meta.nil?
552
552
  # check if it ends with id and see if att_meta is there
@@ -573,7 +573,7 @@ module SimpleRecord
573
573
  end
574
574
  attvalue = strip_array(attvalue)
575
575
  make_dirty(name, attvalue) if dirtify
576
- puts "ARG=#{attname.to_s} setting to #{attvalue}"
576
+ # puts "ARG=#{attname.to_s} setting to #{attvalue}"
577
577
  sdb_val = ruby_to_sdb(name, attvalue)
578
578
  # puts "sdb_val=" + sdb_val.to_s
579
579
  @attributes[attname] = sdb_val
@@ -187,6 +187,7 @@ class TestSimpleRecord < TestBase
187
187
  assert mmc2.my_model == nil, "my_model not nil? #{mmc2.my_model.inspect}"
188
188
 
189
189
  mmc3 = MyChildModel.find(mmc.id)
190
+ puts "mmc3 1 =" + mmc3.inspect
190
191
  assert mmc3.my_model_id == nil, "my_model_id not nil? #{mmc3.my_model_id.inspect}"
191
192
  assert mmc3.my_model == nil
192
193
 
@@ -194,6 +195,7 @@ class TestSimpleRecord < TestBase
194
195
  assert mm3.save
195
196
 
196
197
  mmc3.my_model = mm3
198
+ assert mmc3.my_model_changed?
197
199
  assert mmc3.save(:dirty=>true)
198
200
 
199
201
  assert mmc3.my_model_id == mm3.id
@@ -204,6 +206,12 @@ class TestSimpleRecord < TestBase
204
206
  assert mmc3.my_model_id == mm3.id, "my_model_id=#{mmc3.my_model_id.inspect} mm3.id=#{mm3.id.inspect}"
205
207
  assert mmc3.my_model.id == mm3.id
206
208
 
209
+ mmc3 = MyChildModel.find(mmc3.id)
210
+ mmc3.my_model_id = mm2.id
211
+ assert mmc3.my_model_id == mm2.id
212
+ assert mmc3.changed?
213
+ assert mmc3.my_model_changed?
214
+ assert mmc3.my_model.id = mm2.id
207
215
 
208
216
  end
209
217
 
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.64
4
+ version: 1.1.65
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder