simple_record 1.3.9 → 1.3.11

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -84,7 +84,7 @@ module SimpleRecord
84
84
  @aws_access_key = aws_access_key
85
85
  @aws_secret_key = aws_secret_key
86
86
  @@options.merge!(params)
87
- puts 'SimpleRecord.establish_connection with options: ' + @@options.inspect
87
+ #puts 'SimpleRecord.establish_connection with options: ' + @@options.inspect
88
88
  Aws::ActiveSdb.establish_connection(aws_access_key, aws_secret_key, @@options)
89
89
  end
90
90
 
@@ -193,31 +193,32 @@ module SimpleRecord
193
193
 
194
194
  # Sets the domain name for this class
195
195
  def self.set_domain_name(table_name)
196
- # puts 'setting domain name for class ' + self.inspect + '=' + table_name
197
- #@domain_name_for_class = table_name
198
196
  super
199
197
  end
200
198
 
201
199
 
202
200
  def domain
203
- self.class.domain # super # super.domain
201
+ self.class.domain
204
202
  end
205
203
 
206
204
  def self.domain
207
- #return self.get_domain_name unless self.get_domain_name.nil?
208
- d = super
209
- # puts 'in self.domain, d=' + d.to_s + ' domain_prefix=' + SimpleRecord::Base.domain_prefix.to_s
210
- domain_name_for_class = (SimpleRecord::Base.domain_prefix || "") + d.to_s
211
- #self.set_domain_name(domain_name_for_class)
205
+ unless @domain
206
+ # This strips off the module if there is one.
207
+ n2 = name.split('::').last || name
208
+ puts 'n2=' + n2
209
+ if defined? ActiveSupport::CoreExtensions::String::Inflections
210
+ @domain = n2.tableize
211
+ else
212
+ @domain = n2.downcase
213
+ end
214
+ set_domain_name @domain
215
+ end
216
+ domain_name_for_class = (SimpleRecord::Base.domain_prefix || "") + @domain.to_s
212
217
  domain_name_for_class
213
218
  end
214
219
 
215
220
  def get_attribute_sdb(name)
216
221
  name = name.to_sym
217
- # arg = arg.to_s
218
- # puts "get_attribute_sdb(#{arg}) - #{arg.class.name}"
219
- # puts 'self[]=' + self.inspect
220
- # att_meta = get_att_meta(name)
221
222
  ret = strip_array(@attributes[sdb_att_name(name)])
222
223
  return ret
223
224
  end
@@ -230,10 +231,7 @@ module SimpleRecord
230
231
  def get_att_meta(name)
231
232
  name_s = name.to_s
232
233
  att_meta = defined_attributes_local[name.to_sym]
233
- # puts 'name_s=' + name_s
234
- # puts 'end of string=' + name_s[-3..-1] if name_s.length > 4
235
234
  if att_meta.nil? && has_id_on_end(name_s)
236
- # puts 'strip _id=' + name_s[0..-4].to_s
237
235
  att_meta = defined_attributes_local[name_s[0..-4].to_sym]
238
236
  end
239
237
  return att_meta
@@ -306,7 +304,7 @@ module SimpleRecord
306
304
 
307
305
  def domain_ok(ex)
308
306
  if (ex.message().index("NoSuchDomain") != nil)
309
- puts "Creating new SimpleDB Domain: " + domain
307
+ #puts "Creating new SimpleDB Domain: " + domain
310
308
  self.class.create_domain
311
309
  return true
312
310
  end
@@ -792,6 +790,7 @@ module SimpleRecord
792
790
  #
793
791
  # Extra options:
794
792
  # :per_token => the number of results to return per next_token, max is 2500.
793
+ # :consistent_read => true/false -- as per http://developer.amazonwebservices.com/connect/entry.jspa?externalID=3572
795
794
  def self.find(*params)
796
795
  #puts 'params=' + params.inspect
797
796
  q_type = :all
@@ -809,9 +808,11 @@ module SimpleRecord
809
808
  #puts 'after collect=' + options.inspect
810
809
  convert_condition_params(options)
811
810
  per_token = options[:per_token]
812
- if per_token
811
+ consistent_read = options[:consistent_read]
812
+ if per_token || consistent_read then
813
813
  op_dup = options.dup
814
814
  op_dup[:limit] = per_token # simpledb uses Limit as a paging thing, not what is normal
815
+ op_dup[:consistent_read] = consistent_read
815
816
  params[1] = op_dup
816
817
  end
817
818
 
@@ -820,13 +821,23 @@ module SimpleRecord
820
821
 
821
822
  results = q_type == :all ? [] : nil
822
823
  begin
823
- results=super(*params)
824
- # puts "RESULT=" + results.inspect
824
+ results=find_with_metadata(*params)
825
+ # puts "RESULT=" + results.inspect
825
826
  #puts 'params3=' + params.inspect
826
827
  SimpleRecord.stats.selects += 1
827
- if q_type != :count
828
+ if q_type == :count
829
+ results = results[:count]
830
+ elsif q_type == :first
831
+ ret = results[:items].first
832
+ # todo: we should store request_id and box_usage with the object maybe?
833
+ cache_results(ret)
834
+ results = ret
835
+ elsif results[:single]
836
+ results = results[:single]
828
837
  cache_results(results)
829
- if results.is_a?(Array)
838
+ else
839
+ if results[:items] #.is_a?(Array)
840
+ cache_results(results[:items])
830
841
  results = SimpleRecord::ResultsArray.new(self, params, results, next_token)
831
842
  end
832
843
  end
@@ -861,7 +872,7 @@ module SimpleRecord
861
872
 
862
873
  # This gets less and less efficient the higher the page since SimpleDB has no way
863
874
  # to start at a specific row. So it will iterate from the first record and pull out the specific pages.
