Module: SimpleRecord::Attributes
- Included in:
- Base
- Defined in:
- lib/simple_record/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Classes: Attribute
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/simple_record/attributes.rb', line 6
def self.included(base)
=begin
instance_eval <<-endofeval
def self.defined_attributes
#puts 'class defined_attributes'
@attributes ||= {}
@attributes
endendofeval
endofeval
=end
end
|
Instance Method Details
#get_attribute(name) ⇒ 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.
333
334
335
336
337
338
339
340
341
342
343
344
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
397
398
399
400
401
402
403
|
# File 'lib/simple_record/attributes.rb', line 333
def get_attribute(name)
name_s = name.to_s
name = name.to_sym
att_meta = get_att_meta(name)
if att_meta && att_meta.type == :clob
ret = @lobs[name]
if ret
if ret.is_a? RemoteNil
return nil
else
return ret
end
end
unless new_record?
if self.class.get_sr_config[:single_clob]
begin
single_clob = s3_bucket(false, :s3_bucket=>:new).get(single_clob_id)
single_clob = JSON.parse(single_clob)
single_clob.each_pair do |name2, val|
@lobs[name2.to_sym] = val
end
ret = @lobs[name]
SimpleRecord.stats.s3_gets += 1
rescue Aws::AwsError => ex
if ex.include?(/NoSuchKey/) || ex.include?(/NoSuchBucket/)
ret = nil
else
raise ex
end
end
else
begin
ret = s3_bucket.get(s3_lob_id(name))
SimpleRecord.stats.s3_gets += 1
rescue Aws::AwsError => ex
if ex.include?(/NoSuchKey/) || ex.include?(/NoSuchBucket/)
ret = nil
else
raise ex
end
end
end
if ret.nil?
ret = RemoteNil.new
end
end
@lobs[name] = ret
return nil if ret.is_a? RemoteNil
return ret
else
@attributes_rb = {} unless @attributes_rb ret = @attributes_rb[name_s] return ret unless ret.nil?
return nil if ret.is_a? RemoteNil
ret = get_attribute_sdb(name)
ret = sdb_to_ruby(name, ret)
@attributes_rb[name_s] = ret
return ret
end
end
|
#get_attribute_sdb(name) ⇒ Object
325
326
327
328
329
|
# File 'lib/simple_record/attributes.rb', line 325
def get_attribute_sdb(name)
name = name.to_sym
ret = strip_array(@attributes[sdb_att_name(name)])
return ret
end
|
#handle_virtuals(attrs) ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/simple_record/attributes.rb', line 245
def handle_virtuals(attrs)
self.class.virtuals.each do |virtual|
send("#{virtual}=", attrs[virtual])
attrs.delete(virtual)
end
end
|
#set(name, value, dirtify = true) ⇒ Object
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
# File 'lib/simple_record/attributes.rb', line 259
def set(name, value, dirtify=true)
attname = name.to_s name = name.to_sym
att_meta = get_att_meta(name)
store_rb_val = false
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.to_sym
end
return if att_meta.nil?
else
if att_meta.type == :belongs_to
ends_with = name.to_s[-3, 3]
if ends_with == "_id"
att_name = name.to_s
attvalue = value
else
attname = name.to_s + '_id'
attvalue = value.nil? ? nil : value.id
store_rb_val = true
end
elsif att_meta.type == :clob
make_dirty(name, value) if dirtify
@lobs[name] = value
return
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
if store_rb_val
@attributes_rb[name.to_s] = value
else
@attributes_rb.delete(name.to_s)
end
end
|
#set_attribute_sdb(name, val) ⇒ Object
320
321
322
|
# File 'lib/simple_record/attributes.rb', line 320
def set_attribute_sdb(name, val)
@attributes[sdb_att_name(name)] = val
end
|