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)
  #puts 'Callbacks included in ' + base.inspect
=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)
#            puts "get_attribute #{name}"
# Check if this arg is already converted
  name_s = name.to_s
  name = name.to_sym
  att_meta = get_att_meta(name)
#            puts "att_meta for #{name}: " + att_meta.inspect
  if att_meta && att_meta.type == :clob
    ret = @lobs[name]
#                puts 'get_attribute clob ' + ret.inspect
    if ret
      if ret.is_a? RemoteNil
        return nil
      else
        return ret
      end
    end
# get it from s3
    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)
#                            puts "single_clob=" + single_clob.inspect
          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))
            # puts 'got from s3 ' + ret.inspect
          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 # was getting errors after upgrade.
    ret = @attributes_rb[name_s] # instance_variable_get(instance_var)
    return ret unless ret.nil?
    return nil if ret.is_a? RemoteNil
    ret = get_attribute_sdb(name)
                                              #                p ret
    ret = sdb_to_ruby(name, ret)
                                              #                p 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)
  puts 'handle_virtuals'
  self.class.virtuals.each do |virtual|
    puts 'virtual=' + virtual.inspect
      #we first copy the information for the virtual to an instance variable of the same name
    send("#{virtual}=", attrs[virtual])
    #eval("@#{virtual}=attrs['#{virtual}']")
      #and then remove the parameter before it is passed to initialize, so that it is NOT sent to SimpleDB
    attrs.delete(virtual)
    #eval("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)
#            puts "SET #{name}=#{value.inspect}" if SimpleRecord.logging?
#            puts "self=" + self.inspect
  attname = name.to_s # default attname
  name = name.to_sym
  att_meta = get_att_meta(name)
  store_rb_val = false
  if att_meta.nil?
    # check if it ends with id and see if att_meta is there
    ends_with = name.to_s[-3, 3]
    if ends_with == "_id"
#                    puts 'ends with id'
      n2 = name.to_s[0, name.length-3]
#                    puts 'n2=' + n2
      att_meta = defined_attributes_local[n2.to_sym]
#                    puts 'defined_attributes_local=' + defined_attributes_local.inspect
      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)
#                  attvalue = value
#puts 'converted ' + value.inspect + ' to ' + attvalue.inspect
    end
  end
  attvalue = strip_array(attvalue)
  make_dirty(name, attvalue) if dirtify
                      #            puts "ARG=#{attname.to_s} setting to #{attvalue}"
  sdb_val = ruby_to_sdb(name, attvalue)
                      #            puts "sdb_val=" + sdb_val.to_s
  @attributes[attname] = sdb_val
                      #            attvalue = wrap_if_required(name, attvalue, sdb_val)
                      #            puts 'attvalue2=' + attvalue.to_s

  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