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/relation_registry"
11
10
12
11
module ROM
13
12
class Configuration
@@ -23,68 +22,75 @@ class Configuration
23
22
register_event ( "configuration.commands.class.before_build" )
24
23
25
24
include ROM ::ConfigurationDSL
25
+ include Configurable
26
26
27
27
NoDefaultAdapterError = Class . new ( StandardError )
28
28
29
- # @!attribute [r] environment
30
- # @return [Environment] Environment object with gateways
31
- attr_reader :environment
32
-
33
29
# @!attribute [r] setup
34
- # @return [Setup] Setup object which collects component classes and plugins
30
+ # @return [Setup] Setup object which manages component and plugins
35
31
attr_reader :setup
36
32
37
33
# @!attribute [r] notifications
38
34
# @return [Notifications] Notification bus instance
39
35
attr_reader :notifications
40
36
41
- # @!attribute [r] cache
42
- # @return [Notifications] Cache
43
- attr_reader :cache
44
-
45
- # @!attribute [r] relations
46
- # @return [ROM::RelationRegistry] relations
47
- attr_reader :relations
48
-
49
- def_delegators :@setup , :register_relation , :register_command ,
50
- :register_mapper , :register_plugin , :auto_register ,
51
- :inflector , :inflector= , :components , :plugins
52
-
53
- def_delegators :@environment , :gateways , :gateways_map , :configure , :config
37
+ def_delegators :@setup , :cache , :relations , :gateways ,
38
+ :register_relation , :register_command , :register_mapper ,
39
+ :register_plugin , :auto_register , :inflector , :inflector= ,
40
+ :components , :plugins
54
41
55
42
# Initialize a new configuration
56
43
#
57
- # @see Environment#initialize
58
- #
59
44
# @return [Configuration]
60
45
#
61
46
# @api private
62
47
def initialize ( *args , &block )
63
- @environment = Environment . new ( *args )
64
- @setup = Setup . new
65
48
@notifications = Notifications . event_bus ( :configuration )
66
- @cache = Cache . new
67
49
68
- @relations = RelationRegistry . build
50
+ config . gateways = Config . new
51
+ @setup = Setup . new ( config : config . gateways )
69
52
70
- block &. call ( self )
53
+ configure ( * args , & block )
71
54
end
72
55
73
- # @api private
74
- def finalize
75
- setup . finalize
56
+ # @api public
57
+ def configure ( *args )
58
+ unless args . empty?
59
+ gateways_config = args . first . is_a? ( Hash ) ? args . first : { default : args }
60
+
61
+ gateways_config . each do |name , value |
62
+ args = Array ( value )
63
+
64
+ adapter , *rest = args
65
+
66
+ if rest . size > 1 && rest . last . is_a? ( Hash )
67
+ load_config ( config . gateways [ name ] , { adapter : adapter , args : rest [ 0 ..-1 ] , **rest . last } )
68
+ else
69
+ options = rest . first . is_a? ( Hash ) ? rest . first : { args : rest . flatten ( 1 ) }
70
+ load_config ( config . gateways [ name ] , { adapter : adapter , **options } )
71
+ end
72
+ end
73
+ end
74
+
75
+ # Load adapters explicitly here to ensure their plugins are present already
76
+ # while setup loads components and then triggers finalization
77
+ setup . load_adapters
78
+
79
+ yield ( self ) if block_given?
80
+
81
+ # No more changes allowed
82
+ config . freeze
83
+
84
+ # Load gateways after yielding config because gateways *need finalized config*
85
+ setup . load_gateways
86
+
76
87
self
77
88
end
78
89
79
90
# @api private
80
- def command_compiler
81
- @command_compiler ||= CommandCompiler . new (
82
- gateways ,
83
- relations ,
84
- Registry . new ,
85
- notifications ,
86
- inflector : inflector
87
- )
91
+ def finalize
92
+ setup . finalize
93
+ self
88
94
end
89
95
90
96
# Apply a plugin to the configuration
@@ -96,10 +102,9 @@ def command_compiler
96
102
#
97
103
# @api public
98
104
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 ) }
105
+ case plugin
106
+ when Array then plugin . each { |p | use ( p ) }
107
+ when Hash then plugin . to_a . each { |p | use ( *p ) }
103
108
else
104
109
ROM . plugin_registry [ :configuration ] . fetch ( plugin ) . apply_to ( self , options )
105
110
end
@@ -116,16 +121,14 @@ def [](name)
116
121
gateways . fetch ( name )
117
122
end
118
123
119
- # Hook for respond_to? used internally
120
- #
121
124
# @api private
122
- def respond_to? ( name , include_all = false )
123
- gateways . key? ( name ) || super
125
+ def default_gateway
126
+ @default_gateway ||= gateways [ :default ] if gateways . key? ( :default )
124
127
end
125
128
126
129
# @api private
127
- def default_gateway
128
- @default_gateway ||= gateways [ :default ]
130
+ def default_adapter
131
+ @default_adapter ||= adapter_for_gateway ( default_gateway ) || ROM . adapters . keys . first
129
132
end
130
133
131
134
# @api private
@@ -136,22 +139,34 @@ def adapter_for_gateway(gateway)
136
139
end
137
140
138
141
# @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 }
142
+ def command_compiler
143
+ @command_compiler ||= CommandCompiler . new (
144
+ gateways ,
145
+ relations ,
146
+ Registry . new ,
147
+ notifications ,
148
+ inflector : inflector
149
+ )
146
150
end
147
151
148
152
# @api private
149
- def default_adapter
150
- @default_adapter ||= adapter_for_gateway ( default_gateway ) || ROM . adapters . keys . first
153
+ def respond_to_missing? ( name , include_all = false )
154
+ gateways . key? ( name ) || super
151
155
end
152
156
153
157
private
154
158
159
+ # @api private
160
+ def load_config ( config , hash )
161
+ hash . each do |key , value |
162
+ if value . is_a? ( Hash )
163
+ load_config ( config [ key ] , value )
164
+ else
165
+ config . send ( "#{ key } =" , value )
166
+ end
167
+ end
168
+ end
169
+
155
170
# Returns gateway if method is a name of a registered gateway
156
171
#
157
172
# @return [Gateway]
0 commit comments