864
- def self.paginate(options={})
875
+ def self.paginate(options={})
865
876
  # options = args.pop
866
877
  # puts 'paginate options=' + options.inspect if SimpleRecord.logging?
867
878
  page = options[:page] || 1
@@ -893,6 +904,13 @@ module SimpleRecord
893
904
  if !cache_store.nil? && !results.nil?
894
905
  if results.is_a?(Array)
895
906
  # todo: cache each result
907
+ results.each do |item|
908
+ class_name = item.class.name
909
+ id = item.id
910
+ cache_key = self.cache_key(class_name, id)
911
+ #puts 'caching result at ' + cache_key + ': ' + results.inspect
912
+ cache_store.write(cache_key, item, :expires_in =>30)
913
+ end
896
914
  else
897
915
  class_name = results.class.name
898
916
  id = results.id
@@ -5,10 +5,10 @@ module SimpleRecord
5
5
  class ResultsArray
6
6
  include Enumerable
7
7
 
8
- attr_reader :next_token, :clz, :params, :items, :i
8
+ attr_reader :next_token, :clz, :params, :items, :i, :box_usage, :request_id
9
9
 
10
10
 
11
- def initialize(clz=nil, params=[], items=[], next_token=nil)
11
+ def initialize(clz=nil, params=[], results=nil, next_token=nil)
12
12
  @clz = clz
13
13
  #puts 'class=' + clz.inspect
14
14
  @params = params
@@ -16,9 +16,12 @@ module SimpleRecord
16
16
  options = {}
17
17
  @params[1] = options
18
18
  end
19
- @items = items
20
- @currentset_items = items
19
+ @items = results[:items]
20
+ @currentset_items = results[:items]
21
21
  @next_token = next_token
22
+ puts 'bu=' + results[:box_usage]
23
+ @box_usage = results[:box_usage].to_f
24
+ @request_id = results[:request_id]
22
25
  @options = @params[1]
23
26
  if @options[:page]
24
27
  load_to(@options[:per_page] * @options[:page])
@@ -80,6 +83,9 @@ module SimpleRecord
80
83
  end
81
84
 
82
85
  def size
86
+ if @options[:per_page]
87
+ return @items.size - @start_at
88
+ end
83
89
  if @next_token.nil?
84
90
  return @items.size
85
91
  end
@@ -87,13 +93,13 @@ module SimpleRecord
87
93
  # puts '@params=' + @params.inspect
88
94
  params_for_count = @params.dup
89
95
  params_for_count[0] = :count
90
- params_for_count[1] = params_for_count[1].dup # for deep clone
96
+ params_for_count[1] = params_for_count[1].dup # for deep clone
91
97
  params_for_count[1].delete(:limit)
92
98
 
93
99
  # puts '@params2=' + @params.inspect
94
100
  # puts 'params_for_count=' + params_for_count.inspect
95
101
  @count = clz.find(*params_for_count)
96
- # puts '@count=' + @count.to_s
102
+ # puts '@count=' + @count.to_s
97
103
  @count
98
104
  end
99
105
 
@@ -19,7 +19,7 @@ class TestPagination < TestBase
19
19
  i = 20
20
20
  (1..3).each do |page|
21
21
  models = MyModel.paginate :page=>page, :per_page=>5, :order=>"created desc"
22
- assert models.size == 5
22
+ assert models.size == 5, "models.size=#{models.size}"
23
23
  models.each do |m|
24
24
  i -= 1
25
25
  puts m.name
@@ -306,7 +306,9 @@ class TestSimpleRecord < TestBase
306
306
  assert count > 0
307
307
 
308
308
  mms = MyModel.find(:all) # select 2
309
+ puts 'mms=' + mms.inspect
309
310
  assert mms.size > 0 # select 3
311
+ puts 'mms=' + mms.inspect
310
312
  assert mms.size == count, "size != count! size=" + mms.size.to_s + " count=" + count.to_s
311
313
  assert SimpleRecord.stats.selects == 3, "should have been 3 select, but was actually #{SimpleRecord.stats.selects}" # count should not have been called twice
312
314
 
@@ -480,15 +482,15 @@ class TestSimpleRecord < TestBase
480
482
 
481
483
  mm = MyModel.find(:first, :conditions=>["date1 >= ?", 1.days.ago.to_date])
482
484
  puts 'mm=' + mm.inspect
483
- assert mm
485
+ assert mm.is_a? MyModel
484
486
 
485
487
  mm = MyModel.find(:first, :conditions=>["date2 >= ?", 1.minutes.ago])
486
488
  puts 'mm=' + mm.inspect
487
- assert mm
489
+ assert mm.is_a? MyModel
488
490
 
489
491
  mm = MyModel.find(:first, :conditions=>["date3 >= ?", 1.minutes.ago])
490
492
  puts 'mm=' + mm.inspect
491
- assert mm
493
+ assert mm.is_a? MyModel
492
494
 
493
495
  end
494
496
 
@@ -662,5 +664,18 @@ class TestSimpleRecord < TestBase
662
664
 
663
665
  end
664
666
 
667
+ def test_box_usage
668
+ mm = MyModel.new
669
+ mm.name = "whatever"
670
+ mm.age = "1"
671
+ mm.save
672
+ sleep 1
673
+
674
+ mms = MyModel.all
675
+
676
+ assert mms.box_usage && mms.box_usage > 0
677
+ assert mms.request_id
678
+
679
+ end
665
680
 
666
681
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_record
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 3
9
- - 9
10
- version: 1.3.9
9
+ - 11
10
+ version: 1.3.11
11
11
  platform: ruby
12
12
  authors:
13
13
  - Travis Reeder
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-06-03 00:00:00 -07:00
20
+ date: 2010-06-25 00:00:00 -07:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency