/
model.rb
109 lines (97 loc) · 2.17 KB
/
model.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'rom'
require 'concurrent'
require 'hanami/entity'
require 'hanami/repository'
# Hanami
#
# @since 0.1.0
module Hanami
# Hanami persistence
#
# @since 0.1.0
module Model
require 'hanami/model/version'
require 'hanami/model/error'
require 'hanami/model/configuration'
require 'hanami/model/configurator'
require 'hanami/model/mapping'
require 'hanami/model/plugins'
# @api private
# @since 0.7.0
@__repositories__ = Concurrent::Array.new
class << self
# @since 0.7.0
# @api private
attr_reader :config
# @since 0.7.0
# @api private
attr_reader :loaded
# @since 0.7.0
# @api private
alias loaded? loaded
end
# Configure the framework
#
# @since 0.1.0
#
# @example
# require 'hanami/model'
#
# Hanami::Model.configure do
# adapter :sql, ENV['DATABASE_URL']
#
# migrations 'db/migrations'
# schema 'db/schema.sql'
# end
def self.configure(&block)
@config = Configurator.build(&block)
self
end
# Current configuration
#
# @since 0.1.0
def self.configuration
@configuration ||= Configuration.new(config)
end
# @since 0.7.0
# @api private
def self.repositories
@__repositories__
end
# @since 0.7.0
# @api private
def self.container
raise 'Not loaded' unless loaded?
@container
end
# @since 0.1.0
def self.load!(&blk)
@container = configuration.load!(repositories, &blk)
@loaded = true
end
# Disconnect from the database
#
# This is useful for rebooting applications in production and to ensure that
# the framework prunes stale connections.
#
# @since 1.0.0
#
# @example With Full Stack Hanami Project
# # config/puma.rb
# # ...
# on_worker_boot do
# Hanami.boot
# end
#
# @example With Standalone Hanami::Model
# # config/puma.rb
# # ...
# on_worker_boot do
# Hanami::Model.disconnect
# Hanami::Model.load!
# end
def self.disconnect
configuration.connection&.disconnect
end
end
end