simple_record 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/simple_record.rb +673 -654
  2. metadata +2 -2
data/lib/simple_record.rb CHANGED
@@ -7,7 +7,7 @@
7
7
  # are_ints :age
8
8
  #
9
9
  # end
10
- #
10
+ #
11
11
  # AWS_ACCESS_KEY_ID='XXXXX'
12
12
  # AWS_SECRET_ACCESS_KEY='YYYYY'
13
13
  # RightAws::ActiveSdb.establish_connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
@@ -29,65 +29,65 @@ require 'local_cache'
29
29
 
30
30
  module SimpleRecord
31
31
 
32
- VERSION = '1.0.6'
32
+ VERSION = '1.0.7'
33
33
 
34
- class Base < RightAws::ActiveSdb::Base
34
+ class Base < RightAws::ActiveSdb::Base
35
35
 
36
- attr_accessor :errors
37
- @@domain_prefix = ''
38
- @domain_name_for_class = nil
36
+ attr_accessor :errors
37
+ @@domain_prefix = ''
38
+ @domain_name_for_class = nil
39
39
 
40
- @@cache_store = nil
41
- # Set the cache to use
42
- def self.cache_store=(cache)
43
- @@cache_store = cache
44
- end
45
- def self.cache_store
46
- return @@cache_store
47
- end
40
+ @@cache_store = nil
41
+ # Set the cache to use
42
+ def self.cache_store=(cache)
43
+ @@cache_store = cache
44
+ end
45
+ def self.cache_store
46
+ return @@cache_store
47
+ end
48
48
 
49
- # If you want a domain prefix for all your models, set it here.
50
- def self.set_domain_prefix(prefix)
51
- @@domain_prefix = prefix
52
- end
49
+ # If you want a domain prefix for all your models, set it here.
50
+ def self.set_domain_prefix(prefix)
51
+ @@domain_prefix = prefix
52
+ end
53
53
 
54
- # Same as set_table_name
55
- def self.set_table_name(table_name)
56
- set_domain_name table_name
57
- end
54
+ # Same as set_table_name
55
+ def self.set_table_name(table_name)
56
+ set_domain_name table_name
57
+ end
58
58
 
59
- # Sets the domain name for this class
60
- def self.set_domain_name(table_name)
61
- # puts 'setting domain name for class ' + self.inspect + '=' + table_name
62
- @domain_name_for_class = table_name
63
- super
64
- end
59
+ # Sets the domain name for this class
60
+ def self.set_domain_name(table_name)
61
+ # puts 'setting domain name for class ' + self.inspect + '=' + table_name
62
+ @domain_name_for_class = table_name
63
+ super
64
+ end
65
65
 
66
- def self.get_domain_name
67
- # puts 'returning domain_name=' + @domain_name_for_class.to_s
68
- return @domain_name_for_class
69
- end
66
+ def self.get_domain_name
67
+ # puts 'returning domain_name=' + @domain_name_for_class.to_s
68
+ return @domain_name_for_class
69
+ end
70
70
 
71
71
 
72
- def domain
73
- super # super.domain
74
- end
72
+ def domain
73
+ super # super.domain
74
+ end
75
75
 
76
- def self.domain
77
- return self.get_domain_name unless self.get_domain_name.nil?
78
- d = super
79
- domain_name_for_class = @@domain_prefix + d.to_s
80
- self.set_domain_name(domain_name_for_class)
81
- domain_name_for_class
82
- end
76
+ def self.domain
77
+ return self.get_domain_name unless self.get_domain_name.nil?
78
+ d = super
79
+ domain_name_for_class = @@domain_prefix + d.to_s
80
+ self.set_domain_name(domain_name_for_class)
81
+ domain_name_for_class
82
+ end
83
83
 
