rom 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +81 -0
- data/.travis.yml +2 -1
- data/CHANGELOG.md +41 -0
- data/Gemfile +12 -8
- data/Guardfile +17 -11
- data/README.md +7 -7
- data/Rakefile +29 -0
- data/lib/rom.rb +9 -66
- data/lib/rom/adapter.rb +45 -12
- data/lib/rom/adapter/memory.rb +0 -4
- data/lib/rom/adapter/memory/commands.rb +0 -10
- data/lib/rom/adapter/memory/dataset.rb +18 -6
- data/lib/rom/adapter/memory/storage.rb +0 -3
- data/lib/rom/command_registry.rb +24 -43
- data/lib/rom/commands.rb +5 -6
- data/lib/rom/commands/create.rb +5 -5
- data/lib/rom/commands/delete.rb +8 -6
- data/lib/rom/commands/result.rb +82 -0
- data/lib/rom/commands/update.rb +5 -4
- data/lib/rom/commands/with_options.rb +1 -4
- data/lib/rom/config.rb +70 -0
- data/lib/rom/env.rb +11 -3
- data/lib/rom/global.rb +107 -0
- data/lib/rom/header.rb +122 -89
- data/lib/rom/header/attribute.rb +148 -0
- data/lib/rom/mapper.rb +46 -67
- data/lib/rom/mapper_builder.rb +20 -73
- data/lib/rom/mapper_builder/mapper_dsl.rb +114 -0
- data/lib/rom/mapper_builder/model_dsl.rb +29 -0
- data/lib/rom/mapper_registry.rb +21 -0
- data/lib/rom/model_builder.rb +11 -17
- data/lib/rom/processor.rb +28 -0
- data/lib/rom/processor/transproc.rb +105 -0
- data/lib/rom/reader.rb +81 -21
- data/lib/rom/reader_builder.rb +14 -4
- data/lib/rom/relation.rb +19 -5
- data/lib/rom/relation_builder.rb +20 -6
- data/lib/rom/repository.rb +0 -2
- data/lib/rom/setup.rb +156 -0
- data/lib/rom/{boot → setup}/base_relation_dsl.rb +4 -8
- data/lib/rom/setup/command_dsl.rb +46 -0
- data/lib/rom/setup/finalize.rb +125 -0
- data/lib/rom/setup/mapper_dsl.rb +19 -0
- data/lib/rom/{boot → setup}/relation_dsl.rb +1 -4
- data/lib/rom/setup/schema_dsl.rb +33 -0
- data/lib/rom/support/registry.rb +10 -6
- data/lib/rom/version.rb +1 -1
- data/rom.gemspec +3 -1
- data/spec/integration/adapters/extending_relations_spec.rb +0 -2
- data/spec/integration/commands/create_spec.rb +2 -9
- data/spec/integration/commands/delete_spec.rb +4 -5
- data/spec/integration/commands/error_handling_spec.rb +4 -3
- data/spec/integration/commands/update_spec.rb +3 -8
- data/spec/integration/mappers/deep_embedded_spec.rb +52 -0
- data/spec/integration/mappers/definition_dsl_spec.rb +0 -118
- data/spec/integration/mappers/embedded_spec.rb +82 -0
- data/spec/integration/mappers/group_spec.rb +170 -0
- data/spec/integration/mappers/prefixing_attributes_spec.rb +2 -2
- data/spec/integration/mappers/renaming_attributes_spec.rb +8 -6
- data/spec/integration/mappers/symbolizing_attributes_spec.rb +80 -0
- data/spec/integration/mappers/wrap_spec.rb +162 -0
- data/spec/integration/multi_repo_spec.rb +64 -0
- data/spec/integration/relations/reading_spec.rb +12 -8
- data/spec/integration/relations/registry_dsl_spec.rb +1 -3
- data/spec/integration/schema_spec.rb +10 -0
- data/spec/integration/setup_spec.rb +57 -6
- data/spec/spec_helper.rb +2 -1
- data/spec/unit/config_spec.rb +60 -0
- data/spec/unit/rom/adapter/memory/dataset_spec.rb +52 -0
- data/spec/unit/rom/adapter_spec.rb +31 -11
- data/spec/unit/rom/header_spec.rb +60 -16
- data/spec/unit/rom/mapper_builder_spec.rb +311 -0
- data/spec/unit/rom/mapper_registry_spec.rb +25 -0
- data/spec/unit/rom/mapper_spec.rb +4 -5
- data/spec/unit/rom/model_builder_spec.rb +15 -13
- data/spec/unit/rom/processor/transproc_spec.rb +331 -0
- data/spec/unit/rom/reader_spec.rb +73 -0
- data/spec/unit/rom/registry_spec.rb +38 -0
- data/spec/unit/rom/relation_spec.rb +0 -1
- data/spec/unit/rom/setup_spec.rb +55 -0
- data/spec/unit/rom_spec.rb +14 -0
- metadata +62 -22
- data/Gemfile.devtools +0 -71
- data/lib/rom/boot.rb +0 -197
- data/lib/rom/boot/command_dsl.rb +0 -48
- data/lib/rom/boot/dsl.rb +0 -37
- data/lib/rom/boot/mapper_dsl.rb +0 -23
- data/lib/rom/boot/schema_dsl.rb +0 -27
- data/lib/rom/ra.rb +0 -172
- data/lib/rom/ra/operation/group.rb +0 -47
- data/lib/rom/ra/operation/join.rb +0 -39
- data/lib/rom/ra/operation/wrap.rb +0 -45
- data/lib/rom/transformer.rb +0 -77
- data/spec/integration/ra/group_spec.rb +0 -46
- data/spec/integration/ra/join_spec.rb +0 -50
- data/spec/integration/ra/wrap_spec.rb +0 -37
- data/spec/unit/rom/ra/operation/group_spec.rb +0 -55
- data/spec/unit/rom/ra/operation/wrap_spec.rb +0 -29
- data/spec/unit/rom/transformer_spec.rb +0 -41
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ROM::Registry do
|
4
|
+
subject(:registry) { registry_class.new(mars: mars) }
|
5
|
+
|
6
|
+
let(:mars) { double('mars') }
|
7
|
+
|
8
|
+
let(:registry_class) do
|
9
|
+
Class.new(ROM::Registry) do
|
10
|
+
def self.name
|
11
|
+
'Candy'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#fetch' do
|
17
|
+
it 'returns registered elemented identified by name' do
|
18
|
+
expect(registry[:mars]).to be(mars)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'raises error when element is not found' do
|
22
|
+
expect { registry[:twix] }.to raise_error(
|
23
|
+
ROM::Registry::ElementNotFoundError,
|
24
|
+
":twix doesn't exist in Candy registry"
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '.element_name' do
|
30
|
+
it 'returns registered elemented identified by element_name' do
|
31
|
+
expect(registry[:mars]).to be(mars)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'raises no-method error when element is not there' do
|
35
|
+
expect { registry.twix }.to raise_error(NoMethodError)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ROM::Setup do
|
4
|
+
describe '#finalize' do
|
5
|
+
context 'with an adapter that supports schema inferring' do
|
6
|
+
it 'builds relation from inferred schema' do
|
7
|
+
adapter = double('adapter').as_null_object
|
8
|
+
repo = double('repo', adapter: adapter).as_null_object
|
9
|
+
dataset = double('dataset')
|
10
|
+
|
11
|
+
allow(repo).to receive(:schema).and_return([
|
12
|
+
[:users, dataset, [:name, :email]]
|
13
|
+
])
|
14
|
+
|
15
|
+
setup = ROM::Setup.new(memory: repo)
|
16
|
+
env = setup.finalize
|
17
|
+
|
18
|
+
users = env.relations.users
|
19
|
+
|
20
|
+
expect(users.dataset).to be(dataset)
|
21
|
+
expect(users.header).to eql([:name, :email])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#method_missing' do
|
27
|
+
it 'returns a repository if it is defined' do
|
28
|
+
repo = double('repo')
|
29
|
+
setup = ROM::Setup.new(repo: repo)
|
30
|
+
|
31
|
+
expect(setup.repo).to be(repo)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'raises error if repo is not defined' do
|
35
|
+
setup = ROM::Setup.new({})
|
36
|
+
|
37
|
+
expect { setup.not_here }.to raise_error(NoMethodError, /not_here/)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#[]' do
|
42
|
+
it 'returns a repository if it is defined' do
|
43
|
+
repo = double('repo')
|
44
|
+
setup = ROM::Setup.new(repo: repo)
|
45
|
+
|
46
|
+
expect(setup[:repo]).to be(repo)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'raises error if repo is not defined' do
|
50
|
+
setup = ROM::Setup.new({})
|
51
|
+
|
52
|
+
expect { setup[:not_here] }.to raise_error(KeyError, /not_here/)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ROM do
|
4
|
+
describe '.setup' do
|
5
|
+
it 'creates a boot instance using a database config hash' do
|
6
|
+
boot = ROM.setup(
|
7
|
+
adapter: 'memory', database: 'test', hostname: 'localhost'
|
8
|
+
)
|
9
|
+
|
10
|
+
expect(boot[:default]).to be(boot.repositories[:default])
|
11
|
+
expect(boot.default.adapter.uri.to_s).to eql("memory://localhost/test")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
|
-
- piotr.solnica@gmail.com
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-31 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: addressable
|
@@ -25,6 +24,20 @@ dependencies:
|
|
25
24
|
- - "~>"
|
26
25
|
- !ruby/object:Gem::Version
|
27
26
|
version: '2.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: transproc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.1'
|
28
41
|
- !ruby/object:Gem::Dependency
|
29
42
|
name: equalizer
|
30
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -133,6 +146,20 @@ dependencies:
|
|
133
146
|
- - "~>"
|
134
147
|
- !ruby/object:Gem::Version
|
135
148
|
version: '3.1'
|
149
|
+
- !ruby/object:Gem::Dependency
|
150
|
+
name: rspec-mocks
|
151
|
+
requirement: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - "~>"
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '3.1'
|
156
|
+
type: :development
|
157
|
+
prerelease: false
|
158
|
+
version_requirements: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - "~>"
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '3.1'
|
136
163
|
- !ruby/object:Gem::Dependency
|
137
164
|
name: rspec-expectations
|
138
165
|
requirement: !ruby/object:Gem::Requirement
|
@@ -155,11 +182,11 @@ extra_rdoc_files: []
|
|
155
182
|
files:
|
156
183
|
- ".gitignore"
|
157
184
|
- ".rspec"
|
185
|
+
- ".rubocop.yml"
|
158
186
|
- ".ruby-version"
|
159
187
|
- ".travis.yml"
|
160
188
|
- CHANGELOG.md
|
161
189
|
- Gemfile
|
162
|
-
- Gemfile.devtools
|
163
190
|
- Guardfile
|
164
191
|
- LICENSE
|
165
192
|
- README.md
|
@@ -170,35 +197,39 @@ files:
|
|
170
197
|
- lib/rom/adapter/memory/commands.rb
|
171
198
|
- lib/rom/adapter/memory/dataset.rb
|
172
199
|
- lib/rom/adapter/memory/storage.rb
|
173
|
-
- lib/rom/boot.rb
|
174
|
-
- lib/rom/boot/base_relation_dsl.rb
|
175
|
-
- lib/rom/boot/command_dsl.rb
|
176
|
-
- lib/rom/boot/dsl.rb
|
177
|
-
- lib/rom/boot/mapper_dsl.rb
|
178
|
-
- lib/rom/boot/relation_dsl.rb
|
179
|
-
- lib/rom/boot/schema_dsl.rb
|
180
200
|
- lib/rom/command_registry.rb
|
181
201
|
- lib/rom/commands.rb
|
182
202
|
- lib/rom/commands/create.rb
|
183
203
|
- lib/rom/commands/delete.rb
|
204
|
+
- lib/rom/commands/result.rb
|
184
205
|
- lib/rom/commands/update.rb
|
185
206
|
- lib/rom/commands/with_options.rb
|
207
|
+
- lib/rom/config.rb
|
186
208
|
- lib/rom/env.rb
|
209
|
+
- lib/rom/global.rb
|
187
210
|
- lib/rom/header.rb
|
211
|
+
- lib/rom/header/attribute.rb
|
188
212
|
- lib/rom/mapper.rb
|
189
213
|
- lib/rom/mapper_builder.rb
|
214
|
+
- lib/rom/mapper_builder/mapper_dsl.rb
|
215
|
+
- lib/rom/mapper_builder/model_dsl.rb
|
216
|
+
- lib/rom/mapper_registry.rb
|
190
217
|
- lib/rom/model_builder.rb
|
191
|
-
- lib/rom/
|
192
|
-
- lib/rom/
|
193
|
-
- lib/rom/ra/operation/join.rb
|
194
|
-
- lib/rom/ra/operation/wrap.rb
|
218
|
+
- lib/rom/processor.rb
|
219
|
+
- lib/rom/processor/transproc.rb
|
195
220
|
- lib/rom/reader.rb
|
196
221
|
- lib/rom/reader_builder.rb
|
197
222
|
- lib/rom/relation.rb
|
198
223
|
- lib/rom/relation_builder.rb
|
199
224
|
- lib/rom/repository.rb
|
225
|
+
- lib/rom/setup.rb
|
226
|
+
- lib/rom/setup/base_relation_dsl.rb
|
227
|
+
- lib/rom/setup/command_dsl.rb
|
228
|
+
- lib/rom/setup/finalize.rb
|
229
|
+
- lib/rom/setup/mapper_dsl.rb
|
230
|
+
- lib/rom/setup/relation_dsl.rb
|
231
|
+
- lib/rom/setup/schema_dsl.rb
|
200
232
|
- lib/rom/support/registry.rb
|
201
|
-
- lib/rom/transformer.rb
|
202
233
|
- lib/rom/version.rb
|
203
234
|
- rom.gemspec
|
204
235
|
- spec/integration/adapters/extending_relations_spec.rb
|
@@ -208,12 +239,15 @@ files:
|
|
208
239
|
- spec/integration/commands/error_handling_spec.rb
|
209
240
|
- spec/integration/commands/try_spec.rb
|
210
241
|
- spec/integration/commands/update_spec.rb
|
242
|
+
- spec/integration/mappers/deep_embedded_spec.rb
|
211
243
|
- spec/integration/mappers/definition_dsl_spec.rb
|
244
|
+
- spec/integration/mappers/embedded_spec.rb
|
245
|
+
- spec/integration/mappers/group_spec.rb
|
212
246
|
- spec/integration/mappers/prefixing_attributes_spec.rb
|
213
247
|
- spec/integration/mappers/renaming_attributes_spec.rb
|
214
|
-
- spec/integration/
|
215
|
-
- spec/integration/
|
216
|
-
- spec/integration/
|
248
|
+
- spec/integration/mappers/symbolizing_attributes_spec.rb
|
249
|
+
- spec/integration/mappers/wrap_spec.rb
|
250
|
+
- spec/integration/multi_repo_spec.rb
|
217
251
|
- spec/integration/relations/reading_spec.rb
|
218
252
|
- spec/integration/relations/registry_dsl_spec.rb
|
219
253
|
- spec/integration/schema_spec.rb
|
@@ -221,16 +255,22 @@ files:
|
|
221
255
|
- spec/shared/users_and_tasks.rb
|
222
256
|
- spec/spec_helper.rb
|
223
257
|
- spec/support/mutant.rb
|
258
|
+
- spec/unit/config_spec.rb
|
259
|
+
- spec/unit/rom/adapter/memory/dataset_spec.rb
|
224
260
|
- spec/unit/rom/adapter_spec.rb
|
225
261
|
- spec/unit/rom/env_spec.rb
|
226
262
|
- spec/unit/rom/header_spec.rb
|
263
|
+
- spec/unit/rom/mapper_builder_spec.rb
|
264
|
+
- spec/unit/rom/mapper_registry_spec.rb
|
227
265
|
- spec/unit/rom/mapper_spec.rb
|
228
266
|
- spec/unit/rom/model_builder_spec.rb
|
229
|
-
- spec/unit/rom/
|
230
|
-
- spec/unit/rom/
|
267
|
+
- spec/unit/rom/processor/transproc_spec.rb
|
268
|
+
- spec/unit/rom/reader_spec.rb
|
269
|
+
- spec/unit/rom/registry_spec.rb
|
231
270
|
- spec/unit/rom/relation_spec.rb
|
232
271
|
- spec/unit/rom/repository_spec.rb
|
233
|
-
- spec/unit/rom/
|
272
|
+
- spec/unit/rom/setup_spec.rb
|
273
|
+
- spec/unit/rom_spec.rb
|
234
274
|
homepage: http://rom-rb.org
|
235
275
|
licenses:
|
236
276
|
- MIT
|
data/Gemfile.devtools
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
group :development do
|
4
|
-
gem 'rake', '~> 10.3.2'
|
5
|
-
gem 'rspec', '~> 3.1.0'
|
6
|
-
gem 'rspec-its', '~> 1.0.1'
|
7
|
-
gem 'yard', '~> 0.8.7.4'
|
8
|
-
|
9
|
-
platform :rbx do
|
10
|
-
gem 'rubysl-singleton', '~> 2.0.0'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
group :yard do
|
15
|
-
gem 'kramdown', '~> 1.3.3'
|
16
|
-
end
|
17
|
-
|
18
|
-
group :guard do
|
19
|
-
gem 'guard', '~> 2.6.1'
|
20
|
-
gem 'guard-bundler', '~> 2.0.0'
|
21
|
-
gem 'guard-rspec', '~> 4.2.9'
|
22
|
-
gem 'guard-rubocop', '~> 1.1.0'
|
23
|
-
|
24
|
-
# file system change event handling
|
25
|
-
gem 'listen', '~> 2.7.7'
|
26
|
-
gem 'rb-fchange', '~> 0.0.6', require: false
|
27
|
-
gem 'rb-fsevent', '~> 0.9.4', require: false
|
28
|
-
gem 'rb-inotify', '~> 0.9.5', require: false
|
29
|
-
|
30
|
-
# notification handling
|
31
|
-
gem 'libnotify', '~> 0.8.3', require: false
|
32
|
-
gem 'rb-notifu', '~> 0.0.4', require: false
|
33
|
-
gem 'terminal-notifier-guard', '~> 1.5.3', require: false
|
34
|
-
end
|
35
|
-
|
36
|
-
group :metrics do
|
37
|
-
gem 'coveralls', '~> 0.7.0'
|
38
|
-
gem 'flay', '~> 2.5.0'
|
39
|
-
gem 'flog', '~> 4.2.1'
|
40
|
-
gem 'reek', '~> 1.3.7'
|
41
|
-
gem 'rubocop', '~> 0.23.0'
|
42
|
-
gem 'simplecov', '~> 0.7.1'
|
43
|
-
gem 'yardstick', '~> 0.9.9'
|
44
|
-
|
45
|
-
platforms :mri do
|
46
|
-
gem 'mutant', '~> 0.6.3'
|
47
|
-
gem 'mutant-rspec', '~> 0.6.3'
|
48
|
-
end
|
49
|
-
|
50
|
-
platforms :ruby_19, :ruby_20 do
|
51
|
-
gem 'yard-spellcheck', '~> 0.1.5'
|
52
|
-
end
|
53
|
-
|
54
|
-
platform :rbx do
|
55
|
-
gem 'json', '~> 1.8.1'
|
56
|
-
gem 'racc', '~> 1.4.11'
|
57
|
-
gem 'rubysl-logger', '~> 2.0.0'
|
58
|
-
gem 'rubysl-open-uri', '~> 2.0.0'
|
59
|
-
gem 'rubysl-prettyprint', '~> 2.0.3'
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
group :benchmarks do
|
64
|
-
gem 'rbench', '~> 0.2.3'
|
65
|
-
end
|
66
|
-
|
67
|
-
platform :jruby do
|
68
|
-
group :jruby do
|
69
|
-
gem 'jruby-openssl', '~> 0.9.4'
|
70
|
-
end
|
71
|
-
end
|
data/lib/rom/boot.rb
DELETED
@@ -1,197 +0,0 @@
|
|
1
|
-
require 'rom/boot/dsl'
|
2
|
-
require 'rom/relation_builder'
|
3
|
-
require 'rom/reader_builder'
|
4
|
-
require 'rom/command_registry'
|
5
|
-
|
6
|
-
module ROM
|
7
|
-
|
8
|
-
# Exposes DSL for defining schema, relations and mappers
|
9
|
-
#
|
10
|
-
# @api public
|
11
|
-
class Boot
|
12
|
-
include Equalizer.new(:repositories, :env)
|
13
|
-
|
14
|
-
attr_reader :repositories, :adapter_relation_map, :env
|
15
|
-
|
16
|
-
# @api private
|
17
|
-
def initialize(repositories)
|
18
|
-
@repositories = repositories
|
19
|
-
@schema = {}
|
20
|
-
@relations = {}
|
21
|
-
@mappers = []
|
22
|
-
@commands = {}
|
23
|
-
@adapter_relation_map = {}
|
24
|
-
@env = nil
|
25
|
-
end
|
26
|
-
|
27
|
-
# Schema definition DSL
|
28
|
-
#
|
29
|
-
# @example
|
30
|
-
#
|
31
|
-
# setup.schema do
|
32
|
-
# base_relation(:users) do
|
33
|
-
# repository :sqlite
|
34
|
-
#
|
35
|
-
# attribute :id
|
36
|
-
# attribute :name
|
37
|
-
# end
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# @api public
|
41
|
-
def schema(&block)
|
42
|
-
base_relations = DSL.new(self).schema(&block)
|
43
|
-
base_relations.each do |repo, relations|
|
44
|
-
(@schema[repo] ||= []).concat(relations)
|
45
|
-
end
|
46
|
-
self
|
47
|
-
end
|
48
|
-
|
49
|
-
# Relation definition DSL
|
50
|
-
#
|
51
|
-
# @example
|
52
|
-
#
|
53
|
-
# setup.relation(:users) do
|
54
|
-
# def names
|
55
|
-
# project(:name)
|
56
|
-
# end
|
57
|
-
# end
|
58
|
-
#
|
59
|
-
# @api public
|
60
|
-
def relation(name, &block)
|
61
|
-
@relations.update(name => block)
|
62
|
-
self
|
63
|
-
end
|
64
|
-
|
65
|
-
# Mapper definition DSL
|
66
|
-
#
|
67
|
-
# @example
|
68
|
-
#
|
69
|
-
# setup.mappers do
|
70
|
-
# define(:users) do
|
71
|
-
# model name: 'User'
|
72
|
-
# end
|
73
|
-
#
|
74
|
-
# define(:names, parent: :users) do
|
75
|
-
# exclude :id
|
76
|
-
# end
|
77
|
-
# end
|
78
|
-
#
|
79
|
-
# @api public
|
80
|
-
def mappers(&block)
|
81
|
-
@mappers.concat(DSL.new(self).mappers(&block))
|
82
|
-
self
|
83
|
-
end
|
84
|
-
|
85
|
-
def commands(name, &block)
|
86
|
-
@commands.update(name => DSL.new(self).commands(&block))
|
87
|
-
end
|
88
|
-
|
89
|
-
# Finalize the setup
|
90
|
-
#
|
91
|
-
# @return [Env] frozen env with access to repositories, schema, relations and mappers
|
92
|
-
#
|
93
|
-
# @api public
|
94
|
-
def finalize
|
95
|
-
raise EnvAlreadyFinalizedError if env
|
96
|
-
|
97
|
-
schema = load_schema
|
98
|
-
relations = load_relations(schema)
|
99
|
-
readers = load_readers(relations)
|
100
|
-
commands = load_commands(relations)
|
101
|
-
|
102
|
-
@env = Env.new(repositories, schema, relations, readers, commands)
|
103
|
-
end
|
104
|
-
|
105
|
-
# @api private
|
106
|
-
def [](name)
|
107
|
-
repositories.fetch(name)
|
108
|
-
end
|
109
|
-
|
110
|
-
# @api private
|
111
|
-
def respond_to_missing?(name, include_context = false)
|
112
|
-
repositories.key?(name)
|
113
|
-
end
|
114
|
-
|
115
|
-
private
|
116
|
-
|
117
|
-
# @api private
|
118
|
-
def method_missing(name, *args)
|
119
|
-
repositories.fetch(name)
|
120
|
-
end
|
121
|
-
|
122
|
-
# @api private
|
123
|
-
def load_schema
|
124
|
-
repositories.values.each do |repo|
|
125
|
-
(@schema[repo] ||= []).concat(repo.schema)
|
126
|
-
end
|
127
|
-
|
128
|
-
base_relations = @schema.each_with_object({}) do |(repo, schema), h|
|
129
|
-
schema.each do |name, dataset, header|
|
130
|
-
adapter_relation_map[name] = repo.adapter
|
131
|
-
h[name] = Relation.new(dataset, header)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
Schema.new(base_relations)
|
136
|
-
end
|
137
|
-
|
138
|
-
# @api private
|
139
|
-
def load_relations(schema)
|
140
|
-
return RelationRegistry.new unless adapter_relation_map.any?
|
141
|
-
|
142
|
-
builder = RelationBuilder.new(schema)
|
143
|
-
|
144
|
-
relations = @relations.each_with_object({}) do |(name, block), h|
|
145
|
-
adapter = adapter_relation_map[name]
|
146
|
-
|
147
|
-
relation = builder.call(name) { |klass|
|
148
|
-
adapter.extend_relation_class(klass)
|
149
|
-
klass.class_eval(&block) if block
|
150
|
-
}
|
151
|
-
|
152
|
-
adapter.extend_relation_instance(relation)
|
153
|
-
|
154
|
-
h[name] = relation
|
155
|
-
end
|
156
|
-
|
157
|
-
relations.each_value { |relation| relation.class.finalize(relations, relation) }
|
158
|
-
|
159
|
-
RelationRegistry.new(relations)
|
160
|
-
end
|
161
|
-
|
162
|
-
# @api private
|
163
|
-
def load_readers(relations)
|
164
|
-
return ReaderRegistry.new unless adapter_relation_map.any?
|
165
|
-
|
166
|
-
reader_builder = ReaderBuilder.new(relations)
|
167
|
-
|
168
|
-
readers = @mappers.each_with_object({}) do |(name, options, block), h|
|
169
|
-
h[name] = reader_builder.call(name, options, &block)
|
170
|
-
end
|
171
|
-
|
172
|
-
ReaderRegistry.new(readers)
|
173
|
-
end
|
174
|
-
|
175
|
-
def load_commands(relations)
|
176
|
-
return Registry.new unless relations.elements.any?
|
177
|
-
|
178
|
-
commands = @commands.each_with_object({}) do |(name, definitions), h|
|
179
|
-
adapter = adapter_relation_map[name]
|
180
|
-
|
181
|
-
rel_commands = {}
|
182
|
-
|
183
|
-
definitions.each do |command_name, definition|
|
184
|
-
rel_commands[command_name] = adapter.command(
|
185
|
-
command_name, relations[name], definition
|
186
|
-
)
|
187
|
-
end
|
188
|
-
|
189
|
-
h[name] = CommandRegistry.new(rel_commands)
|
190
|
-
end
|
191
|
-
|
192
|
-
Registry.new(commands)
|
193
|
-
end
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
end
|