3
3
require "forwardable"
4
4
5
5
require "rom/support/notifications"
6
- require "rom/gateways/environment"
7
6
require "rom/setup"
8
7
require "rom/configuration_dsl"
9
8
require "rom/support/configurable"
10
9
require "rom/support/inflector"
10
+ require "rom/gateway_registry"
11
11
require "rom/relation_registry"
12
12
13
13
module ROM
@@ -29,7 +29,7 @@ class Configuration
29
29
NoDefaultAdapterError = Class . new ( StandardError )
30
30
31
31
# @!attribute [r] gateways
32
- # @return [Gateways::Environment ] Configured runtime gateways
32
+ # @return [GatewayRegistry ] Configured runtime gateways
33
33
attr_reader :gateways
34
34
35
35
# @!attribute [r] setup
@@ -65,16 +65,17 @@ def initialize(*args, &block)
65
65
@setup = Setup . new
66
66
@cache = Cache . new
67
67
68
+ config . gateways = Config . new
69
+ @gateways = GatewayRegistry . new ( { } , cache : cache , config : config . gateways )
70
+
68
71
configure ( *args , &block )
69
72
70
73
@relations = RelationRegistry . build
71
74
end
72
75
73
76
# @api public
74
77
def configure ( *args )
75
- if args . empty?
76
- config . gateways = Config . new
77
- else
78
+ unless args . empty?
78
79
gateways_config = args . first . is_a? ( Hash ) ? args . first : { default : args }
79
80
80
81
gateways_config . each do |name , value |
@@ -91,7 +92,7 @@ def configure(*args)
91
92
end
92
93
end
93
94
94
- @gateways = Gateways :: Environment . new ( config . gateways )
95
+ load_gateways
95
96
96
97
yield ( self ) if block_given?
97
98
@@ -142,7 +143,7 @@ def respond_to?(name, include_all = false)
142
143
143
144
# @api private
144
145
def default_gateway
145
- @default_gateway ||= gateways [ :default ]
146
+ @default_gateway ||= gateways [ :default ] if gateways . key? ( :default )
146
147
end
147
148
148
149
# @api private
@@ -170,6 +171,17 @@ def command_compiler
170
171
171
172
private
172
173
174
+ # @api private
175
+ def load_gateways
176
+ config . gateways . each do |name , gateway_config |
177
+ gateway = gateway_config . adapter . is_a? ( Gateway ) ?
178
+ gateway_config . adapter : Gateway . setup ( gateway_config . adapter , gateway_config )
179
+ gateway . instance_variable_set ( :"@name" , name )
180
+
181
+ gateways . add ( name , gateway )
182
+ end
183
+ end
184
+
173
185
# @api private
174
186
def load_config ( config , hash )
175
187
hash . each do |key , value |
0 commit comments