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

Instance Method Summary collapse

Class Method Details

.datasetObject



434
435
436
437
438
439
440
441
442
443
# File 'lib/dcmgr/models/base_new.rb', line 434

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

.default_row_lock_mode=(mode) ⇒ Object

Raises:

  • (ArgumentError)


413
414
415
416
# File 'lib/dcmgr/models/base_new.rb', line 413

def self.default_row_lock_mode=(mode)
  raise ArgumentError unless [nil, :share, :update].member?(mode)
  @default_row_lock_mode = mode
end

.install_dataObject

Callback when the initial data is setup to the database.



468
469
470
# File 'lib/dcmgr/models/base_new.rb', line 468

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



480
481
482
483
484
485
486
# File 'lib/dcmgr/models/base_new.rb', line 480

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

.lock!(mode = nil) ⇒ Object

Raises:

  • (ArgumentError)


418
419
420
421
422
423
424
425
# File 'lib/dcmgr/models/base_new.rb', line 418

def self.lock!(mode=nil)
  raise ArgumentError unless [nil, :share, :update].member?(mode)
  mode ||= @default_row_lock_mode
  locktbls = Thread.current[LOCK_TABLES_KEY]
  if locktbls
    locktbls[self.db.uri.to_s + @dataset.first_source_alias.to_s]=mode
  end
end

.Proxy(klass) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/dcmgr/models/base_new.rb', line 447

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



427
428
429
430
431
432
# File 'lib/dcmgr/models/base_new.rb', line 427

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

Instance Method Details

#to_hashObject



407
408
409
# File 'lib/dcmgr/models/base_new.rb', line 407

def to_hash()
  self.values.dup
end

#with_timestamps?Boolean

Returns true if this Model has time stamps

Returns:

  • (Boolean)


463
464
465
# File 'lib/dcmgr/models/base_new.rb', line 463

def with_timestamps?
  self.columns.include?(:created_at) && self.columns.include?(:updated_at)
end