/
gateway.rb
154 lines (141 loc) · 3.53 KB
/
gateway.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module ROM
# Abstract gateway class
#
# @api public
class Gateway
extend ClassMacros
defines :adapter
# Return connection object
#
# @return [Object] type varies depending on the gateway
#
# @api public
attr_reader :connection
# Setup a gateway
#
# @overload setup(type, *args)
# Sets up a single-gateway given a gateway type.
# For custom gateways, create an instance and pass it directly.
#
# @param [Symbol] type
# @param [Array] *args
#
# @overload setup(gateway)
# @param [Gateway] gateway
#
# @return [Gateway] a specific gateway subclass
#
# @example
# module SuperDB
# class Gateway < ROM::Gateway
# def initialize(options)
# end
# end
# end
#
# ROM.register_adapter(:super_db, SuperDB)
#
# Gateway.setup(:super_db, some: 'options')
# # SuperDB::Gateway.new(some: 'options') is called
#
# # or alternatively
# super_db = Gateway.setup(SuperDB::Gateway.new(some: 'options'))
# Gateway.setup(super_db)
#
# @api public
def self.setup(gateway_or_scheme, *args)
case gateway_or_scheme
when String
raise ArgumentError, <<-STRING.gsub(/^ {10}/, '')
URIs without an explicit scheme are not supported anymore.
See https://github.com/rom-rb/rom/blob/master/CHANGELOG.md
STRING
when Symbol
klass = class_from_symbol(gateway_or_scheme)
if klass.instance_method(:initialize).arity == 0
klass.new
else
klass.new(*args)
end
else
if args.empty?
gateway_or_scheme
else
raise ArgumentError, "Can't accept arguments when passing an instance"
end
end
end
# Get gateway subclass for a specific adapter
#
# @param [Symbol] type adapter identifier
#
# @return [Class]
#
# @api private
def self.class_from_symbol(type)
adapter = ROM.adapters.fetch(type) {
begin
require "rom/#{type}"
rescue LoadError
raise AdapterLoadError, "Failed to load adapter rom/#{type}"
end
ROM.adapters.fetch(type)
}
if adapter.const_defined?(:Gateway)
adapter.const_get(:Gateway)
else
adapter.const_get(:Repository)
end
end
# Returns the adapter, defined for the class
#
# @return [Symbol]
#
# @api public
def adapter
self.class.adapter || raise(
MissingAdapterIdentifierError,
"gateway class +#{self}+ is missing the adapter identifier"
)
end
# A generic interface for setting up a logger
#
# @api public
def use_logger(*)
# noop
end
# A generic interface for returning default logger
#
# @api public
def logger
# noop
end
# Extension hook for adding gateway-specific behavior to a command class
#
# @param [Class] klass command class
# @param [Object] _dataset dataset that will be used with this command class
#
# @return [Class]
#
# @api public
def extend_command_class(klass, _dataset)
klass
end
# Schema inference hook
#
# Every gateway that supports schema inference should implement this method
#
# @return [Array] array with datasets and their names
#
# @api private
def schema
[]
end
# Disconnect is optional and it's a no-op by default
#
# @api public
def disconnect
# noop
end
end
end