Class: Tetrahedron::Database
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
.connect ⇒ Object
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
model = self.class_variable_get(:@@application).const_get("Model")
model.db = self.connection if connected
connected
end
|
.connected? ⇒ Boolean
21
22
23
|
# File 'lib/tetrahedron/database.rb', line 21
def self.connected?
!self.connection.nil?
end
|
.connection ⇒ Object
25
26
27
28
|
# File 'lib/tetrahedron/database.rb', line 25
def self.connection
configured!
self.class_variable_get(:@@provider).connection
end
|
.disconnect ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/tetrahedron/database.rb', line 40
def self.disconnect
configured!
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)
application.class_eval("Model = Class.new(Sequel::Model);")
model = application.const_get("Model")
model.db = Sequel.mock
model.send :define_singleton_method, :db= do |db|
super(db)
self.descendents.each do |descendent|
descendent.db = db
end
end
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
|