Class: Dcmgr::Models::BaseNew

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/dcmgr/models/base_new.rb

Constant Summary collapse

LOCK_TABLES_KEY =
'__locked_tables'

Class Method Summary collapse

Class Method Details

.datasetObject



415
416
417
418
419
420
421
422
423
# File 'lib/dcmgr/models/base_new.rb', line 415

def self.dataset
  locktbls = Thread.current[LOCK_TABLES_KEY]
  if locktbls && locktbls[self.db.uri.to_s + @dataset.first_source_alias.to_s]
    @dataset.opts = @dataset.opts.merge({:lock=>:update})
  else
    @dataset.opts = @dataset.opts.merge({:lock=>nil})
  end
  @dataset
end

.install_dataObject

Callback when the initial data is setup to the database.



444
445
446
# File 'lib/dcmgr/models/base_new.rb', line 444

def self.install_data
  install_data_hooks.each{|h| h.call }
end

.install_data_hooks(&blk) ⇒ Object

Add callbacks to setup the initial data. The hooks will be called when Model1.install_data() is called.

class Model1 < Base

install_data_hooks do
  Model1.create({:col1=>1, :col2=>2})
end

end



456
457
458
459
460
461
462
# File 'lib/dcmgr/models/base_new.rb', line 456

def self.install_data_hooks(&blk)
  @install_data_hooks ||= []
  if blk
    @install_data_hooks << blk
  end
  @install_data_hooks
end

.lock!Object



401
402
403
404
405
406
# File 'lib/dcmgr/models/base_new.rb', line 401

def self.lock!
  locktbls = Thread.current[LOCK_TABLES_KEY]
  if locktbls
    locktbls[self.db.uri.to_s + @dataset.first_source_alias.to_s]=1
  end
end

.Proxy(klass) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/dcmgr/models/base_new.rb', line 427

def self.Proxy(klass)
  colnames = klass.schema.columns.map {|i| i[:name] }
  colnames.delete_if(klass.primary_key) if klass.restrict_primary_key?
  s = ::Struct.new(*colnames) do
    def to_hash
      n = {}
      self.each_pair { |k,v|
        n[k.to_sym]=v
      }
      n
    end
  end
  s
end

.unlock!Object



408
409
410
411
412
413
# File 'lib/dcmgr/models/base_new.rb', line 408

def self.unlock!
  locktbls = Thread.current[LOCK_TABLES_KEY]
  if locktbls
    locktbls.delete(self.db.uri.to_s + @dataset.first_source_alias.to_s)
  end
end