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

Defined in:
lib/bj/table.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



277
278
279
# File 'lib/bj/table.rb', line 277

def [] key
  get key
end

#[]=(key, value) ⇒ Object



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

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

#cast_for(value) ⇒ Object



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

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



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/bj/table.rb', line 390

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



290
291
292
293
294
295
296
# File 'lib/bj/table.rb', line 290

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

#default_for(key) ⇒ Object



298
299
300
301
# File 'lib/bj/table.rb', line 298

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

#delete(key) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/bj/table.rb', line 326

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



352
353
354
355
356
357
358
# File 'lib/bj/table.rb', line 352

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



281
282
283
284
285
286
287
288
# File 'lib/bj/table.rb', line 281

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)


338
339
340
341
# File 'lib/bj/table.rb', line 338

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

#keysObject



344
345
346
# File 'lib/bj/table.rb', line 344

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

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



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/bj/table.rb', line 307

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



348
349
350
# File 'lib/bj/table.rb', line 348

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