simple_record 1.1.46 → 1.1.47

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -100,7 +100,8 @@ module SimpleRecord
100
100
  @errors=SimpleRecord_errors.new
101
101
  @dirty = {}
102
102
 
103
- @attributes = {}
103
+ @attributes = {} # sdb values
104
+ @attributes_rb = {} # ruby values
104
105
  @new_record = true
105
106
 
106
107
  end
@@ -414,8 +415,18 @@ module SimpleRecord
414
415
  ok
415
416
  end
416
417
 
417
- def save_attributes(*params)
418
- ret = super(*params)
418
+ def save_attributes(atts)
419
+ # puts 'atts=' + atts.inspect
420
+ ret = super(atts)
421
+ # puts '@atts=' + @attributes.inspect
422
+ atts.each_pair do |k, v|
423
+ if k.is_a?(Symbol)
424
+ @attributes[k.to_s] = v
425
+ @attributes.delete(k)
426
+ end
427
+ end
428
+ # puts '@atts2=' + @attributes.inspect
429
+ @attributes_rb.clear # clear out the ruby versions so they can reload on next get.
419
430
  if ret
420
431
  self.class.cache_results(self)
421
432
  end
@@ -500,18 +511,19 @@ module SimpleRecord
500
511
  def get_attribute(arg)
501
512
  # Check if this arg is already converted
502
513
  arg_s = arg.to_s
503
- instance_var = ("@" + arg_s)
514
+ # instance_var = ("@" + arg_s)
504
515
  # puts "defined?(#{instance_var.to_sym}) " + (defined?(instance_var.to_sym)).inspect
505
516
  # if defined?(instance_var.to_sym) # this returns "method" for some reason??
506
517
  # puts "attribute #{instance_var} is defined"
507
- ret = instance_variable_get(instance_var)
518
+ ret = @attributes_rb[arg_s] # instance_variable_get(instance_var)
508
519
  # puts 'ret=' + ret.to_s
509
520
  return ret if !ret.nil?
510
521
  # end
511
522
  ret = get_attribute_sdb(arg)
512
523
  ret = sdb_to_ruby(arg, ret)
513
- # puts "Setting instance var #{instance_var}"
514
- instance_variable_set(instance_var, ret)
524
+ # puts "Setting instance var #{arg_s} to #{ret}"
525
+ # instance_variable_set(instance_var, ret)
526
+ @attributes_rb[arg_s] = ret
515
527
  return ret
516
528
  end
517
529
 
@@ -523,17 +535,19 @@ module SimpleRecord
523
535
  attname = name.to_s + '_id'
524
536
  attvalue = value.nil? ? nil : value.id
525
537
  else
526
- attname = name
538
+ attname = name.to_s
527
539
  attvalue = value
528
540
  end
529
- value = strip_array(attvalue)
541
+ attvalue = strip_array(attvalue)
530
542
  make_dirty(attname, attvalue)
531
- instance_var = "@" + attname.to_s
532
543
  # puts 'ARG=' + arg.to_s
533
544
  sdb_val = ruby_to_sdb(name, attvalue)
534
- @attributes[attname.to_s] = sdb_val
535
- value = wrap_if_required(name, value, sdb_val)
536
- instance_variable_set(instance_var, attvalue)
545
+ @attributes[attname] = sdb_val
546
+ attvalue = wrap_if_required(name, value, sdb_val)
547
+ @attributes_rb[attname] = attvalue
548
+
549
+ # instance_var = "@" + attname.to_s
550
+ # instance_variable_set(instance_var, attvalue)
537
551
  end
538
552
 
539
553
  def delete_niled(to_delete)
@@ -548,8 +562,8 @@ module SimpleRecord
548
562
  super()
549
563
  end
550
564
 
551
- def update_attributes(*params)
552
- return save_attributes(*params)
565
+ def update_attributes(atts)
566
+ return save_attributes(atts)
553
567
  end
554
568
 
555
569
  def self.quote_regexp(a, re)
@@ -696,44 +710,6 @@ module SimpleRecord
696
710
 
697
711
  end
698
712
 
