Class: Tetrahedron::Database

Inherits:
Service
  • Object
show all
Defined in:
lib/tetrahedron/database.rb

Defined Under Namespace

Classes: Configuration, Provider

Constant Summary

Constants inherited from Service

Service::AlreadyConfigured, Service::Misconfigured, Service::Unconfigured, Service::Unreachable

Class Method Summary collapse

Methods inherited from Service

configure, configured!, configured?, wait_until_reachable, wait_until_reachable!

Class Method Details

.connectObject



30
31
32
33
34
35
36
37
38
# File 'lib/tetrahedron/database.rb', line 30

def self.connect
  configured!
  connected = self.class_variable_get(:@@provider).connect
  self.connection.loggers << Logger.new($stdout) if connected
  # Back our models with the new connection.
  model = self.class_variable_get(:@@application).const_get("Model")
  model.db = self.connection if connected
  connected
end

.connected?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/tetrahedron/database.rb', line 21

def self.connected?
  !self.connection.nil?
end

.connectionObject



25
26
27
28
# File 'lib/tetrahedron/database.rb', line 25

def self.connection
  configured!
  self.class_variable_get(:@@provider).connection
end

.disconnectObject



40
41
42
43
44
45
46
# File 'lib/tetrahedron/database.rb', line 40

def self.disconnect
  configured!
  # TODO(mtwilliams): Verify that this is thread-safe.
  model = self.class_variable_get(:@@application).const_get("Model")
  model.db = Sequel.mock
  self.class_variable_get(:@@provider).disconnect
end

.install(application) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tetrahedron/database.rb', line 52

def self.install(application)
  super(application)

  # TODO(mtwilliams): Refactor out this horrid mess.

  # We can't build an inheritence heirarchy.
  # See https://groups.google.com/d/msg/sequel-talk/OG5ti9JAJIE/p1iqO57cwqwJ.
  application.class_eval("Model = Class.new(Sequel::Model);")
  model = application.const_get("Model")

  # Stop Sequel from bitching when we users subclass models before the
  # database connection is established.
  model.db = Sequel.mock

  # Back descendents.
  model.send :define_singleton_method, :db= do |db|
    super(db)
    # All the way down, boys.
    self.descendents.each do |descendent|
      descendent.db = db
    end
  end

  # Custom names are cool, m'kay.
  application.send :define_singleton_method, :Model do |source|
    anonymous_model_class = model::ANONYMOUS_MODEL_CLASSES_MUTEX.synchronize do
                              model::ANONYMOUS_MODEL_CLASSES[source]
                            end
    unless anonymous_model_class
      anonymous_model_class = if source.is_a?(Sequel::Database)
                                Class.new(model).db = source
                              else
                                Class.new(model).set_dataset(source)
                              end
      model::ANONYMOUS_MODEL_CLASSES_MUTEX.synchronize do
        model::ANONYMOUS_MODEL_CLASSES[source] = anonymous_model_class
      end
    end
    anonymous_model_class
  end
end

.migrate(migrations) ⇒ Object



48
49
50
# File 'lib/tetrahedron/database.rb', line 48

def self.migrate(migrations)
  Sequel::IntegerMigrator.run(self.connection, migrations)
end