Class: SimpleRecord::Base
Constant Summary
collapse
- @@cache_store =
nil
- @@regex_no_id =
/.*Couldn't find.*with ID.*/
- @@debug =
""
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Attributes
are_booleans, are_dates, are_ints, belongs_to, define_dirty_methods, defined_attributes, handle_virtuals, has_attributes, has_booleans, has_dates, has_ints, has_many, has_one, has_strings, has_virtuals, included
Methods included from Callbacks
#after_destroy, #before_destroy, included, setup_callbacks
#convert_dates_to_sdb, decrypt, encrypt, pad_and_offset, #pad_and_offset_ints_to_sdb, pass_hash, pass_hash_check, #ruby_to_sdb, #sdb_to_ruby, #to_bool, #to_date, un_offset_int, #unpad, #unpad_self, #wrap_if_required
Constructor Details
#initialize(attrs = {}) ⇒ Base
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/simple_record.rb', line 108
def initialize(attrs={})
initialize_base(attrs)
attrs.each_pair do |name, value|
set(name, value, true)
end
end
|
Class Attribute Details
.domain_prefix ⇒ Object
Returns the value of attribute domain_prefix.
163
164
165
|
# File 'lib/simple_record.rb', line 163
def domain_prefix
@domain_prefix
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
160
161
162
|
# File 'lib/simple_record.rb', line 160
def errors
@errors
end
|
Class Method Details
.all(*args) ⇒ Object
708
709
710
|
# File 'lib/simple_record.rb', line 708
def self.all(*args)
find(:all, *args)
end
|
.batch_save(objects, options = {}) ⇒ Object
Run pre_save on each object, then runs batch_put_attributes Returns
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
# File 'lib/simple_record.rb', line 473
def self.batch_save(objects, options={})
results = []
to_save = []
if objects && objects.size > 0
objects.each do |o|
ok = o.pre_save(options)
raise "Pre save failed on object [" + o.inspect + "]" if !ok
results << ok
next if !ok
o.pre_save2
to_save << Aws::SdbInterface::Item.new(o.id, o.attributes, true)
if to_save.size == 25
connection.batch_put_attributes(domain, to_save)
to_save.clear
end
end
end
connection.batch_put_attributes(domain, to_save) if to_save.size > 0
results
end
|
.cache_key(class_name, id) ⇒ Object
778
779
780
|
# File 'lib/simple_record.rb', line 778
def self.cache_key(class_name, id)
return class_name + "/" + id.to_s
end
|
.cache_results(results) ⇒ Object
764
765
766
767
768
769
770
771
772
773
774
775
776
|
# File 'lib/simple_record.rb', line 764
def self.cache_results(results)
if !cache_store.nil? && !results.nil?
if results.is_a?(Array)
else
class_name = results.class.name
id = results.id
cache_key = self.cache_key(class_name, id)
cache_store.write(cache_key, results, :expires_in =>30)
end
end
end
|
.cache_store ⇒ Object
174
175
176
|
# File 'lib/simple_record.rb', line 174
def self.cache_store
return @@cache_store
end
|
.cache_store=(cache) ⇒ Object
170
171
172
|
# File 'lib/simple_record.rb', line 170
def self.cache_store=(cache)
@@cache_store = cache
end
|
.convert_condition_params(options) ⇒ Object
752
753
754
755
756
757
758
759
760
761
762
|
# File 'lib/simple_record.rb', line 752
def self.convert_condition_params(options)
return if options.nil?
conditions = options[:conditions]
if !conditions.nil? && conditions.size > 1
conditions.collect! { |x|
Translations.pad_and_offset(x)
}
end
end
|
.count(*args) ⇒ Object
716
717
718
|
# File 'lib/simple_record.rb', line 716
def self.count(*args)
find(:count, *args)
end
|
.create(attributes = {}) ⇒ Object
645
646
647
648
|
# File 'lib/simple_record.rb', line 645
def self.create(attributes={})
super
end
|
.debug ⇒ Object
784
785
786
|
# File 'lib/simple_record.rb', line 784
def self.debug
@@debug
end
|
.delete(id) ⇒ Object
Usage: ClassName.delete id
497
498
499
|
# File 'lib/simple_record.rb', line 497
def self.delete(id)
connection.delete_attributes(domain, id)
end
|
.delete_all(*params) ⇒ Object
501
502
503
504
505
506
507
508
509
510
|
# File 'lib/simple_record.rb', line 501
def self.delete_all(*params)
obs = self.find(params)
i = 0
obs.each do |a|
a.delete
i+=1
end
return i
end
|
.destroy_all(*params) ⇒ Object
512
513
514
515
516
517
518
519
520
|
# File 'lib/simple_record.rb', line 512
def self.destroy_all(*params)
obs = self.find(params)
i = 0
obs.each do |a|
a.destroy
i+=1
end
return i
end
|
.domain ⇒ Object
201
202
203
204
205
206
207
208
|
# File 'lib/simple_record.rb', line 201
def self.domain
d = super
domain_name_for_class = (SimpleRecord::Base.domain_prefix || "") + d.to_s
domain_name_for_class
end
|
.find(*params) ⇒ Object
Usage: Find by ID:
MyModel.find(ID)
Query example:
MyModel.find(:all, :conditions=>["name = ?", name], :order=>"created desc", :limit=>10)
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
|
# File 'lib/simple_record.rb', line 660
def self.find(*params)
q_type = :all
select_attributes=[]
if params.size > 0
q_type = params[0]
end
options = {}
if params.size > 1
options = params[1]
convert_condition_params(options)
end
results = q_type == :all ? [] : nil
begin
results=super(*params)
SimpleRecord.stats.selects += 1
if q_type != :count
cache_results(results)
if results.is_a?(Array)
results = SimpleRecord::ResultsArray.new(self, params, results, next_token)
end
end
rescue Aws::AwsError, Aws::ActiveSdb::ActiveSdbError
if ($!.message().index("NoSuchDomain") != nil)
elsif ($!.message() =~ @@regex_no_id)
results = nil
else
raise $!
end
end
return results
end
|
.first(*args) ⇒ Object
712
713
714
|
# File 'lib/simple_record.rb', line 712
def self.first(*args)
find(:first, *args)
end
|
.get_encryption_key ⇒ Object
411
412
413
414
415
416
417
418
|
# File 'lib/simple_record.rb', line 411
def self.get_encryption_key()
key = SimpleRecord.options[:encryption_key]
return key
end
|
.inherited(base) ⇒ Object
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/simple_record.rb', line 141
def self.inherited(base)
Callbacks.setup_callbacks(base)
base.has_dates :created, :updated
base.before_create :set_created, :set_updated
base.before_update :set_updated
end
|
.paginate(options = {}) ⇒ Object
This gets less and less efficient the higher the page since SimpleDB has no way to start at a specific row. So it will iterate from the first record and pull out the specific pages.
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
# File 'lib/simple_record.rb', line 722
def self.paginate(options={})
puts 'paginate options=' + options.inspect if SimpleRecord.logging?
page = options[:page] || 1
per_page = options[:per_page] || self.per_page || 50
total = options[:total_entries]
options[:limit] = page * per_page
fr = find(:all, options)
puts 'fr.size=' + fr.size.to_s
ret = []
i = 0
p = 1
fr.each do |x|
if p == page
ret << x
end
i += 1
if i > 0 && i % per_page == 0
p += 1
if p > page
break
end
end
end
ret
end
|
.quote_regexp(a, re) ⇒ Object
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
# File 'lib/simple_record.rb', line 625
def self.quote_regexp(a, re)
a =~ re
if $&
before=$`
middle=$&
after=$'
before =~ /'$/
unless $&
return "#{before}'#{middle}'#{quote_regexp(after, re)}"
else
return "#{before}#{middle}#{quote_regexp(after, re)}"
end
else
return a
end
end
|
.sanitize_sql(*params) ⇒ Object
788
789
790
|
# File 'lib/simple_record.rb', line 788
def self.sanitize_sql(*params)
return ActiveRecord::Base.sanitize_sql(*params)
end
|
.select(*params) ⇒ Object
704
705
706
|
# File 'lib/simple_record.rb', line 704
def self.select(*params)
return find(*params)
end
|
.set_domain_name(table_name) ⇒ Object
Sets the domain name for this class
190
191
192
193
194
|
# File 'lib/simple_record.rb', line 190
def self.set_domain_name(table_name)
super
end
|
.set_domain_prefix(prefix) ⇒ Object
If you want a domain prefix for all your models, set it here.
179
180
181
182
|
# File 'lib/simple_record.rb', line 179
def self.set_domain_prefix(prefix)
self.domain_prefix = prefix
end
|
.set_table_name(table_name) ⇒ Object
185
186
187
|
# File 'lib/simple_record.rb', line 185
def self.set_table_name(table_name)
set_domain_name table_name
end
|
.table_name ⇒ Object
792
793
794
|
# File 'lib/simple_record.rb', line 792
def self.table_name
return domain
end
|
Instance Method Details
#[](attribute) ⇒ Object
265
266
267
|
# File 'lib/simple_record.rb', line 265
def []( attribute)
super
end
|
#[]=(attribute, values) ⇒ Object
260
261
262
263
|
# File 'lib/simple_record.rb', line 260
def []=(attribute, values)
make_dirty(attribute, values)
super
end
|
#cache_store ⇒ Object
288
289
290
|
# File 'lib/simple_record.rb', line 288
def cache_store
@@cache_store
end
|
#changed ⇒ Object
796
797
798
|
# File 'lib/simple_record.rb', line 796
def changed
return @dirty.keys
end
|
#changed? ⇒ Boolean
800
801
802
|
# File 'lib/simple_record.rb', line 800
def changed?
return @dirty.size > 0
end
|
#changes ⇒ Object
804
805
806
807
808
809
|
# File 'lib/simple_record.rb', line 804
def changes
ret = {}
@dirty.each_pair {|key, value| ret[key] = [value, get_attribute(key)]}
return ret
end
|
#clear_errors ⇒ Object
256
257
258
|
# File 'lib/simple_record.rb', line 256
def clear_errors
@errors=SimpleRecord_errors.new
end
|
#defined_attributes_local ⇒ Object
153
154
155
156
157
|
# File 'lib/simple_record.rb', line 153
def defined_attributes_local
ret = self.class.defined_attributes
ret.merge!(self.class.superclass.defined_attributes) if self.class.superclass.respond_to?(:defined_attributes)
end
|
#delete ⇒ Object
522
523
524
|
# File 'lib/simple_record.rb', line 522
def delete()
super
end
|
#delete_niled(to_delete) ⇒ Object
600
601
602
603
604
605
606
607
|
# File 'lib/simple_record.rb', line 600
def delete_niled(to_delete)
if to_delete.size > 0
SimpleRecord.stats.deletes += 1
delete_attributes to_delete
end
end
|
#destroy ⇒ Object
526
527
528
|
# File 'lib/simple_record.rb', line 526
def destroy
return run_before_destroy && delete && run_after_destroy
end
|
#domain ⇒ Object
197
198
199
|
# File 'lib/simple_record.rb', line 197
def domain
self.class.domain
end
|
#domain_ok(ex) ⇒ Object
292
293
294
295
296
297
298
299
|
# File 'lib/simple_record.rb', line 292
def domain_ok(ex)
if (ex.message().index("NoSuchDomain") != nil)
puts "Creating new SimpleDB Domain: " + domain
self.class.create_domain
return true
end
return false
end
|
#get_attribute(arg) ⇒ Object
Since SimpleDB supports multiple attributes per value, the values are an array. This method will return the value unwrapped if it’s the only, otherwise it will return the array.
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
# File 'lib/simple_record.rb', line 533
def get_attribute(arg)
arg_s = arg.to_s
@attributes_rb = {} unless @attributes_rb
ret = @attributes_rb[arg_s]
return ret if !ret.nil?
ret = get_attribute_sdb(arg)
ret = sdb_to_ruby(arg, ret)
@attributes_rb[arg_s] = ret
return ret
end
|
#get_attribute_sdb(name) ⇒ Object
210
211
212
213
214
215
216
|
# File 'lib/simple_record.rb', line 210
def get_attribute_sdb(name)
ret = strip_array(@attributes[sdb_att_name(name)])
return ret
end
|
#get_atts_to_delete ⇒ Object
458
459
460
461
462
463
464
465
466
467
468
469
|
# File 'lib/simple_record.rb', line 458
def get_atts_to_delete
to_delete = []
@attributes.each do |key, value|
if value.nil? || (value.is_a?(Array) && value.size == 0) || (value.is_a?(Array) && value.size == 1 && value[0] == nil)
to_delete << key
@attributes.delete(key)
end
end
return to_delete
end
|
#hash ⇒ Object
816
817
818
819
|
# File 'lib/simple_record.rb', line 816
def hash
id.hash
end
|
#initialize_base(attrs = {}) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/simple_record.rb', line 119
def initialize_base(attrs={})
Attributes.handle_virtuals(attrs)
@errors=SimpleRecord_errors.new
@dirty = {}
@attributes = {}
@attributes_rb = {}
@new_record = true
end
|
#initialize_from_db(attrs = {}) ⇒ Object
133
134
135
136
137
138
|
# File 'lib/simple_record.rb', line 133
def initialize_from_db(attrs={})
initialize_base(attrs)
attrs.each_pair do |k, v|
@attributes[k.to_s] = v
end
end
|
#invalid? ⇒ Boolean
323
324
325
|
# File 'lib/simple_record.rb', line 323
def invalid?
!valid?
end
|
#make_dirty(arg, value) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/simple_record.rb', line 240
def make_dirty(arg, value)
sdb_att_name = sdb_att_name(arg)
arg = arg.to_s
if @dirty.include?(sdb_att_name)
old = @dirty[sdb_att_name]
@dirty.delete(sdb_att_name) if value == old
else
old = get_attribute(arg)
@dirty[sdb_att_name] = old if value != old
end
end
|
#mark_as_old ⇒ Object
811
812
813
814
|
# File 'lib/simple_record.rb', line 811
def mark_as_old
super
@dirty = {}
end
|
#new_record? ⇒ Boolean
318
319
320
321
|
# File 'lib/simple_record.rb', line 318
def new_record?
super
end
|
#pre_save(options) ⇒ Object
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
# File 'lib/simple_record.rb', line 421
def pre_save(options)
is_create = self[:id].nil?
ok = run_before_validation && (is_create ? run_before_validation_on_create : run_before_validation_on_update)
return false unless ok
validate()
is_create ? validate_on_create : validate_on_update
if (!@errors.nil? && @errors.length > 0 )
return false
end
ok = run_after_validation && (is_create ? run_after_validation_on_create : run_after_validation_on_update)
return false unless ok
ok = respond_to?('before_save') ? before_save : true
if ok
if is_create && respond_to?('before_create')
ok = before_create
elsif !is_create && respond_to?('before_update')
ok = before_update
end
end
if ok
ok = run_before_save && (is_create ? run_before_create : run_before_update)
end
if ok
end
ok
end
|
#reload ⇒ Object
609
610
611
|
# File 'lib/simple_record.rb', line 609
def reload
super()
end
|
#save(options = {}) ⇒ Object
Options:
- :except => Array of attributes to NOT save
- :dirty => true - Will only store attributes that were modified. To make it save regardless and have it update the :updated value, include this and set it to false.
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
# File 'lib/simple_record.rb', line 345
def save(options={})
clear_errors
if options[:dirty]
return true if @dirty.size == 0
end
is_create = self[:id].nil?
ok = pre_save(options)
if ok
begin
if options[:dirty]
return true if @dirty.size == 0
options[:dirty_atts] = @dirty
end
to_delete = get_atts_to_delete
SimpleRecord.stats.puts += 1
if super(options)
self.class.cache_results(self)
delete_niled(to_delete)
if (is_create ? run_after_create : run_after_update) && run_after_save
return true
else
return false
end
else
return false
end
rescue Aws::AwsError
if (domain_ok($!))
if !@create_domain_called
@create_domain_called = true
save(options)
else
raise $!
end
else
raise $!
end
end
else
return false
end
end
|
#save!(options = {}) ⇒ Object
398
399
400
|
# File 'lib/simple_record.rb', line 398
def save!(options={})
save(options) || raise(RecordNotSaved)
end
|
#save_with_validation!(options = {}) ⇒ Object
402
403
404
405
406
407
408
|
# File 'lib/simple_record.rb', line 402
def save_with_validation!(options={})
if valid?
save
else
raise RecordInvalid.new(self)
end
end
|
#sdb_att_name(name) ⇒ Object
218
219
220
221
222
223
224
|
# File 'lib/simple_record.rb', line 218
def sdb_att_name(name)
att_meta = defined_attributes_local[name.to_sym]
if att_meta.type == :belongs_to
return "#{name}_id"
end
name.to_s
end
|
#set(name, value, dirtify = true) ⇒ Object
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
# File 'lib/simple_record.rb', line 556
def set(name, value, dirtify=true)
att_meta = defined_attributes_local[name.to_sym]
if att_meta.nil?
ends_with = name.to_s[-3, 3]
if ends_with == "_id"
n2 = name.to_s[0, name.length-3]
att_meta = defined_attributes_local[n2.to_sym]
attname = name.to_s
attvalue = value
name = n2
end
return if att_meta.nil?
else
if att_meta.type == :belongs_to
attname = name.to_s + '_id'
attvalue = value.nil? ? nil : value.id
else
attname = name.to_s
attvalue = att_meta.init_value(value)
end
end
attvalue = strip_array(attvalue)
make_dirty(name, attvalue) if dirtify
sdb_val = ruby_to_sdb(name, attvalue)
@attributes[attname] = sdb_val
@attributes_rb.delete(name.to_s)
end
|
#set_created ⇒ Object
270
271
272
273
274
275
276
277
|
# File 'lib/simple_record.rb', line 270
def set_created
set(:created, Time.now)
end
|
#set_updated ⇒ Object
279
280
281
282
283
284
285
|
# File 'lib/simple_record.rb', line 279
def set_updated
set(:updated, Time.now)
end
|
#strip_array(arg) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/simple_record.rb', line 226
def strip_array(arg)
if arg.class==Array
if arg.length==1
ret = arg[0]
else
ret = arg
end
else
ret = arg
end
return ret
end
|
#update_attributes(atts) ⇒ Object
614
615
616
617
|
# File 'lib/simple_record.rb', line 614
def update_attributes(atts)
set_attributes(atts)
save
end
|
#update_attributes!(atts) ⇒ Object
619
620
621
622
|
# File 'lib/simple_record.rb', line 619
def update_attributes!(atts)
set_attributes(atts)
save!
end
|
#valid? ⇒ Boolean
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
# File 'lib/simple_record.rb', line 301
def valid?
errors.clear
validate
if new_record?
validate_on_create
else
validate_on_update
end
errors.empty?
end
|
#validate ⇒ Object
327
328
329
|
# File 'lib/simple_record.rb', line 327
def validate
true
end
|
#validate_on_create ⇒ Object
331
332
333
|
# File 'lib/simple_record.rb', line 331
def validate_on_create
true
end
|
#validate_on_update ⇒ Object
335
336
337
|
# File 'lib/simple_record.rb', line 335
def validate_on_update
true
end
|