Module: Bj::Table::Config::ClassMethods

Defined in:
lib/bj/table.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



235
236
237
# File 'lib/bj/table.rb', line 235

def [] key
  get key
end

#[]=(key, value) ⇒ Object



261
262
263
# File 'lib/bj/table.rb', line 261

def []= key, value
  set key, value
end

#cast_for(value) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/bj/table.rb', line 318

def cast_for value
  case value
    when TrueClass, FalseClass
      'to_bool'
    when NilClass
      'to_nil'
    when Fixnum, Bignum
      'to_i'
    when Float
      'to_f'
    when Time
      'to_time'
    when Symbol
      'to_sym'
    else
      case value.to_s
        when %r/^\d+$/
          'to_i'
        when %r/^\d+\.\d+$/
          'to_f'
        when %r/^nil$|^$/
          'to_nil'
        when %r/^true|false$/
          'to_bool'
        else
          'to_s'
      end
  end
end

#castsObject



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
# File 'lib/bj/table.rb', line 348

def casts
  @casts ||= {
    'to_bool' => lambda do |value|
      value.to_s =~ %r/^true$/i ? true : false
    end,
    'to_i' => lambda do |value|
      Integer value.to_s.gsub(%r/^(-)?0*/,'\1')
    end,
    'to_f' => lambda do |value|
      Float value.to_s.gsub(%r/^0*/,'')
    end,
    'to_time' => lambda do |value|
      Time.parse(value.to_s)
    end,
    'to_sym' => lambda do |value|
      value.to_s.to_sym
    end,
    'to_nil' => lambda do |value|
      value.to_s =~ %r/^nil$|^$/i ? nil : value.to_s 
    end,
    'to_s' => lambda do |value|
      value.to_s
    end,
  }
end

#conditions(options = {}) ⇒ Object



248
249
250
251
252
253
254
# File 'lib/bj/table.rb', line 248

def conditions options = {}
  options.to_options!
  options.reverse_merge!(
    :hostname => Bj.hostname
  )
  options
end

#default_for(key) ⇒ Object



256
257
258
259
# File 'lib/bj/table.rb', line 256

def default_for key
  record = find :first, :conditions => conditions(:key => key, :hostname => '*')
  record ? record.value : nil 
end

#delete(key) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/bj/table.rb', line 284

def delete key
  transaction do
    record = find :first, :conditions => conditions(:key => key), :lock => true
    if record
      record.destroy
      record
    else
      nil
    end
  end
end

#for(options = {}) ⇒ Object



310
311
312
313
314
315
316
# File 'lib/bj/table.rb', line 310

def for options = {}
  oh = OrderedHash.new
  find(:all, :conditions => conditions(options)).each do |record|
    oh[record.key] = record.value
  end
  oh
end

#get(key, options = {}) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/bj/table.rb', line 239

def get key, options = {}
  transaction do
    options.to_options!
    hostname = options[:hostname] || Bj.hostname
    record = find :first, :conditions => conditions(:key => key, :hostname => hostname) 
    record ? record.value : default_for(key) 
  end
end

#has_key?(key) ⇒ Boolean Also known as: has_key

Returns:

  • (Boolean)


296
297
298
299
# File 'lib/bj/table.rb', line 296

def has_key? key
  record = find :first, :conditions => conditions(:key => key)
  record ? record : false
end

#keysObject



302
303
304
# File 'lib/bj/table.rb', line 302

def keys
  find(:all, :conditions => conditions).map(&:key)
end

#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

#valuesObject



306
307
308
# File 'lib/bj/table.rb', line 306

def values
  find(:all, :conditions => conditions).map(&:value)
end