simple_record 1.1.43 → 1.1.44

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -204,25 +204,6 @@ module SimpleRecord
204
204
  return ret
205
205
  end
206
206
 
207
- # Since SimpleDB supports multiple attributes per value, the values are an array.
208
- # This method will return the value unwrapped if it's the only, otherwise it will return the array.
209
- def get_attribute(arg)
210
- # Check if this arg is already converted
211
- arg_s = arg.to_s
212
- instance_var = ("@" + arg_s)
213
- # puts "defined?(#{instance_var.to_sym}) " + (defined?(instance_var.to_sym)).inspect
214
- # if defined?(instance_var.to_sym) # this returns "method" for some reason??
215
- # puts "attribute #{instance_var} is defined"
216
- ret = instance_variable_get(instance_var)
217
- # puts 'ret=' + ret.to_s
218
- return ret if !ret.nil?
219
- # end
220
- ret = get_attribute_sdb(arg)
221
- ret = sdb_to_ruby(arg, ret)
222
- # puts "Setting instance var #{instance_var}"
223
- instance_variable_set(instance_var, ret)
224
- return ret
225
- end
226
207
 
227
208
  def make_dirty(arg, value)
228
209
  arg = arg.to_s
@@ -513,40 +494,53 @@ module SimpleRecord
513
494
  return run_before_destroy && delete && run_after_destroy
514
495
  end
515
496
 
516
- def delete_niled(to_delete)
517
- # puts 'to_delete=' + to_delete.inspect
518
- if to_delete.size > 0
519
- # puts 'Deleting attributes=' + to_delete.inspect
520
- delete_attributes to_delete
521
- end
522
- end
523
497
 
498
+ # Since SimpleDB supports multiple attributes per value, the values are an array.
499
+ # This method will return the value unwrapped if it's the only, otherwise it will return the array.
500
+ def get_attribute(arg)
501
+ # Check if this arg is already converted
502
+ arg_s = arg.to_s
503
+ instance_var = ("@" + arg_s)
504
+ # puts "defined?(#{instance_var.to_sym}) " + (defined?(instance_var.to_sym)).inspect
505
+ # if defined?(instance_var.to_sym) # this returns "method" for some reason??
506
+ # puts "attribute #{instance_var} is defined"
507
+ ret = instance_variable_get(instance_var)
508
+ # puts 'ret=' + ret.to_s
509
+ return ret if !ret.nil?
510
+ # end
511
+ ret = get_attribute_sdb(arg)
512
+ ret = sdb_to_ruby(arg, ret)
513
+ # puts "Setting instance var #{instance_var}"
514
+ instance_variable_set(instance_var, ret)
515
+ return ret
516
+ end
517
+
524
518
  def set(name, value)
525
519
 
526
520
  att_meta = defined_attributes_local[name.to_sym]
527
521
  return if att_meta.nil?
528
522
  if att_meta.type == :belongs_to
529
- set_belongs_to(name, value)
530
- return
523
+ attname = name.to_s + '_id'
524
+ attvalue = value.nil? ? nil : value.id
525
+ else
526
+ attname = name
527
+ attvalue = value
531
528
  end
532
- value = strip_array(value)
533
- make_dirty(name, value)
534
- instance_var = "@" + name.to_s
529
+ value = strip_array(attvalue)
530
+ make_dirty(attname, attvalue)
531
+ instance_var = "@" + attname.to_s
535
532
  # puts 'ARG=' + arg.to_s
536
- sdb_val = ruby_to_sdb(name, value)
537
- @attributes[name.to_s] = sdb_val
533
+ sdb_val = ruby_to_sdb(name, attvalue)
534
+ @attributes[attname.to_s] = sdb_val
538
535
  value = wrap_if_required(name, value, sdb_val)
539
- instance_variable_set(instance_var, value)
536
+ instance_variable_set(instance_var, attvalue)
540
537
  end
541
538
 
542
- def set_belongs_to(name, value)
543
- arg_id = name.to_s + '_id'
544
- if value.nil?
545
- make_dirty(arg_id, nil)
546
- self[arg_id]=nil unless self[arg_id].nil? # todo: can we remove unless check since dirty should take care of things?
547
- else
548
- make_dirty(arg_id, value.id)
549
- self[arg_id]=value.id
539
+ def delete_niled(to_delete)
540
+ # puts 'to_delete=' + to_delete.inspect
541
+ if to_delete.size > 0
542
+ # puts 'Deleting attributes=' + to_delete.inspect
543
+ delete_attributes to_delete
550
544
  end
551
545
  end
552
546
 
@@ -119,11 +119,11 @@ module SimpleRecord
119
119
  # This method will also create an {association)_id method that will return the ID of the foreign object
120
120
  # without actually materializing it.
121
121
  def belongs_to(association_id, options = {})
122
- attribute = Attribute.new(:belongs_to, options)
123
- defined_attributes[association_id] = attribute
124
122
  arg = association_id
125
123
  arg_s = arg.to_s
126
124
  arg_id = arg.to_s + '_id'
125
+ attribute = Attribute.new(:belongs_to, options)
126
+ defined_attributes[arg] = attribute
127
127
 
128
128
  # todo: should also handle foreign_key http://74.125.95.132/search?q=cache:KqLkxuXiBBQJ:wiki.rubyonrails.org/rails/show/belongs_to+rails+belongs_to&hl=en&ct=clnk&cd=1&gl=us
129
129
  # puts "arg_id=#{arg}_id"
@@ -132,6 +132,7 @@ module SimpleRecord
132
132
 
133
133
  # Define reader method
134
134
  send(:define_method, arg) do
135
+ puts 'GETTING ' + arg.to_s
135
136
  attribute = defined_attributes_local[arg]
136
137
  options2 = attribute.options # @@belongs_to_map[arg]
137
138
  class_name = options2[:class_name] || arg.to_s[0...1].capitalize + arg.to_s[1...arg.to_s.length]
@@ -173,7 +174,7 @@ module SimpleRecord
173
174
 
174
175
  # Define writer method
175
176
  send(:define_method, arg.to_s + "=") do |value|
176
- set_belongs_to(arg, value)
177
+ set(arg, value)
177
178
  end
178
179
 
179
180
 
@@ -222,6 +223,8 @@ module SimpleRecord
222
223
 
223
224
  end
224
225
 
226
+
227
+
225
228
  def self.handle_virtuals(attrs)
226
229
  @@virtuals.each do |virtual|
227
230
  #we first copy the information for the virtual to an instance variable of the same name
@@ -13,7 +13,7 @@ module SimpleRecord
13
13
  name = name.to_s
14
14
 
15
15
  # puts "Converting #{name} to sdb value=#{value}"
16
- # puts "atts_local=" + defined_attributes_local.inspect
16
+ puts "atts_local=" + defined_attributes_local.inspect
17
17
 
18
18
  att_meta = defined_attributes_local[name.to_sym]
19
19
 
@@ -114,10 +114,14 @@ class TestSimpleRecord < Test::Unit::TestCase
114
114
  child.my_model = mm
115
115
  child.save
116
116
 
117
+ puts "child=" + child.inspect
117
118
  sleep 1
118
119
 
119
120
  child = MyChildModel.find(child.id)
120
- puts child.my_model_id
121
+ puts "child find=" + child.inspect
122
+ puts "child.my_model_id = " + child.my_model_id.to_s
123
+ assert !child.my_model_id.nil?
124
+ assert !child.my_model.nil?
121
125
  assert child.my_model_id == mm.id
122
126
  end
123
127
 
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.43
4
+ version: 1.1.44
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder