simple_record 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -29,7 +29,7 @@ require 'local_cache'
29
29
 
30
30
  module SimpleRecord
31
31
 
32
- VERSION = '1.0.3'
32
+ VERSION = '1.0.4'
33
33
 
34
34
  class Base < RightAws::ActiveSdb::Base
35
35
 
@@ -386,7 +386,44 @@ VERSION = '1.0.3'
386
386
 
387
387
  def save(*params)
388
388
  # puts 'SAVING: ' + self.inspect
389
+ ok = pre_save(*params)
390
+ if ok
391
+ begin
392
+ # puts 'is frozen? ' + self.frozen?.to_s + ' - ' + self.inspect
393
+ to_delete = get_atts_to_delete
394
+ if super(*params)
395
+ # puts 'SAVED super'
396
+ self.class.cache_results(self)
397
+ delete_niled(to_delete)
398
+ if run_after_save && is_create ? run_after_create : run_after_update
399
+ return true
400
+ else
401
+ #I thought about calling destroy here, but rails doesn't behave that way, so neither will I
402
+ return false
403
+ end
404
+ else
405
+ return false
406
+ end
407
+ rescue RightAws::AwsError
408
+ # puts "RESCUED in save: " + $!
409
+ if (domain_ok($!))
410
+ if !@create_domain_called
411
+ @create_domain_called = true
412
+ save(*params)
413
+ else
414
+ raise $!
415
+ end
416
+ else
417
+ raise $!
418
+ end
419
+ end
420
+ else
421
+ #@debug = "not saved"
422
+ return false
423
+ end
424
+ end
389
425
 
426
+ def pre_save(*params)
390
427
  if respond_to?('validate')
391
428
  validate
392
429
  # puts 'AFTER VALIDATIONS, ERRORS=' + errors.inspect
@@ -405,7 +442,10 @@ VERSION = '1.0.3'
405
442
  ok = before_update
406
443
  end
407
444
  end
408
- if ok && run_before_save && is_create ? run_before_create : run_before_update
445
+ if ok
446
+ ok = run_before_save && is_create ? run_before_create : run_before_update
447
+ end
448
+ if ok
409
449
  # puts 'ABOUT TO SAVE: ' + self.inspect
410
450
  # First we gotta pad and offset
411
451
  if !@@ints.nil?
@@ -416,9 +456,7 @@ VERSION = '1.0.3'
416
456
  # puts @attributes.inspect
417
457
  # puts @attributes[i.to_s].inspect
418
458
  arr = @attributes[i.to_s]
419
- arr.collect!{ |x|
420
- self.class.pad_and_offset(x)
421
- }
459
+ arr.collect!{ |x| self.class.pad_and_offset(x) }
422
460
  @attributes[i.to_s] = arr
423
461
  # puts 'after: ' + @attributes[i.to_s].inspect
424
462
  else
@@ -426,39 +464,8 @@ VERSION = '1.0.3'
426
464
  end
427
465
  end
428
466
  end
429
- begin
430
- # puts 'is frozen? ' + self.frozen?.to_s + ' - ' + self.inspect
431
- to_delete = get_atts_to_delete
432
- if super(*params)
433
- # puts 'SAVED super'
434
- self.class.cache_results(self)
435
- delete_niled(to_delete)
436
- if run_after_save && is_create ? run_after_create : run_after_update
437
- return true
438
- else
439
- #I thought about calling destroy here, but rails doesn't behave that way, so neither will I
440
- return false
441
- end
442
- else
443
- return false
444
- end
445
- rescue RightAws::AwsError
446
- # puts "RESCUED in save: " + $!
447
- if (domain_ok($!))
448
- if !@create_domain_called
449
- @create_domain_called = true
450
- save(*params)
451
- else
452
- raise $!
453
- end
454
- else
455
- raise $!
456
- end
457
- end
458
- else
459
- #@debug = "not saved"
460
- return false
461
467
  end
468
+ ok
462
469
  end
463
470
 
464
471
  def save_attributes(*params)
@@ -480,6 +487,25 @@ VERSION = '1.0.3'
480
487
  return to_delete
481
488
  end
482
489
 
490
+ # Run pre_save on each object, then runs batch_put_attributes
491
+ # Returns
492
+ def self.batch_save(objects)
493
+ results = []
494
+ to_save = []
495
+ if objects && objects.size > 0
496
+ objects.each do |o|
497
+ ok = o.pre_save
498
+ raise "Pre save failed on object with id = " + o.id if !ok
499
+ results << ok
500
+ next if !ok
501
+ o.pre_save2
502
+ to_save << RightAws::SdbInterface::Item.new(o.id, o.attributes, true)
503
+ end
504
+ end
505
+ connection.batch_put_attributes(domain, to_save)
506
+ results
507
+ end
508
+
483
509
  #
484
510
  # Usage: ClassName.delete id
485
511
  # todo: move to RightAWS
@@ -35,6 +35,29 @@ class TestSimpleRecord < Test::Unit::TestCase
35
35
  assert mm2.cool == mm.cool
36
36
  end
37
37
 
38
+ def test_batch_save
39
+ items = []
40
+ mm = MyModel.new
41
+ mm.name = "Travis"
42
+ mm.age = 32
43
+ mm.cool = true
44
+ items << mm
45
+ mm = MyModel.new
46
+ mm.name = "Tritt"
47
+ mm.age = 44
48
+ mm.cool = false
49
+ items << mm
50
+ MyModel.batch_save(items)
51
+ items.each do |item|
52
+ puts 'id=' + item.id
53
+ new_item = MyModel.find(item.id)
54
+ puts 'new=' + new_item.inspect
55
+ assert item.id == new_item.id
56
+ assert item.name == new_item.name
57
+ assert item.cool == new_item.cool
58
+ end
59
+ end
60
+
38
61
  def test_callbacks
39
62
  # these DO NOT work right now, all objects get all callbacks
40
63
  # I tried like this, seem to be getting somewhere.
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.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-19 00:00:00 -07:00
12
+ date: 2009-05-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency