Method: Bj::Table::Config::ClassMethods#set

Defined in:
lib/bj/table.rb

#set(key, value, options = {}) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/bj/table.rb', line 265

def set key, value, options = {}
  transaction do
    options.to_options!
    hostname = options[:hostname] || Bj.hostname
    record = find :first, :conditions => conditions(:key => key, :hostname => hostname), :lock => true
    cast = options[:cast] || cast_for(value)
    key = key.to_s
    value = value.to_s
    if record
      record["value"] = value
      record["cast"] = cast
      record.save!
    else
      create! :hostname => hostname, :key => key, :value => value, :cast => cast
    end
    value
  end
end