appoxy-simple_record 1.0.26 → 1.0.27

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ module SimpleRecord
2
+ class ResultsArray
3
+
4
+ attr_reader :next_token, :clz, :params, :items, :i
5
+
6
+ def initialize(clz=nil, params=[], items=[], next_token=nil)
7
+ @clz = clz
8
+ puts 'class=' + clz.inspect
9
+ @params = params
10
+ #puts 'params in ra=' + params.inspect
11
+ @items = items
12
+ @next_token = next_token
13
+ @i = 0
14
+ end
15
+
16
+ def << (val)
17
+ @items << val
18
+ end
19
+
20
+ def [](i)
21
+ @items[i]
22
+ end
23
+
24
+
25
+ def each(&blk)
26
+ limit = nil
27
+ if params.size > 1
28
+ options = params[1]
29
+ limit = options[:limit]
30
+ else
31
+ options = {}
32
+ params[1] = options
33
+ end
34
+
35
+ @items.each do |v|
36
+ puts @i.to_s
37
+ yield v
38
+ @i += 1
39
+ if !limit.nil? && @i >= limit
40
+ return
41
+ end
42
+ end
43
+ # no more items, but is there a next token?
44
+ return if clz.nil?
45
+
46
+ unless next_token.nil?
47
+ puts 'finding more items...'
48
+ #puts 'params in block=' + params.inspect
49
+ options[:next_token] = next_token
50
+ res = clz.find(*params)
51
+ @items = res.items
52
+ @next_token = res.next_token
53
+ each(&blk)
54
+ end
55
+ end
56
+ end
57
+ end
58
+
data/lib/simple_record.rb CHANGED
@@ -25,6 +25,8 @@
25
25
 
26
26
  require 'right_aws'
27
27
  require 'sdb/active_sdb'
28
+ #require 'results_array'
29
+ require File.expand_path(File.dirname(__FILE__) + "/results_array")
28
30
 
29
31
  module SimpleRecord
30
32
 
@@ -841,6 +843,7 @@ This is done on getters now
841
843
  end
842
844
 
843
845
  def self.find(*params)
846
+ puts 'params=' + params.inspect
844
847
  reload=true
845
848
  first=false
846
849
  all=false
@@ -854,15 +857,20 @@ This is done on getters now
854
857
 
855
858
 
856
859
  # Pad and Offset number attributes
857
- options = params[1]
858
- #puts 'options=' + options.inspect
860
+ options = {}
861
+ if params.size > 1
862
+ options = params[1]
863
+ #puts 'options=' + options.inspect
864
+ #puts 'after collect=' + options.inspect
865
+ end
859
866
  convert_condition_params(options)
860
- #puts 'after collect=' + options.inspect
867
+ #puts 'params2=' + params.inspect
861
868
 
862
869
  results = all ? [] : nil
863
870
  begin
864
871
  results=super(*params)
865
872
  cache_results(results)
873
+ results = SimpleRecord::ResultsArray.new(self, params, results, next_token)
866
874
  rescue RightAws::AwsError, RightAws::ActiveSdb::ActiveSdbError
867
875
  puts "RESCUED: " + $!.message
868
876
  if ($!.message().index("NoSuchDomain") != nil)
@@ -909,15 +917,15 @@ This is done on getters now
909
917
  end
910
918
 
911
919
  def self.convert_condition_params(options)
912
- if !options.nil? && options.size > 0
913
- conditions = options[:conditions]
914
- if !conditions.nil? && conditions.size > 1
915
- # all after first are values
916
- conditions.collect! { |x|
917
- self.pad_and_offset(x)
918
- }
919
- end
920
+ return if options.nil?
921
+ conditions = options[:conditions]
922
+ if !conditions.nil? && conditions.size > 1
923
+ # all after first are values
924
+ conditions.collect! { |x|
925
+ self.pad_and_offset(x)
926
+ }
920
927
  end
928
+
921
929
  end
922
930
 
923
931
  def self.cache_results(results)
@@ -0,0 +1,15 @@
1
+
2
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/results_array")
3
+
4
+ array = SimpleRecord::ResultsArray.new()
5
+ #array.extend(ResultsArray)
6
+
7
+ 500.times do |i|
8
+ array << "_ob_" + i.to_s
9
+ end
10
+
11
+ array.each do |v|
12
+ puts v.to_s
13
+ end
14
+
15
+ puts array[10]
@@ -18,6 +18,7 @@ class TestSimpleRecord < Test::Unit::TestCase
18
18
  SimpleRecord.close_connection()
19
19
  end
20
20
 
21
+
21
22
  def test_save_get
22
23
  mm = MyModel.new
23
24
  mm.name = "Travis"
@@ -176,4 +177,16 @@ class TestSimpleRecord < Test::Unit::TestCase
176
177
  #MyChildModel.defined_attributes.inspect
177
178
 
178
179
  end
180
+
181
+
182
+ # ensures that it uses next token and what not
183
+ def test_big_result
184
+ #110.times do |i|
185
+ # MyModel
186
+ #end
187
+ #rs = MyModel.find(:all, :limit=>300)
188
+ #rs.each do |x|
189
+ # puts 'x=' + x.id
190
+ #end
191
+ end
179
192
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appoxy-simple_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.26
4
+ version: 1.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -23,6 +23,7 @@ extensions: []
23
23
  extra_rdoc_files:
24
24
  - README.markdown
25
25
  files:
26
+ - lib/results_array.rb
26
27
  - lib/simple_record.rb
27
28
  - README.markdown
28
29
  has_rdoc: true
@@ -55,5 +56,6 @@ summary: Drop in replacement for ActiveRecord to Amazon SimpleDB instead.
55
56
  test_files:
56
57
  - test/my_child_model.rb
57
58
  - test/my_model.rb
59
+ - test/paging_array_test.rb
58
60
  - test/temp_test.rb
59
61
  - test/test_simple_record.rb