simple_record 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # AWS_ACCESS_KEY_ID='XXXXX'
12
12
  # AWS_SECRET_ACCESS_KEY='YYYYY'
13
- # RightAws::ActiveSdb.establish_connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY, {:multi_thread => false})
13
+ # RightAws::ActiveSdb.establish_connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
14
14
  # # Save an object
15
15
  # mm = MyModel.new
16
16
  # mm.name = "Travis"
@@ -29,7 +29,7 @@ require 'local_cache'
29
29
 
30
30
  module SimpleRecord
31
31
 
32
- VERSION = '1.0.2'
32
+ VERSION = '1.0.3'
33
33
 
34
34
  class Base < RightAws::ActiveSdb::Base
35
35
 
@@ -124,6 +124,7 @@ VERSION = '1.0.2'
124
124
  else
125
125
  ret = self[arg.to_s]
126
126
  end
127
+ return nil if ret.nil?
127
128
  return un_offset_if_int(arg, ret)
128
129
  end
129
130
 
@@ -153,6 +154,13 @@ VERSION = '1.0.2'
153
154
  # @@dates = args
154
155
  # puts 'dates=' + @@dates.inspect
155
156
  end
157
+
158
+ @@booleans = []
159
+ def self.are_booleans(*args)
160
+ args.each do |arg|
161
+ @@booleans << arg if @@booleans.index(arg).nil?
162
+ end
163
+ end
156
164
 
157
165
  @@virtuals=[]
158
166
  def self.has_virtuals(*args)
@@ -184,8 +192,8 @@ VERSION = '1.0.2'
184
192
  # puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil?
185
193
  ret = nil
186
194
  arg_id = arg.to_s + '_id'
187
- if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0
188
- if !@@cache_store.nil?
195
+ if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != ''
196
+ if !@@cache_store.nil?
189
197
  arg_id_val = @attributes[arg_id][0]
190
198
  cache_key = self.class.cache_key(class_name, arg_id_val)
191
199
  # puts 'cache_key=' + cache_key
@@ -502,7 +510,25 @@ VERSION = '1.0.2'
502
510
  for d in @@dates
503
511
  # puts 'converting created: ' + self['created'].inspect
504
512
  if d == arg
505
- x = DateTime.parse(x)
513
+ x = to_date(x)
514
+ end
515
+ # if !self[d].nil?
516
+ # self[d].collect!{ |d2|
517
+ # if d2.is_a?(String)
518
+ # DateTime.parse(d2)
519
+ # else
520
+ # d2
521
+ # end
522
+ # }
523
+ # end
524
+ # puts 'after=' + self['created'].inspect
525
+ end
526
+ end
527
+ if !@@booleans.nil?
528
+ for b in @@booleans
529
+ # puts 'converting created: ' + self['created'].inspect
530
+ if b == arg
531
+ x = to_bool(x)
506
532
  end
507
533
  # if !self[d].nil?
508
534
  # self[d].collect!{ |d2|
@@ -518,6 +544,25 @@ VERSION = '1.0.2'
518
544
  end
519
545
  x
520
546
  end
547
+
548
+
549
+ def to_date(x)
550
+ if x.is_a?(String)
551
+ DateTime.parse(x)
552
+ else
553
+ x
554
+ end
555
+
556
+ end
557
+
558
+ def to_bool(x)
559
+ if x.is_a?(String)
560
+ x == "true"
561
+ else
562
+ x
563
+ end
564
+ end
565
+
521
566
 
522
567
  def un_offset_int(x)
523
568
  if x.is_a?(String)
@@ -621,9 +666,16 @@ This is done on getters now
621
666
 
622
667
  def self.find(*params)
623
668
  reload=true
624
- all=false
625
- select=false
626
- select_attributes=[]
669
+ first=false
670
+ all=false
671
+ select=false
672
+ select_attributes=[]
673
+
674
+ if params.size > 0
675
+ all = params[0] == :all
676
+ first = params[0] == :first
677
+ end
678
+
627
679
 
628
680
  # Pad and Offset number attributes
629
681
  options = params[1]
@@ -632,29 +684,49 @@ This is done on getters now
632
684
 
633
685
  # puts 'after collect=' + params.inspect
634
686
 
635
- results = []
687
+ results = all ? [] : nil
636
688
  begin
637
689
  results=super(*params)
638
690
  cache_results(results)
639
- rescue RightAws::AwsError
691
+ rescue RightAws::AwsError, RightAws::ActiveSdb::ActiveSdbError
640
692
  puts "RESCUED: " + $!
641
- if ($!.message().index("NoSuchDomain") == nil)
693
+ if ($!.message().index("NoSuchDomain") != nil)
694
+ # this is ok
695
+ elsif ($!.message() =~ @@regex_no_id)
696
+ results = nil
697
+ else
642
698
  raise $!
