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

Defined in:
lib/bj/table.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



246
247
248
# File 'lib/bj/table.rb', line 246

def [] key
  get key
end

#[]=(key, value) ⇒ Object



272
273
274
# File 'lib/bj/table.rb', line 272

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

#cast_for(value) ⇒ Object



329
330
331
332
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
# File 'lib/bj/table.rb', line 329

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



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

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



259
260
261
262
263
264
265
# File 'lib/bj/table.rb', line 259

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

#default_for(key) ⇒ Object



267
268
269
270
# File 'lib/bj/table.rb', line 267

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

#delete(key) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/bj/table.rb', line 295

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

#for(options = {}) ⇒ Object



321
322
323
324
325
326
327
# File 'lib/bj/table.rb', line 321

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



250
251
252
253
254
255
256
257
# File 'lib/bj/table.rb', line 250

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)


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

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

#keysObject



313
314
315
# File 'lib/bj/table.rb', line 313

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

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



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/bj/table.rb', line 276

def set key, value, options = {}
  transaction do
    options.to_options!
    hostname = options[:hostname] || Bj.hostname
    record = find :first, :conditions => conditions(:key => key, :hostname => hostname) 
    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



317
318
319
# File 'lib/bj/table.rb', line 317

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