699
- class SimpleRecord_errors
700
- def initialize(*params)
701
- super(*params)
702
- @errors=[]
703
- end
704
-
705
- def add_to_base(value)
706
- @errors+=[value]
707
- end
708
-
709
- def add(attribute, value)
710
- @errors+=["#{attribute.to_s} #{value}"]
711
- end
712
-
713
- def count
714
- return length
715
- end
716
-
717
- def length
718
- return @errors.length
719
- end
720
-
721
- def size
722
- return length
723
- end
724
-
725
- def full_messages
726
- return @errors
727
- end
728
-
729
- def clear
730
- @errors.clear
731
- end
732
-
733
- def empty?
734
- @errors.empty?
735
- end
736
- end
737
713
 
738
714
  class Activerecordtosdb_subrecord_array
739
715
  def initialize(subname, referencename, referencevalue)
@@ -12,6 +12,46 @@ module SimpleRecord
12
12
  @record = record
13
13
  end
14
14
  end
15
+
16
+
17
+ class SimpleRecord_errors
18
+ def initialize(*params)
19
+ super(*params)
20
+ @errors=[]
21
+ end
22
+
23
+ def add_to_base(value)
24
+ @errors+=[value]
25
+ end
26
+
27
+ def add(attribute, value)
28
+ @errors+=["#{attribute.to_s} #{value}"]
29
+ end
30
+
31
+ def count
32
+ return length
33
+ end
34
+
35
+ def length
36
+ return @errors.length
37
+ end
38
+
39
+ def size
40
+ return length
41
+ end
42
+
43
+ def full_messages
44
+ return @errors
45
+ end
46
+
47
+ def clear
48
+ @errors.clear
49
+ end
50
+
51
+ def empty?
52
+ @errors.empty?
53
+ end
54
+ end
15
55
 
16
56
  end
17
57
 
@@ -0,0 +1,37 @@
1
+ # These ones take longer to run
2
+
3
+ require 'test/unit'
4
+ require File.join(File.dirname(__FILE__), "/../lib/simple_record")
5
+ require File.join(File.dirname(__FILE__), "./test_helpers")
6
+ require File.join(File.dirname(__FILE__), "./test_base")
7
+ require "yaml"
8
+ require 'aws'
9
+ require 'my_model'
10
+ require 'my_child_model'
11
+ require 'active_support'
12
+
13
+ # Tests for SimpleRecord
14
+ #
15
+
16
+ class TestResultsArray < TestBase
17
+
18
+
19
+ # ensures that it uses next token and what not
20
+ def test_big_result
21
+ i = TestHelpers.clear_out_my_models
22
+ num_made = 110
23
+ num_made.times do |i|
24
+ mm = MyModel.create(:name=>"Travis", :age=>i, :cool=>true)
25
+ end
26
+ rs = MyModel.find(:all) # should get 100 at a time
27
+ assert rs.size == num_made
28
+ i = 0
29
+ rs.each do |x|
30
+ #puts 'x=' + x.id
31
+ i+=1
32
+ end
33
+ assert i == num_made
34
+ end
35
+
36
+
37
+ end
data/test/test_base.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), "/../lib/simple_record")
3
+ require File.join(File.dirname(__FILE__), "./test_helpers")
4
+ require "yaml"
5
+ require 'aws'
6
+ require 'my_model'
7
+ require 'my_child_model'
8
+ require 'active_support'
9
+
10
+ class TestBase < Test::Unit::TestCase
11
+
12
+ def setup
13
+ @config = YAML::load(File.open(File.expand_path("~/.test-configs/simple_record.yml")))
14
+ #puts 'inspecting config = ' + @config.inspect
15
+
16
+ # Establish AWS connection directly
17
+ @@sdb = Aws::SdbInterface.new(@config['amazon']['access_key'], @config['amazon']['secret_key'], {:connection_mode => :per_request, :protocol => "http", :port => 80})
18
+
19
+ SimpleRecord.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'], :connection_mode=>:single)
20
+ SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
21
+ end
22
+
23
+ def teardown
24
+ SimpleRecord.close_connection
25
+ end
26
+
27
+
28
+ end
@@ -0,0 +1,15 @@
1
+
2
+ class TestHelpers
3
+
4
+ def self.clear_out_my_models
5
+ mms = MyModel.find(:all)
6
+ puts 'mms.size=' + mms.size.to_s
7
+ i = 0
8
+ mms.each do |x|
9
+ puts 'deleting=' + i.to_s
10
+ x.delete
11
+ i+=1
12
+ end
13
+ end
14
+
15
+ end
@@ -1,5 +1,7 @@
1
1
  require 'test/unit'