643
699
  end
644
700
  end
645
701
  return results
646
702
  end
647
703
 
648
- def select(*params)
649
- options = params[1]
704
+ @@regex_no_id = /.*Couldn't find.*with ID.*/
705
+ def self.select(*params)
706
+ first=false
707
+ all=false
708
+ select=false
709
+ select_attributes=[]
710
+
711
+ if params.size > 0
712
+ all = params[0] == :all
713
+ first = params[0] == :first
714
+ end
715
+
716
+ options = params[1]
650
717
  convert_condition_params(options)
651
- results = []
718
+
719
+ results = all ? [] : nil
652
720
  begin
653
721
  results=super(*params)
654
722
  cache_results(results)
655
- rescue RightAws::AwsError
723
+ rescue RightAws::AwsError, RightAws::ActiveSdb::ActiveSdbError
656
724
  puts "RESCUED: " + $!
657
- if ($!.message().index("NoSuchDomain") == nil)
725
+ if ($!.message().index("NoSuchDomain") != nil)
726
+ # this is ok
727
+ elsif ($!.message() =~ @@regex_no_id)
728
+ results = nil
729
+ else
658
730
  raise $!
659
731
  end
660
732
  end
@@ -667,7 +739,7 @@ This is done on getters now
667
739
  conditions = options[:conditions]
668
740
  if !conditions.nil? && conditions.size > 1
669
741
  # all after first are values
670
- conditions[1...conditions.size] = conditions[1...conditions.size].collect { |x|
742
+ conditions.collect! { |x|
671
743
  self.pad_and_offset(x)
672
744
  }
673
745
  end
@@ -1,8 +1,74 @@
1
1
  require "test/unit"
2
- require "simple_record"
2
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
3
+ require "yaml"
4
+ require 'right_aws'
5
+ require 'my_model'
3
6
 
4
7
  class TestSimpleRecord < Test::Unit::TestCase
5
- def test_sanity
6
- flunk "write tests or I will kneecap you"
8
+
9
+ def setup
10
+ @config = YAML::load(File.read('test-config.yml'))
11
+ puts 'akey=' + @config['amazon']['access_key']
12
+ puts 'skey=' + @config['amazon']['secret_key']
13
+ RightAws::ActiveSdb.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'])
14
+ SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
15
+ end
16
+
17
+ def teardown
18
+ RightAws::ActiveSdb.close_connection()
19
+ end
20
+
21
+ def test_save_get
22
+ mm = MyModel.new
23
+ mm.name = "Travis"
24
+ mm.age = 32
25
+ mm.cool = true
26
+ mm.save
27
+ id = mm.id
28
+ puts 'id=' + id.to_s
29
+ # Get the object back
30
+ mm2 = MyModel.find(id)
31
+ puts 'got=' + mm2.name + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
32
+ puts mm2.cool.class.name
33
+ assert mm2.id == mm.id
34
+ assert mm2.age == mm.age
35
+ assert mm2.cool == mm.cool
36
+ end
37
+
38
+ def test_callbacks
39
+ # these DO NOT work right now, all objects get all callbacks
40
+ # I tried like this, seem to be getting somewhere.
41
+ #
42
+ # class << self;
43
+ # @@callbacks.each do |callback|
44
+ # #we first have to make an initialized array for each of the callbacks, to prevent problems if they are not called
45
+ # puts 'setting callback ' + callback.to_s + ' on ' + self.inspect
46
+ # eval %{
47
+ #
48
+ # # add the methods to the class
49
+ # def #{callback}(*args)
50
+ # args.each do |arg|
51
+ # cb_names = self.instance_variable_get(:@#{callback}_names)
52
+ # cb_names = [] if cb_names.nil?
53
+ # cb_names << arg.to_s if cb_names.index(arg.to_s).nil?
54
+ # self.instance_variable_set(:@#{callback}_names, cb_names)
55
+ # end
56
+ ## asdf @@#{callback}_names=args.map{|arg| arg.to_s}
57
+ # end
58
+ #
59
+ # # now we run the methods in the callback array for this class
60
+ #send :define_method, "run_#{callback}" do
61
+ ## def run_#{callback}
62
+ # cb_names = self.instance_variable_get(:@#{callback}_names)
63
+ # cb_names.each { |name|
64
+ # unless eval(name)
65
+ # return false
66
+ # end
67
+ # }
68
+ # return true
69
+ # end
70
+ # }
71
+ # end
72
+ # end
7
73
  end
8
74
  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.0.2
4
+ version: 1.0.3
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-04-25 00:00:00 -07:00
12
+ date: 2009-05-19 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency