Skip to content

Commit c53cb34

Browse files
committed
Turn Environment into a standard registry
1 parent af03190 commit c53cb34

File tree

11 files changed

+79
-93
lines changed

11 files changed

+79
-93
lines changed

lib/rom/compat.rb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,11 @@ def relation_classes(gateway = nil)
1818
gw_name = gateway.is_a?(Symbol) ? gateway : gateways_map[gateway]
1919
classes.select { |rel| rel.gateway == gw_name }
2020
end
21-
end
2221

23-
module Gateways
2422
# @api private
25-
class Environment
26-
# @api private
27-
# @deprecated
28-
def gateways_map
29-
@gateways_map ||= gateways.map { |gateway| [gateway, gateway.name] }.to_h
30-
end
31-
32-
alias_method :values, :to_a
33-
34-
# @api private
35-
# @deprecated
36-
def keys
37-
map(&:name)
38-
end
23+
# @deprecated
24+
def gateways_map
25+
@gateways_map ||= gateways.to_a.map(&:reverse).to_h
3926
end
4027
end
4128
end

lib/rom/configuration.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
require "forwardable"
44

55
require "rom/support/notifications"
6-
require "rom/gateways/environment"
76
require "rom/setup"
87
require "rom/configuration_dsl"
98
require "rom/support/configurable"
109
require "rom/support/inflector"
10+
require "rom/gateway_registry"
1111
require "rom/relation_registry"
1212

1313
module ROM
@@ -29,7 +29,7 @@ class Configuration
2929
NoDefaultAdapterError = Class.new(StandardError)
3030

3131
# @!attribute [r] gateways
32-
# @return [Gateways::Environment] Configured runtime gateways
32+
# @return [GatewayRegistry] Configured runtime gateways
3333
attr_reader :gateways
3434

3535
# @!attribute [r] setup
@@ -65,16 +65,17 @@ def initialize(*args, &block)
6565
@setup = Setup.new
6666
@cache = Cache.new
6767

68+
config.gateways = Config.new
69+
@gateways = GatewayRegistry.new({}, cache: cache, config: config.gateways)
70+
6871
configure(*args, &block)
6972

7073
@relations = RelationRegistry.build
7174
end
7275

7376
# @api public
7477
def configure(*args)
75-
if args.empty?
76-
config.gateways = Config.new
77-
else
78+
unless args.empty?
7879
gateways_config = args.first.is_a?(Hash) ? args.first : {default: args}
7980

8081
gateways_config.each do |name, value|
@@ -91,7 +92,7 @@ def configure(*args)
9192
end
9293
end
9394

94-
@gateways = Gateways::Environment.new(config.gateways)
95+
load_gateways
9596

9697
yield(self) if block_given?
9798

@@ -142,7 +143,7 @@ def respond_to?(name, include_all = false)
142143

143144
# @api private
144145
def default_gateway
145-
@default_gateway ||= gateways[:default]
146+
@default_gateway ||= gateways[:default] if gateways.key?(:default)
146147
end
147148

148149
# @api private
@@ -170,6 +171,17 @@ def command_compiler
170171

171172
private
172173

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+
173185
# @api private
174186
def load_config(config, hash)
175187
hash.each do |key, value|

lib/rom/constants.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def initialize(adapter, component)
2020
end
2121

2222
EnvAlreadyFinalizedError = Class.new(StandardError)
23+
GatewayAlreadyDefinedError = Class.new(StandardError)
2324
RelationAlreadyDefinedError = Class.new(StandardError)
2425
CommandAlreadyDefinedError = Class.new(StandardError)
2526
MapperAlreadyDefinedError = Class.new(StandardError)

lib/rom/container.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def commands
159159
#
160160
# @api public
161161
def disconnect
162-
gateways.each(&:disconnect)
162+
gateways.each_value(&:disconnect)
163163
end
164164
end
165165
end

lib/rom/gateway_registry.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require "rom/registry"
4+
5+
module ROM
6+
# @api private
7+
class GatewayRegistry < Registry
8+
# @!attribute [r] config
9+
# @return [Configurable::Config] Gateway configurations
10+
option :config
11+
12+
# @api private
13+
def add(key, gateway)
14+
raise GatewayAlreadyDefinedError, "+#{key}+ is already defined" if key?(key)
15+
16+
elements[key] = gateway
17+
end
18+
end
19+
end

lib/rom/gateways/environment.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

lib/rom/global.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def container(*args, &block)
4949
end
5050

5151
# TODO: this is super-legacy and should be removed
52-
configuration.gateways.each do |gateway|
52+
configuration.gateways.each_value do |gateway|
5353
gateway_config = configuration.config.gateways[gateway.name]
5454
gateway_config.infer_relations = true unless gateway_config.key?(:infer_relations)
5555
end

lib/rom/registry.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ def each(&block)
8080
elements.each(&block)
8181
end
8282

83+
# @api private
84+
def each_key(&block)
85+
elements.each_key(&block)
86+
end
87+
88+
# @api private
89+
def each_value(&block)
90+
elements.each_value(&block)
91+
end
92+
93+
# @api private
94+
def keys
95+
elements.keys
96+
end
97+
98+
# @api private
99+
def values
100+
elements.values
101+
end
102+
83103
# @api private
84104
def key?(name)
85105
!name.nil? && elements.key?(name.to_sym)

spec/compat/unit/rom/gateways/environment_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
require "spec_helper"
44

5-
RSpec.describe ROM::Gateways::Environment do
6-
let(:gateways) { ROM::Configuration.new(*params).gateways }
5+
RSpec.describe ROM::Configuration do
6+
subject(:config) { ROM::Configuration.new(*params) }
7+
8+
let(:gateways) { config.gateways }
9+
let(:gateways_map) { config.gateways_map }
710
let(:params) { [] }
8-
let(:gateways_map) { gateways.gateways_map }
911

1012
context "with an adapter identifier" do
1113
let(:params) { [:memory] }
@@ -91,7 +93,7 @@ def initialize(settings = {})
9193
let(:params) { [gateway] }
9294

9395
it "configures the gateways hash" do
94-
expect(gateways.keys).to eq([:default])
96+
expect(gateways.keys).to eql([:default])
9597
expect(gateways[:default]).to be(gateway)
9698
end
9799
end

spec/unit/rom/create_container_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def orange?
119119
let(:container) { ROM.container(configuration) }
120120

121121
it "builds empty gateways" do
122-
expect(container.gateways.map(&:name)).to be_empty
122+
expect(container.gateways.keys).to be_empty
123123
end
124124

125125
it "builds empty relations" do

0 commit comments

Comments
 (0)