Class: Sequel::ConnectionGuard::DatabaseGuard
- Inherits:
-
Object
- Object
- Sequel::ConnectionGuard::DatabaseGuard
- Defined in:
- lib/sequel/extensions/connection_guard/database_guard.rb
Overview
An abstraction for safely accessing Sequel models.
Instance Method Summary collapse
- #[](table_name) ⇒ Sequel::ConnectionGuard::Dataset private
- #disconnect ⇒ void private
-
#force_execute(&block) ⇒ Object
Unsafely access the database.
-
#initialize(config, &initializer) ⇒ DatabaseGuard
constructor
A new instance of DatabaseGuard.
-
#raw_handle ⇒ Object
A raw connection handle.
-
#safe_execute(&block) ⇒ Object
Safely access the database.
Constructor Details
#initialize(config, &initializer) ⇒ DatabaseGuard
Returns a new instance of DatabaseGuard.
32 33 34 |
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 32 def initialize(config, &initializer) @connection_guard = ConnectionGuard.new(config, &initializer) end |
Instance Method Details
#[](table_name) ⇒ Sequel::ConnectionGuard::Dataset
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
85 86 87 |
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 85 def [](table_name) Dataset.new(@connection_guard, table_name) end |
#disconnect ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
93 94 95 |
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 93 def disconnect @connection_guard.force_execute(&:disconnect) end |
#force_execute(&block) ⇒ Object
Unsafely access the database. Will fail if connection fails.
66 67 68 |
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 66 def force_execute(&block) @connection_guard.force_execute(&block) end |
#raw_handle ⇒ Object
A raw connection handle. Intended for use in test environments (e.x. with DatabaseCleaner)
76 77 78 |
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 76 def raw_handle @connection_guard.raw_handle end |
#safe_execute(&block) ⇒ Object
Safely access the database.
48 49 50 51 52 53 54 |
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 48 def safe_execute(&block) executor = Executor.new executor.instance_eval(&block) @connection_guard.force_execute(&executor.on_alive) rescue Sequel::DatabaseConnectionError executor.on_dead&.call end |