Class: Sequel::ConnectionGuard::ModelGuard
- Inherits:
-
Object
- Object
- Sequel::ConnectionGuard::ModelGuard
- Defined in:
- lib/sequel/extensions/connection_guard/model_guard.rb
Overview
An abstraction for safely accessing Sequel models.
Instance Method Summary collapse
-
#force_execute(&block) ⇒ Object
Unsafely access the model.
-
#initialize(ds, &class_body) ⇒ ModelGuard
constructor
private
A new instance of ModelGuard.
- #raw_model ⇒ Object private
-
#safe_execute(&block) ⇒ Object
Safely access the model.
Constructor Details
#initialize(ds, &class_body) ⇒ ModelGuard
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.
Returns a new instance of ModelGuard.
17 18 19 20 21 22 23 |
# File 'lib/sequel/extensions/connection_guard/model_guard.rb', line 17 def initialize(ds, &class_body) @connection_guard = ds.connection_guard @table_name = ds.table_name @class_body = class_body @connected = false @model = nil end |
Instance Method Details
#force_execute(&block) ⇒ Object
Unsafely access the model. Will fail if connection fails.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sequel/extensions/connection_guard/model_guard.rb', line 62 def force_execute(&block) @connection_guard.force_execute do |connection| instantiate_model(connection) unless @connected yield @model end rescue Sequel::DatabaseConnectionError => error @connected = false raise error end |
#raw_model ⇒ Object
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.
75 76 77 78 |
# File 'lib/sequel/extensions/connection_guard/model_guard.rb', line 75 def raw_model try_instantiate_model if @model.nil? @model end |
#safe_execute(&block) ⇒ Object
Safely access the model.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sequel/extensions/connection_guard/model_guard.rb', line 37 def safe_execute(&block) executor = Executor.new executor.instance_eval(&block) @connection_guard.force_execute do |connection| instantiate_model(connection) unless @connected executor.on_alive.call(@model) end rescue Sequel::DatabaseConnectionError @connected = false executor.on_dead&.call end |