3
3
require "forwardable"
4
4
5
5
require "rom/support/notifications"
6
- require "rom/environment"
7
6
require "rom/setup"
8
7
require "rom/configuration_dsl"
8
+ require "rom/support/configurable"
9
9
require "rom/support/inflector"
10
+ require "rom/gateway_registry"
10
11
require "rom/relation_registry"
11
12
12
13
module ROM
@@ -23,12 +24,13 @@ class Configuration
23
24
register_event ( "configuration.commands.class.before_build" )
24
25
25
26
include ROM ::ConfigurationDSL
27
+ include Configurable
26
28
27
29
NoDefaultAdapterError = Class . new ( StandardError )
28
30
29
- # @!attribute [r] environment
30
- # @return [Environment] Environment object with gateways
31
- attr_reader :environment
31
+ # @!attribute [r] gateways
32
+ # @return [GatewayRegistry] Configured runtime gateways
33
+ attr_reader :gateways
32
34
33
35
# @!attribute [r] setup
34
36
# @return [Setup] Setup object which collects component classes and plugins
@@ -50,24 +52,49 @@ class Configuration
50
52
:register_mapper , :register_plugin , :auto_register ,
51
53
:inflector , :inflector= , :components , :plugins
52
54
53
- def_delegators :@environment , :gateways , :gateways_map , :configure , :config
54
-
55
55
# Initialize a new configuration
56
56
#
57
- # @see Environment#initialize
58
- #
59
57
# @return [Configuration]
60
58
#
61
59
# @api private
62
60
def initialize ( *args , &block )
63
- @environment = Environment . new ( *args )
64
- @setup = Setup . new
65
61
@notifications = Notifications . event_bus ( :configuration )
62
+
63
+ @setup = Setup . new
66
64
@cache = Cache . new
67
65
66
+ config . gateways = Config . new
67
+ @gateways = GatewayRegistry . new ( { } , cache : cache , config : config . gateways )
68
+
69
+ configure ( *args , &block )
70
+
68
71
@relations = RelationRegistry . build
72
+ end
73
+
74
+ # @api public
75
+ def configure ( *args )
76
+ unless args . empty?
77
+ gateways_config = args . first . is_a? ( Hash ) ? args . first : { default : args }
78
+
79
+ gateways_config . each do |name , value |
80
+ args = Array ( value )
81
+
82
+ adapter , *rest = args
83
+
84
+ if rest . size > 1 && rest . last . is_a? ( Hash )
85
+ load_config ( config . gateways [ name ] , { adapter : adapter , args : rest [ 0 ..-1 ] , **rest . last } )
86
+ else
87
+ options = rest . first . is_a? ( Hash ) ? rest . first : { args : rest . flatten ( 1 ) }
88
+ load_config ( config . gateways [ name ] , { adapter : adapter , **options } )
89
+ end
90
+ end
91
+ end
92
+
93
+ load_gateways
94
+
95
+ yield ( self ) if block_given?
69
96
70
- block &. call ( self )
97
+ self
71
98
end
72
99
73
100
# @api private
@@ -76,17 +103,6 @@ def finalize
76
103
self
77
104
end
78
105
79
- # @api private
80
- def command_compiler
81
- @command_compiler ||= CommandCompiler . new (
82
- gateways ,
83
- relations ,
84
- Registry . new ,
85
- notifications ,
86
- inflector : inflector
87
- )
88
- end
89
-
90
106
# Apply a plugin to the configuration
91
107
#
92
108
# @param [Mixed] plugin The plugin identifier, usually a Symbol
@@ -96,10 +112,9 @@ def command_compiler
96
112
#
97
113
# @api public
98
114
def use ( plugin , options = { } )
99
- if plugin . is_a? ( Array )
100
- plugin . each { |p | use ( p ) }
101
- elsif plugin . is_a? ( Hash )
102
- plugin . to_a . each { |p | use ( *p ) }
115
+ case plugin
116
+ when Array then plugin . each { |p | use ( p ) }
117
+ when Hash then plugin . to_a . each { |p | use ( *p ) }
103
118
else
104
119
ROM . plugin_registry [ :configuration ] . fetch ( plugin ) . apply_to ( self , options )
105
120
end
@@ -116,16 +131,14 @@ def [](name)
116
131
gateways . fetch ( name )
117
132
end
118
133
119
- # Hook for respond_to? used internally
120
- #
121
134
# @api private
122
- def respond_to? ( name , include_all = false )
123
- gateways . key? ( name ) || super
135
+ def default_gateway
136
+ @default_gateway ||= gateways [ :default ] if gateways . key? ( :default )
124
137
end
125
138
126
139
# @api private
127
- def default_gateway
128
- @default_gateway ||= gateways [ :default ]
140
+ def default_adapter
141
+ @default_adapter ||= adapter_for_gateway ( default_gateway ) || ROM . adapters . keys . first
129
142
end
130
143
131
144
# @api private
@@ -136,22 +149,51 @@ def adapter_for_gateway(gateway)
136
149
end
137
150
138
151
# @api private
139
- def relation_classes ( gateway = nil )
140
- classes = setup . components . relations . map ( &:constant )
141
-
142
- return classes unless gateway
143
-
144
- gw_name = gateway . is_a? ( Symbol ) ? gateway : gateways_map [ gateway ]
145
- classes . select { |rel | rel . gateway == gw_name }
152
+ def command_compiler
153
+ @command_compiler ||= CommandCompiler . new (
154
+ gateways ,
155
+ relations ,
156
+ Registry . new ,
157
+ notifications ,
158
+ inflector : inflector
159
+ )
146
160
end
147
161
148
162
# @api private
149
- def default_adapter
150
- @default_adapter ||= adapter_for_gateway ( default_gateway ) || ROM . adapters . keys . first
163
+ def respond_to_missing? ( name , include_all = false )
164
+ gateways . key? ( name ) || super
151
165
end
152
166
153
167
private
154
168
169
+ # @api private
170
+ def load_gateways
171
+ config . gateways . each do |name , gateway_config |
172
+ gateway =
173
+ if gateway_config . adapter . is_a? ( Gateway )
174
+ gateway_config . adapter
175
+ else
176
+ Gateway . setup ( gateway_config . adapter , gateway_config )
177
+ end
178
+
179
+ # TODO: this is here to keep backward compatibility
180
+ gateway . instance_variable_set ( :"@name" , name )
181
+
182
+ gateways . add ( name , gateway )
183
+ end
184
+ end
185
+
186
+ # @api private
187
+ def load_config ( config , hash )
188
+ hash . each do |key , value |
189
+ if value . is_a? ( Hash )
190
+ load_config ( config [ key ] , value )
191
+ else
192
+ config . send ( "#{ key } =" , value )
193
+ end
194
+ end
195
+ end
196
+
155
197
# Returns gateway if method is a name of a registered gateway
156
198
#
157
199
# @return [Gateway]
0 commit comments