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, FrontendSystem, History, HostnameLease, InstanceNetfilterGroup, InstanceNic, IpLease, MacLease, NetfilterRule, 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
-
#with_timestamps? ⇒ Boolean
Returns true if this Model has time stamps.
Class Method Details
.dataset ⇒ Object
424 425 426 427 428 429 430 431 432 433 |
# File 'lib/dcmgr/models/base_new.rb', line 424 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
403 404 405 406 |
# File 'lib/dcmgr/models/base_new.rb', line 403 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.
458 459 460 |
# File 'lib/dcmgr/models/base_new.rb', line 458 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
470 471 472 473 474 475 476 |
# File 'lib/dcmgr/models/base_new.rb', line 470 def self.install_data_hooks(&blk) @install_data_hooks ||= [] if blk @install_data_hooks << blk end @install_data_hooks end |
.lock!(mode = nil) ⇒ Object
408 409 410 411 412 413 414 415 |
# File 'lib/dcmgr/models/base_new.rb', line 408 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
437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/dcmgr/models/base_new.rb', line 437 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
417 418 419 420 421 422 |
# File 'lib/dcmgr/models/base_new.rb', line 417 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
#with_timestamps? ⇒ Boolean
Returns true if this Model has time stamps
453 454 455 |
# File 'lib/dcmgr/models/base_new.rb', line 453 def self.columns.include?(:created_at) && self.columns.include?(:updated_at) end |