2
- require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
2
+ require File.join(File.dirname(__FILE__), "/../lib/simple_record")
3
+ require File.join(File.dirname(__FILE__), "./test_helpers")
4
+ require File.join(File.dirname(__FILE__), "./test_base")
3
5
  require "yaml"
4
6
  require 'aws'
5
7
  require 'my_model'
@@ -9,22 +11,7 @@ require 'active_support'
9
11
  # Tests for SimpleRecord
10
12
  #
11
13
 
12
- class TestSimpleRecord < Test::Unit::TestCase
13
-
14
- def setup
15
- @config = YAML::load(File.open(File.expand_path("~/.test-configs/simple_record.yml")))
16
- #puts 'inspecting config = ' + @config.inspect
17
-
18
- # Establish AWS connection directly
19
- @@sdb = Aws::SdbInterface.new(@config['amazon']['access_key'], @config['amazon']['secret_key'], {:connection_mode => :per_request, :protocol => "http", :port => 80})
20
-
21
- SimpleRecord.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'], :connection_mode=>:single)
22
- SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
23
- end
24
-
25
- def teardown
26
- SimpleRecord.close_connection
27
- end
14
+ class TestSimpleRecord < TestBase
28
15
 
29
16
 
30
17
  def test_save_get
@@ -238,34 +225,6 @@ class TestSimpleRecord < Test::Unit::TestCase
238
225
 
239
226
  end
240
227
 
241
- # ensures that it uses next token and what not
242
- def test_big_result
243
- i = clear_out_my_models
244
- num_made = 110
245
- num_made.times do |i|
246
- mm = MyModel.create(:name=>"Travis", :age=>i, :cool=>true)
247
- end
248
- rs = MyModel.find(:all) # should get 100 at a time
249
- assert rs.size == num_made
250
- i = 0
251
- rs.each do |x|
252
- #puts 'x=' + x.id
253
- i+=1
254
- end
255
- assert i == num_made
256
- end
257
-
258
- def clear_out_my_models
259
- mms = MyModel.find(:all)
260
- puts 'mms.size=' + mms.size.to_s
261
- i = 0
262
- mms.each do |x|
263
- puts 'deleting=' + i.to_s
264
- x.delete
265
- i+=1
266
- end
267
- end
268
-
269
228
  def test_results_array
270
229
  mms = MyModel.find(:all) # select 2
271
230
  assert !mms.first.nil?
@@ -363,7 +322,7 @@ class TestSimpleRecord < Test::Unit::TestCase
363
322
 
364
323
  def test_null
365
324
  puts Time.now.to_i.to_s
366
- clear_out_my_models
325
+ TestHelpers.clear_out_my_models
367
326
 
368
327
  mm = MyModel.new(:name=>"birthay is null")
369
328
  mm.save
@@ -495,4 +454,13 @@ class TestSimpleRecord < Test::Unit::TestCase
495
454
  assert mm.name = mm2.name
496
455
  end
497
456
 
457
+ def test_update_attributes
458
+ mm = MyModel.new({:name=>"myname"})
459
+ mm.save
460
+
461
+ mm.update_attributes(:name=>"name2", :age=>21)
462
+ assert mm.name == "name2", "Name is #{mm.name}"
463
+ assert mm.age == 21
464
+ end
465
+
498
466
  end
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.46
4
+ version: 1.1.47
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -79,5 +79,8 @@ test_files:
79
79
  - test/my_child_model.rb
80
80
  - test/my_model.rb
81
81
  - test/paging_array_test.rb
82
+ - test/results_tests.rb
82
83
  - test/temp_test.rb
84
+ - test/test_base.rb
85
+ - test/test_helpers.rb
83
86
  - test/test_simple_record.rb