Module: Sequel

Defined in:
lib/sequel/extensions/connection_guard.rb,
lib/sequel/extensions/connection_guard/dataset.rb,
lib/sequel/extensions/connection_guard/executor.rb,
lib/sequel/extensions/connection_guard/model_guard.rb,
lib/sequel/extensions/connection_guard/database_guard.rb,
lib/sequel/extensions/connection_guard/connection_guard.rb,
lib/sequel/extensions/connection_guard/configuration_error.rb

Overview

Since:

  • 0.1.0

Defined Under Namespace

Modules: ConnectionGuard

Constant Summary collapse

DatabaseGuard =

See Also:

Since:

  • 0.1.0

ConnectionGuard::DatabaseGuard

Class Method Summary collapse

Class Method Details

.ModelGuard(ds, &class_body) ⇒ Object

A constructor for model guards.

rubocop:disable Naming/MethodName

Examples:

Creating model guards

DB = Sequel::DatabaseGuard.new('postgres://localhost/mydb')

UserGuard = Sequel::ModelGuard(DB[:users]) do
  many_to_one :cookies, class: 'CookieGuard::RawModel', key: :user_id

  def admin?
    role == 'admin'
  end
end

CookieGuard = Sequel::ModelGuard(DB[:cookies])

Safely accessing a model

users = UserGuard.safe_execute do
  alive do |model|
    model.all
  end

  dead do
    []
  end
end

Unsafely accessing a model (raises an exception if connection fails)

cookies = UserGuard.force_execute { |model| model.first!(id: id).cookies }

Parameters:

Since:

  • 0.1.0



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sequel/extensions/connection_guard.rb', line 48

def self.ModelGuard(ds, &class_body)
  model = ConnectionGuard::ModelGuard.new(ds, &class_body)

  Class.new.tap do |klass|
    klass.define_singleton_method(:safe_execute) do |&block|
      model.safe_execute(&block)
    end

    klass.define_singleton_method(:force_execute) do |&block|
      model.force_execute(&block)
    end

    klass.define_singleton_method(:const_missing) do |const_name|
      if const_name == :RawModel
        model.raw_model
      else
        super(const_name)
      end
    end
  end
end