84
- #this bit of code creates a "run_blank" function for everything value in the @@callbacks array.
85
- #this function can then be inserted in the appropriate place in the save, new, destroy, etc overrides
86
- #basically, this is how we recreate the callback functions
87
- @@callbacks=["before_save", "before_create", "after_create", "before_update", "after_update", "after_save", "after_destroy"]
88
- @@callbacks.each do |callback|
89
- #we first have to make an initialized array for each of the callbacks, to prevent problems if they are not called
90
- eval %{
84
+ #this bit of code creates a "run_blank" function for everything value in the @@callbacks array.
85
+ #this function can then be inserted in the appropriate place in the save, new, destroy, etc overrides
86
+ #basically, this is how we recreate the callback functions
87
+ @@callbacks=["before_save", "before_create", "after_create", "before_update", "after_update", "after_save", "after_destroy"]
88
+ @@callbacks.each do |callback|
89
+ #we first have to make an initialized array for each of the callbacks, to prevent problems if they are not called
90
+ eval %{
91
91
  @@#{callback}_names=[]
92
92
 
93
93
  def self.#{callback}(*args)
@@ -106,402 +106,421 @@ module SimpleRecord
106
106
  return true
107
107
  end
108
108
  }
109
- end
110
-
111
- def self.has_attributes(*args)
112
- @@attributes = args
113
- args.each do |arg|
114
- # define reader method
115
- send :define_method, arg do
116
- ret = nil
117
- if self[arg.to_s].class==Array
118
- if self[arg.to_s].length==1
119
- ret = self[arg.to_s][0]
120
- else
121
- ret = self[arg.to_s]
122
- end
123
- else
124
- ret = self[arg.to_s]
125
- end
126
- return nil if ret.nil?
127
- return un_offset_if_int(arg, ret)
128
109
  end
129
110
 
130
- # define writer method
131
- method_name = (arg.to_s+"=")
132
- send(:define_method, method_name) do |value|
133
- self[arg.to_s]=value# end
111
+ def self.has_attributes(*args)
112
+ @@attributes = args
113
+ args.each do |arg|
114
+ # define reader method
115
+ send :define_method, arg do
116
+ ret = nil
117
+ if self[arg.to_s].class==Array
118
+ if self[arg.to_s].length==1
119
+ ret = self[arg.to_s][0]
120
+ else
121
+ ret = self[arg.to_s]
122
+ end
123
+ else
124
+ ret = self[arg.to_s]
125
+ end
126
+ return nil if ret.nil?
127
+ return un_offset_if_int(arg, ret)
128
+ end
129
+
130
+ # define writer method
131
+ method_name = (arg.to_s+"=")
132
+ send(:define_method, method_name) do |value|
133
+ self[arg.to_s]=value# end
134
+ end
135
+ end
134
136
  end
135
- end
136
- end
137
137
 
138
- @@ints = []
139
- def self.are_ints(*args)
140
- # puts 'calling are_ints: ' + args.inspect
141
- args.each do |arg|
142
- # todo: maybe @@ints and @@dates should be maps for quicker lookups
143
- @@ints << arg if @@ints.index(arg).nil?
144
- end
138
+ @@ints = []
139
+ def self.are_ints(*args)
140
+ # puts 'calling are_ints: ' + args.inspect
141
+ args.each do |arg|
142
+ # todo: maybe @@ints and @@dates should be maps for quicker lookups
143
+ @@ints << arg if @@ints.index(arg).nil?
144
+ end
145
145
  # @@ints = args
146
- # puts 'ints=' + @@ints.inspect
147
- end
146
+ # puts 'ints=' + @@ints.inspect
147
+ end
148
148
 
149
- @@dates = []
150
- def self.are_dates(*args)
151
- args.each do |arg|
152
- @@dates << arg if @@dates.index(arg).nil?
153
- end
149
+ @@dates = []
150
+ def self.are_dates(*args)
151
+ args.each do |arg|
152
+ @@dates << arg if @@dates.index(arg).nil?
153
+ end
154
154
  # @@dates = args
155
- # puts 'dates=' + @@dates.inspect
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
155
+ # puts 'dates=' + @@dates.inspect
156
+ end
164
157
 
165
- @@virtuals=[]
166
- def self.has_virtuals(*args)
167
- @@virtuals = args
168
- args.each do |arg|
169
- #we just create the accessor functions here, the actual instance variable is created during initialize
170
- attr_accessor(arg)
171
- end
172
- end
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
164
+
165
+ @@virtuals=[]
166
+ def self.has_virtuals(*args)
167
+ @@virtuals = args
168
+ args.each do |arg|
169
+ #we just create the accessor functions here, the actual instance variable is created during initialize
170
+ attr_accessor(arg)
171
+ end
172
+ end
173
173
 
174
- @@belongs_to_map = {}
175
- # One belongs_to association per call. Call multiple times if there are more than one.
176
- #
177
- # This method will also create an {association)_id method that will return the ID of the foreign object
178
- # without actually materializing it.
179
- def self.belongs_to(association_id, options = {})
180
- @@belongs_to_map[association_id] = options
181
- arg = association_id
182
- arg_id = arg.to_s + '_id'
183
-
184
- # todo: should also handle foreign_key http://74.125.95.132/search?q=cache:KqLkxuXiBBQJ:wiki.rubyonrails.org/rails/show/belongs_to+rails+belongs_to&hl=en&ct=clnk&cd=1&gl=us
185
- # puts "arg_id=#{arg}_id"
186
- # puts "is defined? " + eval("(defined? #{arg}_id)").to_s
187
- # puts 'atts=' + @attributes.inspect
188
-
189
- # Define reader method
190
- send(:define_method, arg) do
191
- options2 = @@belongs_to_map[arg]
192
- class_name = options2[:class_name] || arg.to_s[0...1].capitalize + arg.to_s[1...arg.to_s.length]
193
- # puts "attr=" + @attributes[arg_id].inspect
194
- # puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil?
195
- ret = nil
196
- arg_id = arg.to_s + '_id'
197
- if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != ''
198
- if !@@cache_store.nil?
199
- arg_id_val = @attributes[arg_id][0]
200
- cache_key = self.class.cache_key(class_name, arg_id_val)
174
+ @@belongs_to_map = {}
175
+ # One belongs_to association per call. Call multiple times if there are more than one.
176
+ #
177
+ # This method will also create an {association)_id method that will return the ID of the foreign object
178
+ # without actually materializing it.
179
+ def self.belongs_to(association_id, options = {})
180
+ @@belongs_to_map[association_id] = options
181
+ arg = association_id
182
+ arg_id = arg.to_s + '_id'
183
+
184
+ # todo: should also handle foreign_key http://74.125.95.132/search?q=cache:KqLkxuXiBBQJ:wiki.rubyonrails.org/rails/show/belongs_to+rails+belongs_to&hl=en&ct=clnk&cd=1&gl=us
185
+ # puts "arg_id=#{arg}_id"
186
+ # puts "is defined? " + eval("(defined? #{arg}_id)").to_s
187
+ # puts 'atts=' + @attributes.inspect
188
+
189
+ # Define reader method
190
+ send(:define_method, arg) do
191
+ options2 = @@belongs_to_map[arg]
192
+ class_name = options2[:class_name] || arg.to_s[0...1].capitalize + arg.to_s[1...arg.to_s.length]
193
+ # puts "attr=" + @attributes[arg_id].inspect
194
+ # puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil?
195
+ ret = nil
196
+ arg_id = arg.to_s + '_id'
197
+ if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != ''
198
+ if !@@cache_store.nil?
199
+ arg_id_val = @attributes[arg_id][0]
200
+ cache_key = self.class.cache_key(class_name, arg_id_val)
201
201
  # puts 'cache_key=' + cache_key
202
- ret = @@cache_store.read(cache_key)
202
+ ret = @@cache_store.read(cache_key)
203
203
  # puts 'belongs_to incache=' + ret.inspect
204
- end
205
- if ret.nil?
206
- to_eval = "#{class_name}.find(@attributes['#{arg_id}'][0], :auto_load=>true)"
204
+ end
205
+ if ret.nil?
206
+ to_eval = "#{class_name}.find(@attributes['#{arg_id}'][0], :auto_load=>true)"
207
207
  # puts 'to eval=' + to_eval
208
- begin
209
- ret = eval(to_eval) # (defined? #{arg}_id)
210
- rescue RightAws::ActiveSdb::ActiveSdbError
211
- if $!.message.include? "Couldn't find"
212
- ret = nil
213
- else
214
- raise $!
215
- end
208
+ begin
209
+ ret = eval(to_eval) # (defined? #{arg}_id)
210
+ rescue RightAws::ActiveSdb::ActiveSdbError
211
+ if $!.message.include? "Couldn't find"
212
+ ret = nil
213
+ else
214
+ raise $!
215
+ end
216
+ end
217
+
218
+ end
219
+ end
220
+ # puts 'ret=' + ret.inspect
221
+ return ret
216
222
  end
217
223
 
218
- end
219
- end
220
- # puts 'ret=' + ret.inspect
221
- return ret
222
- end
224
+ # Define reader ID method
225
+ send(:define_method, arg_id) do
226
+ if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != ''
227
+ return @attributes[arg_id][0]
228
+ end
229
+ return nil
230
+ end
223
231
 
224
- # Define reader ID method
225
- send(:define_method, arg_id) do
226
- if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != ''
227
- return @attributes[arg_id][0]
228
- end
229
- return nil
230
- end
232
+ # Define writer method
233
+ send(:define_method, arg.to_s + "=") do |value|
234
+ arg_id = arg.to_s + '_id'
235
+ if value.nil?
236
+ self[arg_id]=nil unless self[arg_id].nil? # if it went from something to nil, then we have to remember and remove attribute on save
237
+ else
238
+ self[arg_id]=value.id
239
+ end
240
+ end
231
241
 
232
- # Define writer method
233
- send(:define_method, arg.to_s + "=") do |value|
234
- arg_id = arg.to_s + '_id'
235
- if value.nil?
236
- self[arg_id]=nil unless self[arg_id].nil? # if it went from something to nil, then we have to remember and remove attribute on save
237
- else
238
- self[arg_id]=value.id
242
+ send(:define_method, "create_"+arg.to_s) do |*params|
243
+ newsubrecord=eval(arg.to_s.classify).new(*params)
244
+ newsubrecord.save
245
+ arg_id = arg.to_s + '_id'
246
+ self[arg_id]=newsubrecord.id
247
+ end
239
248
  end
240
- end
241
-
242
- send(:define_method, "create_"+arg.to_s) do |*params|
243
- newsubrecord=eval(arg.to_s.classify).new(*params)
244
- newsubrecord.save
245
- arg_id = arg.to_s + '_id'
246
- self[arg_id]=newsubrecord.id
247
- end
248
- end
249
249
 
250
- def self.has_many(*args)
251
- args.each do |arg|
252
- #okay, this creates an instance method with the pluralized name of the symbol passed to belongs_to
253
- send(:define_method, arg) do
254
- #when called, the method creates a new, very temporary instance of the Activerecordtosdb_subrecord class
255
- #It is passed the three initializers it needs:
256
- #note the first parameter is just a string by time new gets it, like "user"
257
- #the second and third parameters are still a variable when new gets it, like user_id
258
- return eval(%{Activerecordtosdb_subrecord_array.new('#{arg}', self.class.name ,id)})
250
+ def self.has_many(*args)
251
+ args.each do |arg|
252
+ #okay, this creates an instance method with the pluralized name of the symbol passed to belongs_to
253
+ send(:define_method, arg) do
254
+ #when called, the method creates a new, very temporary instance of the Activerecordtosdb_subrecord class
255
+ #It is passed the three initializers it needs:
256
+ #note the first parameter is just a string by time new gets it, like "user"
257
+ #the second and third parameters are still a variable when new gets it, like user_id
258
+ return eval(%{Activerecordtosdb_subrecord_array.new('#{arg}', self.class.name ,id)})
259
+ end
260
+ end
261
+ #Disclaimer: this whole funciton just seems crazy to me, and a bit inefficient. But it was the clearest way I could think to do it code wise.
262
+ #It's bad programming form (imo) to have a class method require something that isn't passed to it through it's variables.
263
+ #I couldn't pass the id when calling find, since the original find doesn't work that way, so I was left with this.
259
264
  end
260
- end
261
- #Disclaimer: this whole funciton just seems crazy to me, and a bit inefficient. But it was the clearest way I could think to do it code wise.
262
- #It's bad programming form (imo) to have a class method require something that isn't passed to it through it's variables.
263
- #I couldn't pass the id when calling find, since the original find doesn't work that way, so I was left with this.
264
- end
265
265
 
266
- def self.has_one(*args)
266
+ def self.has_one(*args)
267
267
 
268
- end
268
+ end
269
269
 
270
- has_attributes :created, :updated
271
- before_create :set_created, :set_updated
272
- before_update :set_updated
273
- are_dates :created, :updated
270
+ has_attributes :created, :updated
271
+ before_create :set_created, :set_updated
272
+ before_update :set_updated
273
+ are_dates :created, :updated
274
274
 
275
- def set_created
275
+ def set_created
276
276
  # puts 'SETTING CREATED'
277
- # @created = DateTime.now
278
- self[:created] = DateTime.now
277
+ # @created = DateTime.now
278
+ self[:created] = DateTime.now
279
279
  # @tester = 'some test value'
280
- # self[:tester] = 'some test value'
281
- end
280
+ # self[:tester] = 'some test value'
281
+ end
282
282
 
283
- def set_updated
283
+ def set_updated
284
284
  # puts 'SETTING UPDATED'
285
- # @updated = DateTime.now
286
- self[:updated] = DateTime.now
285
+ # @updated = DateTime.now
286
+ self[:updated] = DateTime.now
287
287
  # @tester = 'some test value updated'
288
- end
289
-
290
- def initialize(*params)
291
- if params[0]
292
- #we have to handle the virtuals. Right now, this assumes that all parameters are passed from inside an array
293
- #this is the usually the case when the parameters are passed passed via POST and obtained from the params array
294
- @@virtuals.each do |virtual|
295
- #we first copy the information for the virtual to an instance variable of the same name
296
- eval("@#{virtual}=params[0]['#{virtual}']")
297
- #and then remove the parameter before it is passed to initialize, so that it is NOT sent to SimpleDB
298
- eval("params[0].delete('#{virtual}')")
299
- end
300
- super(*params)
301
- else
302
- super()
303
- end
304
- @errors=SimpleRecord_errors.new
305
- end
288
+ end
306
289
 
290
+ def initialize(*params)
291
+ if params[0]
292
+ #we have to handle the virtuals. Right now, this assumes that all parameters are passed from inside an array
293
+ #this is the usually the case when the parameters are passed passed via POST and obtained from the params array
294
+ @@virtuals.each do |virtual|
295
+ #we first copy the information for the virtual to an instance variable of the same name
296
+ eval("@#{virtual}=params[0]['#{virtual}']")
297
+ #and then remove the parameter before it is passed to initialize, so that it is NOT sent to SimpleDB
298
+ eval("params[0].delete('#{virtual}')")
299
+ end
300
+ super(*params)
301
+ else
302
+ super()
303
+ end
304
+ @errors=SimpleRecord_errors.new
305
+ end
307
306
 
308
- @@offset = 9223372036854775808
309
- @@padding = 20
310
-
311
- def self.pad_and_offset(x)
312
- # todo: add Float, etc
313
- if x.kind_of? Integer
314
- x += @@offset
315
- x_str = x.to_s
316
- # pad
317
- x_str = '0' + x_str while x_str.size < 20
318
- return x_str
319
- # elsif x.kind_of? Date
320
- # Does RigthAWS convert dates already?
321
- else
322
- return x
323
- end
324
- end
325
307
 
326
- def self.unpad_and_offset(x)
327
- # todo: add Float, etc
328
- if x.kind_of? Integer
329
- x += @@offset
330
- x_str = x.to_s
331
- # pad
332
- x_str = '0' + x_str while x_str.size < 20
333
- return x_str
334
- else
335
- return x
336
- end
337
- end
308
+ @@offset = 9223372036854775808
309
+ @@padding = 20
310
+
311
+ def self.pad_and_offset(x)
312
+ # todo: add Float, etc
313
+ # puts 'padding=' + x.class.name + " -- " + x.inspect
314
+ if x.kind_of? Integer
315
+ x += @@offset
316
+ x_str = x.to_s
317
+ # pad
318
+ x_str = '0' + x_str while x_str.size < 20
319
+ return x_str
320
+ elsif x.respond_to?(:iso8601)
321
+ # puts x.class.name + ' responds to iso8601'
322
+ if x.is_a? DateTime
323
+ x_str = x.new_offset(0).iso8601
324
+ else
325
+ x_str = x.getutc.iso8601
326
+ end
327
+ # puts 'utc=' + x_str
328
+ return x_str
329
+ else
330
+ return x
331
+ end
332
+ end
338
333
 
339
- def domain_ok(ex)
340
- if (ex.message().index("NoSuchDomain") != nil)
341
- puts "Creating new SimpleDB Domain: " + domain
342
- self.class.create_domain
343
- return true
344
- end
345
- return false
346
- end
334
+ def domain_ok(ex)
335
+ if (ex.message().index("NoSuchDomain") != nil)
336
+ puts "Creating new SimpleDB Domain: " + domain
337
+ self.class.create_domain
338
+ return true
339
+ end
340
+ return false
341
+ end
347
342
 
348
- @create_domain_called = false
349
-
350
- def save(*params)
351
- # puts 'SAVING: ' + self.inspect
352
- is_create = self[:id].nil?
353
- ok = pre_save(*params)
354
- if ok
355
- begin
356
- # puts 'is frozen? ' + self.frozen?.to_s + ' - ' + self.inspect
357
- to_delete = get_atts_to_delete
358
- if super(*params)
343
+ @create_domain_called = false
344
+
345
+ def save(*params)
346
+ # puts 'SAVING: ' + self.inspect
347
+ is_create = self[:id].nil?
348
+ ok = pre_save(*params)
349
+ if ok
350
+ begin
351
+ # puts 'is frozen? ' + self.frozen?.to_s + ' - ' + self.inspect
352
+ to_delete = get_atts_to_delete
353
+ if super(*params)
359
354
  # puts 'SAVED super'
360
- self.class.cache_results(self)
361
- delete_niled(to_delete)
362
- if run_after_save && is_create ? run_after_create : run_after_update
363
- return true
355
+ self.class.cache_results(self)
356
+ delete_niled(to_delete)
357
+ if run_after_save && is_create ? run_after_create : run_after_update
358
+ return true
359
+ else
360
+ #I thought about calling destroy here, but rails doesn't behave that way, so neither will I
361
+ return false
362
+ end
363
+ else
364
+ return false
365
+ end
366
+ rescue RightAws::AwsError
367
+ # puts "RESCUED in save: " + $!
368
+ if (domain_ok($!))
369
+ if !@create_domain_called
370
+ @create_domain_called = true
371
+ save(*params)
372
+ else
373
+ raise $!
374
+ end
375
+ else
376
+ raise $!
377
+ end
378
+ end
364
379
  else
365
- #I thought about calling destroy here, but rails doesn't behave that way, so neither will I
366
- return false
380
+ #@debug = "not saved"
381
+ return false
367
382
  end
368
- else
369
- return false
370
- end
371
- rescue RightAws::AwsError
372
- # puts "RESCUED in save: " + $!
373
- if (domain_ok($!))
374
- if !@create_domain_called
375
- @create_domain_called = true
376
- save(*params)
377
- else
378
- raise $!
383
+ end
384
+
385
+ def pad_and_offset_ints_to_sdb()
386
+ if !@@ints.nil?
387
+ for i in @@ints
388
+ # puts 'int encoding: ' + i.to_s
389
+ if !self[i.to_s].nil?
390
+ # puts 'before: ' + self[i.to_s].inspect
391
+ # puts @attributes.inspect
392
+ # puts @attributes[i.to_s].inspect
393
+ arr = @attributes[i.to_s]
394
+ arr.collect!{ |x| self.class.pad_and_offset(x) }
395
+ @attributes[i.to_s] = arr
396
+ # puts 'after: ' + @attributes[i.to_s].inspect
397
+ else
398
+ # puts 'was nil'
399
+ end
400
+ end
401
+ end
402
+ end
403
+ def convert_dates_to_sdb()
404
+ if !@@dates.nil?
405
+ for i in @@dates
406
+ # puts 'int encoding: ' + i.to_s
407
+ if !self[i.to_s].nil?
408
+ # puts 'before: ' + self[i.to_s].inspect
409
+ # puts @attributes.inspect
410
+ # puts @attributes[i.to_s].inspect
411
+ arr = @attributes[i.to_s]
412
+ puts 'padding date=' + i.to_s
413
+ arr.collect!{ |x| self.class.pad_and_offset(x) }
414
+ @attributes[i.to_s] = arr
415
+ # puts 'after: ' + @attributes[i.to_s].inspect
416
+ else
417
+ # puts 'was nil'
418
+ end
419
+ end
379
420
  end
380
- else
381
- raise $!
382
- end
383
421
  end
384
- else
385
- #@debug = "not saved"
386
- return false
387
- end
388
- end
389
422
 
390
- def pre_save(*params)
391
- if respond_to?('validate')
392
- validate
423
+ def pre_save(*params)
424
+ if respond_to?('validate')
425
+ validate
393
426
  # puts 'AFTER VALIDATIONS, ERRORS=' + errors.inspect
394
- if (!@errors.nil? && @errors.length > 0 )
427
+ if (!@errors.nil? && @errors.length > 0 )
395
428
  # puts 'THERE ARE ERRORS, returning false'
396
- return false
397
- end
398
- end
429
+ return false
430
+ end
431
+ end
399
432
 
400
- is_create = self[:id].nil?
401
- ok = respond_to?('before_save') ? before_save : true
402
- if ok
403
- if is_create && respond_to?('before_create')
404
- ok = before_create
405
- elsif !is_create && respond_to?('before_update')
406
- ok = before_update
407
- end
408
- end
409
- if ok
410
- ok = run_before_save && is_create ? run_before_create : run_before_update
411
- end
412
- if ok
433
+ is_create = self[:id].nil?
434
+ ok = respond_to?('before_save') ? before_save : true
435
+ if ok
436
+ if is_create && respond_to?('before_create')
437
+ ok = before_create
438
+ elsif !is_create && respond_to?('before_update')
439
+ ok = before_update
440
+ end
441
+ end
442
+ if ok
443
+ ok = run_before_save && is_create ? run_before_create : run_before_update
444
+ end
445
+ if ok
413
446
  # puts 'ABOUT TO SAVE: ' + self.inspect
414
- # First we gotta pad and offset
415
- if !@@ints.nil?
416
- for i in @@ints
417
- # puts 'int encoding: ' + i.to_s
418
- if !self[i.to_s].nil?
419
- # puts 'before: ' + self[i.to_s].inspect
420
- # puts @attributes.inspect
421
- # puts @attributes[i.to_s].inspect
422
- arr = @attributes[i.to_s]
423
- arr.collect!{ |x| self.class.pad_and_offset(x) }
424
- @attributes[i.to_s] = arr
425
- # puts 'after: ' + @attributes[i.to_s].inspect
426
- else
427
- # puts 'was nil'
447
+ # First we gotta pad and offset
448
+ pad_and_offset_ints_to_sdb()
449
+ convert_dates_to_sdb()
428
450
  end
429
- end
451
+ ok
430
452
  end
431
- end
432
- ok
433
- end
434
453
 
435
- def save_attributes(*params)
436
- ret = super(*params)
437
- if ret
438
- self.class.cache_results(self)
439
- end
440
- ret
441
- end
454
+ def save_attributes(*params)
455
+ ret = super(*params)
456
+ if ret
457
+ self.class.cache_results(self)
458
+ end
459
+ ret
460
+ end
442
461
 
443
- def get_atts_to_delete
444
- to_delete = []
445
- @attributes.each do |key, value|
446
- # puts 'value=' + value.inspect
447
- if value.nil? || (value.is_a?(Array) && value.size == 0)
448
- to_delete << key
462
+ def get_atts_to_delete
463
+ to_delete = []
464
+ @attributes.each do |key, value|
465
+ # puts 'value=' + value.inspect
466
+ if value.nil? || (value.is_a?(Array) && value.size == 0)
467
+ to_delete << key
468
+ end
469
+ end
470
+ return to_delete
449
471
  end
450
- end
451
- return to_delete
452
- end
453
472
 
454
- # Run pre_save on each object, then runs batch_put_attributes
455
- # Returns
456
- def self.batch_save(objects)
457
- results = []
458
- to_save = []
459
- if objects && objects.size > 0
460
- objects.each do |o|
461
- ok = o.pre_save
462
- raise "Pre save failed on object with id = " + o.id if !ok
463
- results << ok
464
- next if !ok
465
- o.pre_save2
466
- to_save << RightAws::SdbInterface::Item.new(o.id, o.attributes, true)
473
+ # Run pre_save on each object, then runs batch_put_attributes
474
+ # Returns
475
+ def self.batch_save(objects)
476
+ results = []
477
+ to_save = []
478
+ if objects && objects.size > 0
479
+ objects.each do |o|
480
+ ok = o.pre_save
481
+ raise "Pre save failed on object with id = " + o.id if !ok
482
+ results << ok
483
+ next if !ok
484
+ o.pre_save2
485
+ to_save << RightAws::SdbInterface::Item.new(o.id, o.attributes, true)
486
+ end
487
+ end
488
+ connection.batch_put_attributes(domain, to_save)
489
+ results
467
490
  end
468
- end
469
- connection.batch_put_attributes(domain, to_save)
470
- results
471
- end
472
491
 
473
- #
474
- # Usage: ClassName.delete id
475
- # todo: move to RightAWS
476
- #
477
- def self.delete(id)
478
- connection.delete_attributes(domain, id)
479
- end
492
+ #
493
+ # Usage: ClassName.delete id
494
+ # todo: move to RightAWS
495
+ #
496
+ def self.delete(id)
497
+ connection.delete_attributes(domain, id)
498
+ end
480
499
 
481
- def delete_niled(to_delete)
482
- if to_delete.size > 0
500
+ def delete_niled(to_delete)
501
+ if to_delete.size > 0
483
502
  # puts 'Deleting attributes=' + to_delete.inspect
484
- delete_attributes to_delete
485
- end
486
- end
503
+ delete_attributes to_delete
504
+ end
505
+ end
487
506
 
488
- def un_offset_if_int(arg, x)
489
- if !@@ints.nil?
490
- for i in @@ints
507
+ def un_offset_if_int(arg, x)
508
+ if !@@ints.nil?
509
+ for i in @@ints
491
510
  # puts 'unpadding: ' + i.to_s
492
- # unpad and unoffset
493
- if i == arg
511
+ # unpad and unoffset
512
+ if i == arg
494
513
  # puts 'unoffsetting ' + x.to_s
495
- x = un_offset_int(x)
496
- end
497
- end
498
- end
499
- if !@@dates.nil?
500
- for d in @@dates
514
+ x = un_offset_int(x)
515
+ end
516
+ end
517
+ end
518
+ if !@@dates.nil?
519
+ for d in @@dates
501
520
  # puts 'converting created: ' + self['created'].inspect
502
- if d == arg
503
- x = to_date(x)
504
- end
521
+ if d == arg
522
+ x = to_date(x)
523
+ end
505
524
  # if !self[d].nil?
506
525
  # self[d].collect!{ |d2|
507
526
  # if d2.is_a?(String)
@@ -512,14 +531,14 @@ module SimpleRecord
512
531
  # }
513
532
  # end
514
533
  # puts 'after=' + self['created'].inspect
515
- end
516
- end
517
- if !@@booleans.nil?
518
- for b in @@booleans
534
+ end
535
+ end
536
+ if !@@booleans.nil?
537
+ for b in @@booleans
519
538
  # puts 'converting created: ' + self['created'].inspect
520
- if b == arg
521
- x = to_bool(x)
522
- end
539
+ if b == arg
540
+ x = to_bool(x)
541
+ end
523
542
  # if !self[d].nil?
524
543
  # self[d].collect!{ |d2|
525
544
  # if d2.is_a?(String)
@@ -530,72 +549,72 @@ module SimpleRecord
530
549
  # }
531
550
  # end
532
551
  # puts 'after=' + self['created'].inspect
552
+ end
553
+ end
554
+ x
533
555
  end
534
- end
535
- x
536
- end
537
-
538
-
539
- def to_date(x)
540
- if x.is_a?(String)
541
- DateTime.parse(x)
542
- else
543
- x
544
- end
545
556
 
546
- end
547
-
548
- def to_bool(x)
549
- if x.is_a?(String)
550
- x == "true"
551
- else
552
- x
553
- end
554
- end
557
+
558
+ def to_date(x)
559
+ if x.is_a?(String)
560
+ DateTime.parse(x)
561
+ else
562
+ x
563
+ end
564
+
565
+ end
566
+
567
+ def to_bool(x)
568
+ if x.is_a?(String)
569
+ x == "true"
570
+ else
571
+ x
572
+ end
573
+ end
555
574
 
556
575
 
557
- def un_offset_int(x)
558
- if x.is_a?(String)
559
- x2 = x.to_i
576
+ def un_offset_int(x)
577
+ if x.is_a?(String)
578
+ x2 = x.to_i
560
579
  # puts 'to_i=' + x2.to_s
561
- x2 -= @@offset
580
+ x2 -= @@offset
562
581
  # puts 'after subtracting offset='+ x2.to_s
563
- x2
564
- else
565
- x
566
- end
567
- end
582
+ x2
583
+ else
584
+ x
585
+ end
586
+ end
568
587
 
569
- def unpad(i, attributes)
570
- if !attributes[i].nil?
588
+ def unpad(i, attributes)
589
+ if !attributes[i].nil?
571
590
  # puts 'before=' + self[i].inspect
572
- attributes[i].collect!{ |x|
573
- un_offset_int(x)
591
+ attributes[i].collect!{ |x|
592
+ un_offset_int(x)
574
593
 
575
- }
594
+ }
576
595
  # for x in self[i]
577
- # x = self[i][0].to_i
578
- # x -= @@offset
579
- # self[i] = x
580
- # end
581
- end
582
- end
596
+ # x = self[i][0].to_i
597
+ # x -= @@offset
598
+ # self[i] = x
599
+ # end
600
+ end
601
+ end
583
602
 
584
- def unpad_self
585
- if !@@ints.nil?
586
- for i in @@ints
603
+ def unpad_self
604
+ if !@@ints.nil?
605
+ for i in @@ints
587
606
  # puts 'unpadding: ' + i.to_s
588
- # unpad and unoffset
607
+ # unpad and unoffset
589
608
 
590
- unpad(i, @attributes)
609
+ unpad(i, @attributes)
610
+ end
611
+ end
591
612
  end
592
- end
593
- end
594
613
 
595
- def reload
596
- super()
614
+ def reload
615
+ super()
597
616
  # puts 'decoding...'
598
-
617
+
599
618
  =begin
600
619
  This is done on getters now
601
620
  if !@@dates.nil?
@@ -616,263 +635,263 @@ This is done on getters now
616
635
  =end
617
636
 
618
637
  # unpad_self
619
- end
638
+ end
620
639
 
621
- def update_attributes(*params)
622
- return save_attributes(*params)
623
- end
640
+ def update_attributes(*params)
641
+ return save_attributes(*params)
642
+ end
624
643
 
625
- def destroy(*params)
626
- if super(*params)
627
- if run_after_destroy
628
- return true
629
- else
630
- return false
644
+ def destroy(*params)
645
+ if super(*params)
646
+ if run_after_destroy
647
+ return true
648
+ else
649
+ return false
650
+ end
651
+ else
652
+ return false
653
+ end
631
654
  end
632
- else
633
- return false
634
- end
635
- end
636
655
 
637
- def self.quote_regexp(a, re)
638
- a =~ re
639
- #was there a match?
640
- if $&
641
- before=$`
642
- middle=$&
643
- after=$'
644
-
645
- before =~ /'$/ #is there already a quote immediately before the match?
646
- unless $&
647
- return "#{before}'#{middle}'#{quote_regexp(after, re)}" #if not, put quotes around the match
648
- else
649
- return "#{before}#{middle}#{quote_regexp(after, re)}" #if so, assume it is quoted already and move on
650
- end
651
- else
652
- #no match, just return the string
653
- return a
654
- end
655
- end
656
+ def self.quote_regexp(a, re)
657
+ a =~ re
658
+ #was there a match?
659
+ if $&
660
+ before=$`
661
+ middle=$&
662
+ after=$'
663
+
664
+ before =~ /'$/ #is there already a quote immediately before the match?
665
+ unless $&
666
+ return "#{before}'#{middle}'#{quote_regexp(after, re)}" #if not, put quotes around the match
667
+ else
668
+ return "#{before}#{middle}#{quote_regexp(after, re)}" #if so, assume it is quoted already and move on
669
+ end
670
+ else
671
+ #no match, just return the string
672
+ return a
673
+ end
674
+ end
656
675
 
657
- def self.find(*params)
658
- reload=true
659
- first=false
660
- all=false
661
- select=false
662
- select_attributes=[]
676
+ def self.find(*params)
677
+ reload=true
678
+ first=false
679
+ all=false
680
+ select=false
681
+ select_attributes=[]
663
682
 
664
- if params.size > 0
665
- all = params[0] == :all
666
- first = params[0] == :first
667
- end
683
+ if params.size > 0
684
+ all = params[0] == :all
685
+ first = params[0] == :first
686
+ end
668
687
 
669
688
 
670
- # Pad and Offset number attributes
671
- options = params[1]
689
+ # Pad and Offset number attributes
690
+ options = params[1]
672
691
  # puts 'options=' + options.inspect
673
- convert_condition_params(options)
674
-
692
+ convert_condition_params(options)
693
+
675
694
  # puts 'after collect=' + params.inspect
676
695
 
677
- results = all ? [] : nil
678
- begin
679
- results=super(*params)
680
- cache_results(results)
681
- rescue RightAws::AwsError, RightAws::ActiveSdb::ActiveSdbError
682
- puts "RESCUED: " + $!
683
- if ($!.message().index("NoSuchDomain") != nil)
684
- # this is ok
685
- elsif ($!.message() =~ @@regex_no_id)
686
- results = nil
687
- else
688
- raise $!
696
+ results = all ? [] : nil
697
+ begin
698
+ results=super(*params)
699
+ cache_results(results)
700
+ rescue RightAws::AwsError, RightAws::ActiveSdb::ActiveSdbError
701
+ puts "RESCUED: " + $!
702
+ if ($!.message().index("NoSuchDomain") != nil)
703
+ # this is ok
704
+ elsif ($!.message() =~ @@regex_no_id)
705
+ results = nil
706
+ else
707
+ raise $!
708
+ end
709
+ end
710
+ return results
689
711
  end
690
- end
691
- return results
692
- end
693
712
 
694
- @@regex_no_id = /.*Couldn't find.*with ID.*/
695
- def self.select(*params)
696
- first=false
697
- all=false
698
- select=false
699
- select_attributes=[]
713
+ @@regex_no_id = /.*Couldn't find.*with ID.*/
714
+ def self.select(*params)
715
+ first=false
716
+ all=false
717
+ select=false
718
+ select_attributes=[]
700
719
 
701
- if params.size > 0
702
- all = params[0] == :all
703
- first = params[0] == :first
704
- end
720
+ if params.size > 0
721
+ all = params[0] == :all
722
+ first = params[0] == :first
723
+ end
724
+
725
+ options = params[1]
726
+ convert_condition_params(options)
727
+
728
+ results = all ? [] : nil
729
+ begin
730
+ results=super(*params)
731
+ cache_results(results)
732
+ rescue RightAws::AwsError, RightAws::ActiveSdb::ActiveSdbError
733
+ if ($!.message().index("NoSuchDomain") != nil)
734
+ # this is ok
735
+ elsif ($!.message() =~ @@regex_no_id)
736
+ results = nil
737
+ else
738
+ raise $!
739
+ end
740
+ end
741
+ return results
705
742
 
706
- options = params[1]
707
- convert_condition_params(options)
708
-
709
- results = all ? [] : nil
710
- begin
711
- results=super(*params)
712
- cache_results(results)
713
- rescue RightAws::AwsError, RightAws::ActiveSdb::ActiveSdbError
714
- if ($!.message().index("NoSuchDomain") != nil)
715
- # this is ok
716
- elsif ($!.message() =~ @@regex_no_id)
717
- results = nil
718
- else
719
- raise $!
720
743
  end
721
- end
722
- return results
723
744
 
724
- end
725
-
726
- def self.convert_condition_params(options)
727
- if !options.nil? && options.size > 0
728
- conditions = options[:conditions]
729
- if !conditions.nil? && conditions.size > 1
730
- # all after first are values
731
- conditions.collect! { |x|
732
- self.pad_and_offset(x)
733
- }
745
+ def self.convert_condition_params(options)
746
+ if !options.nil? && options.size > 0
747
+ conditions = options[:conditions]
748
+ if !conditions.nil? && conditions.size > 1
749
+ # all after first are values
750
+ conditions.collect! { |x|
751
+ self.pad_and_offset(x)
752
+ }
753
+ end
754
+ end
734
755
  end
735
- end
736
- end
737
-
738
- def self.cache_results(results)
739
- if !@@cache_store.nil? && !results.nil?
740
- if results.is_a?(Array)
741
- # todo: cache each result
742
- else
743
- class_name = results.class.name
744
- id = results.id
745
- cache_key = self.cache_key(class_name, id)
756
+
757
+ def self.cache_results(results)
758
+ if !@@cache_store.nil? && !results.nil?
759
+ if results.is_a?(Array)
760
+ # todo: cache each result
761
+ else
762
+ class_name = results.class.name
763
+ id = results.id
764
+ cache_key = self.cache_key(class_name, id)
746
765
  # puts 'caching result at ' + cache_key + ': ' + results.inspect
747
- @@cache_store.write(cache_key, results, :expires_in =>10*60)
766
+ @@cache_store.write(cache_key, results, :expires_in =>10*60)
767
+ end
768
+ end
748
769
  end
749
- end
750
- end
751
-
752
- def self.cache_key(class_name, id)
753
- return class_name + "/" + id.to_s
754
- end
755
770
 
771
+ def self.cache_key(class_name, id)
772
+ return class_name + "/" + id.to_s
773
+ end
756
774
 
757
775
 
758
- @@debug=""
759
- def self.debug
760
- @@debug
761
- end
762
776
 
763
- def self.sanitize_sql(*params)
764
- return ActiveRecord::Base.sanitize_sql(*params)
765
- end
777
+ @@debug=""
778
+ def self.debug
779
+ @@debug
780
+ end
766
781
 
767
- def self.table_name
768
- return @@domain_prefix + self.class.name.tableize
769
- end
782
+ def self.sanitize_sql(*params)
783
+ return ActiveRecord::Base.sanitize_sql(*params)
784
+ end
770
785
 
771
- end
786
+ def self.table_name
787
+ return @@domain_prefix + self.class.name.tableize
788
+ end
772
789
 
773
- class SimpleRecord_errors
774
- def initialize(*params)
775
- super(*params)
776
- @errors=[]
777
790
  end
778
791
 
779
- def add_to_base(value)
780
- @errors+=[value]
781
- end
792
+ class SimpleRecord_errors
793
+ def initialize(*params)
794
+ super(*params)
795
+ @errors=[]
796
+ end
782
797
 
783
- def add(attribute, value)
784
- @errors+=["#{attribute.to_s} #{value}"]
785
- end
798
+ def add_to_base(value)
799
+ @errors+=[value]
800
+ end
786
801
 
787
- def count
788
- return length
789
- end
802
+ def add(attribute, value)
803
+ @errors+=["#{attribute.to_s} #{value}"]
804
+ end
790
805
 
791
- def length
792
- return @errors.length
793
- end
806
+ def count
807
+ return length
808
+ end
794
809
 
795
- def size
796
- return length
797
- end
810
+ def length
811
+ return @errors.length
812
+ end
798
813
 
799
- def full_messages
800
- return @errors
801
- end
802
- end
814
+ def size
815
+ return length
816
+ end
803
817
 
804
- class Activerecordtosdb_subrecord_array
805
- def initialize(subname, referencename, referencevalue)
806
- @subname=subname.classify
807
- @referencename=referencename.tableize.singularize + "_id"
808
- @referencevalue=referencevalue
818
+ def full_messages
819
+ return @errors
820
+ end
809
821
  end
810
822
 
811
- # Performance optimization if you know the array should be empty
823
+ class Activerecordtosdb_subrecord_array
824
+ def initialize(subname, referencename, referencevalue)
825
+ @subname=subname.classify
826
+ @referencename=referencename.tableize.singularize + "_id"
827
+ @referencevalue=referencevalue
828
+ end
812
829
 
813
- def init_empty
814
- @records = []
815
- end
830
+ # Performance optimization if you know the array should be empty
816
831
 
817
- def load
818
- if @records.nil?
819
- @records = find_all
820
- end
821
- return @records
822
- end
832
+ def init_empty
833
+ @records = []
834
+ end
823
835
 
824
- def [](key)
825
- return load[key]
826
- end
836
+ def load
837
+ if @records.nil?
838
+ @records = find_all
839
+ end
840
+ return @records
841
+ end
827
842
 
828
- def <<(ob)
829
- return load << ob
830
- end
843
+ def [](key)
844
+ return load[key]
845
+ end
831
846
 
832
- def each(*params, &block)
833
- return load.each(*params){|record| block.call(record)}
834
- end
847
+ def <<(ob)
848
+ return load << ob
849
+ end
835
850
 
836
- def find_all(*params)
837
- find(:all, *params)
838
- end
851
+ def each(*params, &block)
852
+ return load.each(*params){|record| block.call(record)}
853
+ end
839
854
 
840
- def build(*params)
841
- params[0][@referencename]=@referencevalue
842
- eval(@subname).new(*params)
843
- end
855
+ def find_all(*params)
856
+ find(:all, *params)
857
+ end
844
858
 
845
- def create(*params)
846
- params[0][@referencename]=@referencevalue
847
- record = eval(@subname).new(*params)
848
- record.save
849
- end
859
+ def build(*params)
860
+ params[0][@referencename]=@referencevalue
861
+ eval(@subname).new(*params)
862
+ end
850
863
 
851
- def find(*params)
852
- query=[:first, {}]
853
- #{:conditions=>"id=>1"}
854
- if params[0]
855
- if params[0]==:all
856
- query[0]=:all
864
+ def create(*params)
865
+ params[0][@referencename]=@referencevalue
866
+ record = eval(@subname).new(*params)
867
+ record.save
857
868
  end
858
- end
859
869
 
860
- if params[1]
861
- query[1]=params[1]
862
- if query[1][:conditions]
863
- query[1][:conditions]=SimpleRecord::Base.sanitize_sql(query[1][:conditions])+" AND "+ SimpleRecord::Base.sanitize_sql(["#{@referencename} = ?", @referencevalue])
864
- #query[1][:conditions]=Activerecordtosdb.sanitize_sql(query[1][:conditions])+" AND id='#{@id}'"
865
- else
866
- query[1][:conditions]=["#{@referencename} = ?", @referencevalue]
867
- #query[1][:conditions]="id='#{@id}'"
868
- end
869
- else
870
- query[1][:conditions]=["#{@referencename} = ?", @referencevalue]
871
- #query[1][:conditions]="id='#{@id}'"
872
- end
870
+ def find(*params)
871
+ query=[:first, {}]
872
+ #{:conditions=>"id=>1"}
873
+ if params[0]
874
+ if params[0]==:all
875
+ query[0]=:all
876
+ end
877
+ end
873
878
 
874
- return eval(@subname).find(*query)
875
- end
879
+ if params[1]
880
+ query[1]=params[1]
881
+ if query[1][:conditions]
882
+ query[1][:conditions]=SimpleRecord::Base.sanitize_sql(query[1][:conditions])+" AND "+ SimpleRecord::Base.sanitize_sql(["#{@referencename} = ?", @referencevalue])
883
+ #query[1][:conditions]=Activerecordtosdb.sanitize_sql(query[1][:conditions])+" AND id='#{@id}'"
884
+ else
885
+ query[1][:conditions]=["#{@referencename} = ?", @referencevalue]
886
+ #query[1][:conditions]="id='#{@id}'"
887
+ end
888
+ else
889
+ query[1][:conditions]=["#{@referencename} = ?", @referencevalue]
890
+ #query[1][:conditions]="id='#{@id}'"
891
+ end
876
892
 
877
- end
893
+ return eval(@subname).find(*query)
894
+ end
895
+
896
+ end
878
897
  end