Class: Dcmgr::Models::BaseNew
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Dcmgr::Models::BaseNew
- Defined in:
- lib/dcmgr/models/base_new.rb
Direct Known Subclasses
Account, AccountResource, DhcpRange, FrontendSystem, History, HostnameLease, InstanceNic, InstanceSecurityGroup, IpLease, MacLease, PhysicalNetwork, Quota, RequestLog, TagMapping
Constant Summary collapse
- LOCK_TABLES_KEY =
'__locked_tables'
Class Method Summary collapse
- .dataset ⇒ Object
- .default_row_lock_mode=(mode) ⇒ Object
-
.install_data ⇒ Object
Callback when the initial data is setup to the database.
-
.install_data_hooks(&blk) ⇒ Object
Add callbacks to setup the initial data.
- .lock!(mode = nil) ⇒ Object
- .Proxy(klass) ⇒ Object
- .unlock! ⇒ Object
Instance Method Summary collapse
- #to_hash ⇒ Object
-
#with_timestamps? ⇒ Boolean
Returns true if this Model has time stamps.
Class Method Details
.dataset ⇒ Object
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
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_data ⇒ Object
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
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_hash ⇒ Object
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
463 464 465 |
# File 'lib/dcmgr/models/base_new.rb', line 463 def self.columns.include?(:created_at) && self.columns.include?(:updated_at) end |