Method: SimpleRecord::Attributes#set

Defined in:
lib/simple_record/attributes.rb

#set(name, value, dirtify = true) ⇒ Object



252
253
254
255
256
257
258
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
# File 'lib/simple_record/attributes.rb', line 252

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