simple_record 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/simple_record.rb +673 -654
- 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
|
-
|
32
|
+
VERSION = '1.0.7'
|
33
33
|
|
34
|
-
|
34
|
+
class Base < RightAws::ActiveSdb::Base
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
attr_accessor :errors
|
37
|
+
@@domain_prefix = ''
|
38
|
+
@domain_name_for_class = nil
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
72
|
+
def domain
|
73
|
+
super # super.domain
|
74
|
+
end
|
75
75
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
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
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
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
|
-
|
147
|
-
|
146
|
+
# puts 'ints=' + @@ints.inspect
|
147
|
+
end
|
148
148
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
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
|
-
|
156
|
-
|
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
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
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
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
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
|
-
|
202
|
+
ret = @@cache_store.read(cache_key)
|
203
203
|
# puts 'belongs_to incache=' + ret.inspect
|
204
|
-
|
205
|
-
|
206
|
-
|
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
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
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
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
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
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
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
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
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
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
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
|
-
|
266
|
+
def self.has_one(*args)
|
267
267
|
|
268
|
-
|
268
|
+
end
|
269
269
|
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
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
|
-
|
275
|
+
def set_created
|
276
276
|
# puts 'SETTING CREATED'
|
277
|
-
|
278
|
-
|
277
|
+
# @created = DateTime.now
|
278
|
+
self[:created] = DateTime.now
|
279
279
|
# @tester = 'some test value'
|
280
|
-
|
281
|
-
|
280
|
+
# self[:tester] = 'some test value'
|
281
|
+
end
|
282
282
|
|
283
|
-
|
283
|
+
def set_updated
|
284
284
|
# puts 'SETTING UPDATED'
|
285
|
-
|
286
|
-
|
285
|
+
# @updated = DateTime.now
|
286
|
+
self[:updated] = DateTime.now
|
287
287
|
# @tester = 'some test value updated'
|
288
|
-
|
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
|
-
|
327
|
-
|
328
|
-
|
329
|
-
x
|
330
|
-
|
331
|
-
#
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
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
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
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
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
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
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
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
|
-
|
366
|
-
|
380
|
+
#@debug = "not saved"
|
381
|
+
return false
|
367
382
|
end
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
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
|
-
|
391
|
-
|
392
|
-
|
423
|
+
def pre_save(*params)
|
424
|
+
if respond_to?('validate')
|
425
|
+
validate
|
393
426
|
# puts 'AFTER VALIDATIONS, ERRORS=' + errors.inspect
|
394
|
-
|
427
|
+
if (!@errors.nil? && @errors.length > 0 )
|
395
428
|
# puts 'THERE ARE ERRORS, returning false'
|
396
|
-
|
397
|
-
|
398
|
-
|
429
|
+
return false
|
430
|
+
end
|
431
|
+
end
|
399
432
|
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
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
|
-
|
415
|
-
|
416
|
-
|
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
|
-
|
451
|
+
ok
|
430
452
|
end
|
431
|
-
end
|
432
|
-
ok
|
433
|
-
end
|
434
453
|
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
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
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
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
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
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
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
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
|
-
|
482
|
-
|
500
|
+
def delete_niled(to_delete)
|
501
|
+
if to_delete.size > 0
|
483
502
|
# puts 'Deleting attributes=' + to_delete.inspect
|
484
|
-
|
485
|
-
|
486
|
-
|
503
|
+
delete_attributes to_delete
|
504
|
+
end
|
505
|
+
end
|
487
506
|
|
488
|
-
|
489
|
-
|
490
|
-
|
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
|
-
|
493
|
-
|
511
|
+
# unpad and unoffset
|
512
|
+
if i == arg
|
494
513
|
# puts 'unoffsetting ' + x.to_s
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
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
|
-
|
503
|
-
|
504
|
-
|
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
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
534
|
+
end
|
535
|
+
end
|
536
|
+
if !@@booleans.nil?
|
537
|
+
for b in @@booleans
|
519
538
|
# puts 'converting created: ' + self['created'].inspect
|
520
|
-
|
521
|
-
|
522
|
-
|
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
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
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
|
-
|
558
|
-
|
559
|
-
|
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
|
-
|
580
|
+
x2 -= @@offset
|
562
581
|
# puts 'after subtracting offset='+ x2.to_s
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
582
|
+
x2
|
583
|
+
else
|
584
|
+
x
|
585
|
+
end
|
586
|
+
end
|
568
587
|
|
569
|
-
|
570
|
-
|
588
|
+
def unpad(i, attributes)
|
589
|
+
if !attributes[i].nil?
|
571
590
|
# puts 'before=' + self[i].inspect
|
572
|
-
|
573
|
-
|
591
|
+
attributes[i].collect!{ |x|
|
592
|
+
un_offset_int(x)
|
574
593
|
|
575
|
-
|
594
|
+
}
|
576
595
|
# for x in self[i]
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
596
|
+
# x = self[i][0].to_i
|
597
|
+
# x -= @@offset
|
598
|
+
# self[i] = x
|
599
|
+
# end
|
600
|
+
end
|
601
|
+
end
|
583
602
|
|
584
|
-
|
585
|
-
|
586
|
-
|
603
|
+
def unpad_self
|
604
|
+
if !@@ints.nil?
|
605
|
+
for i in @@ints
|
587
606
|
# puts 'unpadding: ' + i.to_s
|
588
|
-
|
607
|
+
# unpad and unoffset
|
589
608
|
|
590
|
-
|
609
|
+
unpad(i, @attributes)
|
610
|
+
end
|
611
|
+
end
|
591
612
|
end
|
592
|
-
end
|
593
|
-
end
|
594
613
|
|
595
|
-
|
596
|
-
|
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
|
-
|
638
|
+
end
|
620
639
|
|
621
|
-
|
622
|
-
|
623
|
-
|
640
|
+
def update_attributes(*params)
|
641
|
+
return save_attributes(*params)
|
642
|
+
end
|
624
643
|
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
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
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
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
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
676
|
+
def self.find(*params)
|
677
|
+
reload=true
|
678
|
+
first=false
|
679
|
+
all=false
|
680
|
+
select=false
|
681
|
+
select_attributes=[]
|
663
682
|
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
683
|
+
if params.size > 0
|
684
|
+
all = params[0] == :all
|
685
|
+
first = params[0] == :first
|
686
|
+
end
|
668
687
|
|
669
688
|
|
670
|
-
|
671
|
-
|
689
|
+
# Pad and Offset number attributes
|
690
|
+
options = params[1]
|
672
691
|
# puts 'options=' + options.inspect
|
673
|
-
|
674
|
-
|
692
|
+
convert_condition_params(options)
|
693
|
+
|
675
694
|
# puts 'after collect=' + params.inspect
|
676
695
|
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
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
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
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
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
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
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
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
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
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
|
-
|
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
|
-
|
764
|
-
|
765
|
-
|
777
|
+
@@debug=""
|
778
|
+
def self.debug
|
779
|
+
@@debug
|
780
|
+
end
|
766
781
|
|
767
|
-
|
768
|
-
|
769
|
-
|
782
|
+
def self.sanitize_sql(*params)
|
783
|
+
return ActiveRecord::Base.sanitize_sql(*params)
|
784
|
+
end
|
770
785
|
|
771
|
-
|
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
|
-
|
780
|
-
|
781
|
-
|
792
|
+
class SimpleRecord_errors
|
793
|
+
def initialize(*params)
|
794
|
+
super(*params)
|
795
|
+
@errors=[]
|
796
|
+
end
|
782
797
|
|
783
|
-
|
784
|
-
|
785
|
-
|
798
|
+
def add_to_base(value)
|
799
|
+
@errors+=[value]
|
800
|
+
end
|
786
801
|
|
787
|
-
|
788
|
-
|
789
|
-
|
802
|
+
def add(attribute, value)
|
803
|
+
@errors+=["#{attribute.to_s} #{value}"]
|
804
|
+
end
|
790
805
|
|
791
|
-
|
792
|
-
|
793
|
-
|
806
|
+
def count
|
807
|
+
return length
|
808
|
+
end
|
794
809
|
|
795
|
-
|
796
|
-
|
797
|
-
|
810
|
+
def length
|
811
|
+
return @errors.length
|
812
|
+
end
|
798
813
|
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
end
|
814
|
+
def size
|
815
|
+
return length
|
816
|
+
end
|
803
817
|
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
@referencename=referencename.tableize.singularize + "_id"
|
808
|
-
@referencevalue=referencevalue
|
818
|
+
def full_messages
|
819
|
+
return @errors
|
820
|
+
end
|
809
821
|
end
|
810
822
|
|
811
|
-
|
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
|
-
|
814
|
-
@records = []
|
815
|
-
end
|
830
|
+
# Performance optimization if you know the array should be empty
|
816
831
|
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
end
|
821
|
-
return @records
|
822
|
-
end
|
832
|
+
def init_empty
|
833
|
+
@records = []
|
834
|
+
end
|
823
835
|
|
824
|
-
|
825
|
-
|
826
|
-
|
836
|
+
def load
|
837
|
+
if @records.nil?
|
838
|
+
@records = find_all
|
839
|
+
end
|
840
|
+
return @records
|
841
|
+
end
|
827
842
|
|
828
|
-
|
829
|
-
|
830
|
-
|
843
|
+
def [](key)
|
844
|
+
return load[key]
|
845
|
+
end
|
831
846
|
|
832
|
-
|
833
|
-
|
834
|
-
|
847
|
+
def <<(ob)
|
848
|
+
return load << ob
|
849
|
+
end
|
835
850
|
|
836
|
-
|
837
|
-
|
838
|
-
|
851
|
+
def each(*params, &block)
|
852
|
+
return load.each(*params){|record| block.call(record)}
|
853
|
+
end
|
839
854
|
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
end
|
855
|
+
def find_all(*params)
|
856
|
+
find(:all, *params)
|
857
|
+
end
|
844
858
|
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
end
|
859
|
+
def build(*params)
|
860
|
+
params[0][@referencename]=@referencevalue
|
861
|
+
eval(@subname).new(*params)
|
862
|
+
end
|
850
863
|
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
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
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
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
|
-
|
875
|
-
|
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
|
-
|
893
|
+
return eval(@subname).find(*query)
|
894
|
+
end
|
895
|
+
|
896
|
+
end
|
878
897
|
end
|