rom 2.0.2 → 3.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -4
- data/CHANGELOG.md +34 -1
- data/Gemfile +16 -2
- data/Rakefile +7 -2
- data/lib/rom/array_dataset.rb +44 -0
- data/lib/rom/association_set.rb +11 -5
- data/lib/rom/auto_curry.rb +55 -0
- data/lib/rom/command.rb +70 -41
- data/lib/rom/command_registry.rb +7 -18
- data/lib/rom/commands/class_interface.rb +7 -6
- data/lib/rom/commands/composite.rb +0 -1
- data/lib/rom/commands/graph.rb +7 -15
- data/lib/rom/commands/lazy/update.rb +1 -1
- data/lib/rom/configuration_dsl/command.rb +6 -8
- data/lib/rom/configuration_dsl/mapper.rb +2 -3
- data/lib/rom/configuration_dsl/mapper_dsl.rb +0 -1
- data/lib/rom/configuration_dsl/relation.rb +4 -4
- data/lib/rom/configuration_dsl.rb +0 -4
- data/lib/rom/constants.rb +1 -1
- data/lib/rom/container.rb +0 -2
- data/lib/rom/create_container.rb +0 -2
- data/lib/rom/data_proxy.rb +94 -0
- data/lib/rom/enumerable_dataset.rb +68 -0
- data/lib/rom/gateway.rb +23 -6
- data/lib/rom/global/plugin_dsl.rb +0 -2
- data/lib/rom/global.rb +0 -2
- data/lib/rom/initializer.rb +26 -0
- data/lib/rom/lint/gateway.rb +17 -0
- data/lib/rom/mapper_registry.rb +1 -1
- data/lib/rom/memory/commands.rb +0 -2
- data/lib/rom/memory/dataset.rb +1 -2
- data/lib/rom/memory/relation.rb +14 -1
- data/lib/rom/memory/schema.rb +13 -0
- data/lib/rom/plugin_registry.rb +1 -1
- data/lib/rom/plugins/configuration/configuration_dsl.rb +6 -2
- data/lib/rom/plugins/relation/key_inference.rb +4 -2
- data/lib/rom/plugins/relation/registry_reader.rb +5 -1
- data/lib/rom/registry.rb +50 -0
- data/lib/rom/relation/class_interface.rb +94 -26
- data/lib/rom/relation/curried.rb +15 -15
- data/lib/rom/relation/view_dsl.rb +31 -0
- data/lib/rom/relation.rb +49 -34
- data/lib/rom/schema/dsl.rb +7 -9
- data/lib/rom/schema/type.rb +115 -0
- data/lib/rom/schema.rb +218 -18
- data/lib/rom/setup/auto_registration.rb +20 -17
- data/lib/rom/setup/auto_registration_strategies/base.rb +8 -3
- data/lib/rom/setup/auto_registration_strategies/custom_namespace.rb +4 -3
- data/lib/rom/setup/auto_registration_strategies/no_namespace.rb +5 -4
- data/lib/rom/setup/auto_registration_strategies/with_namespace.rb +3 -3
- data/lib/rom/setup/finalize/finalize_commands.rb +1 -1
- data/lib/rom/setup/finalize/finalize_mappers.rb +1 -1
- data/lib/rom/setup/finalize/finalize_relations.rb +3 -1
- data/lib/rom/setup/finalize.rb +1 -1
- data/lib/rom/transaction.rb +24 -0
- data/lib/rom/types.rb +9 -1
- data/lib/rom/version.rb +1 -1
- data/lib/rom.rb +4 -8
- data/rom.gemspec +4 -3
- data/spec/integration/command_registry_spec.rb +1 -14
- data/spec/integration/commands/create_spec.rb +5 -25
- data/spec/integration/commands/delete_spec.rb +1 -1
- data/spec/integration/commands/error_handling_spec.rb +1 -1
- data/spec/integration/commands/graph_spec.rb +20 -14
- data/spec/integration/commands/update_spec.rb +4 -27
- data/spec/integration/commands_spec.rb +1 -1
- data/spec/integration/{repositories → gateways}/extending_relations_spec.rb +1 -1
- data/spec/integration/{repositories → gateways}/setting_logger_spec.rb +2 -2
- data/spec/integration/mappers/combine_spec.rb +1 -1
- data/spec/integration/mappers/deep_embedded_spec.rb +1 -1
- data/spec/integration/mappers/definition_dsl_spec.rb +1 -1
- data/spec/integration/mappers/embedded_spec.rb +1 -1
- data/spec/integration/mappers/exclude_spec.rb +1 -1
- data/spec/integration/mappers/fold_spec.rb +1 -1
- data/spec/integration/mappers/group_spec.rb +1 -1
- data/spec/integration/mappers/overwrite_attributes_value_spec.rb +1 -1
- data/spec/integration/mappers/prefix_separator_spec.rb +1 -1
- data/spec/integration/mappers/prefix_spec.rb +1 -1
- data/spec/integration/mappers/prefixing_attributes_spec.rb +1 -1
- data/spec/integration/mappers/registering_custom_mappers_spec.rb +1 -1
- data/spec/integration/mappers/renaming_attributes_spec.rb +1 -1
- data/spec/integration/mappers/reusing_mappers_spec.rb +1 -1
- data/spec/integration/mappers/step_spec.rb +1 -1
- data/spec/integration/mappers/symbolizing_attributes_spec.rb +1 -1
- data/spec/integration/mappers/unfold_spec.rb +1 -1
- data/spec/integration/mappers/ungroup_spec.rb +1 -1
- data/spec/integration/mappers/unwrap_spec.rb +2 -2
- data/spec/integration/mappers/wrap_spec.rb +1 -1
- data/spec/integration/memory/commands/create_spec.rb +1 -1
- data/spec/integration/memory/commands/delete_spec.rb +1 -1
- data/spec/integration/memory/commands/update_spec.rb +1 -1
- data/spec/integration/multi_env_spec.rb +1 -1
- data/spec/integration/multi_repo_spec.rb +1 -1
- data/spec/integration/relations/default_dataset_spec.rb +1 -1
- data/spec/integration/relations/reading_spec.rb +1 -1
- data/spec/integration/relations/registry_dsl_spec.rb +1 -1
- data/spec/integration/setup_spec.rb +10 -4
- data/spec/shared/command_graph.rb +8 -4
- data/spec/shared/enumerable_dataset.rb +1 -1
- data/spec/spec_helper.rb +7 -9
- data/spec/support/schema.rb +14 -0
- data/spec/unit/rom/array_dataset_spec.rb +59 -0
- data/spec/unit/rom/association_set_spec.rb +4 -0
- data/spec/unit/rom/auto_curry_spec.rb +63 -0
- data/spec/unit/rom/commands/graph_spec.rb +12 -11
- data/spec/unit/rom/commands/lazy_spec.rb +8 -5
- data/spec/unit/rom/commands/pre_and_post_processors_spec.rb +269 -0
- data/spec/unit/rom/commands/result_spec.rb +1 -1
- data/spec/unit/rom/commands_spec.rb +9 -3
- data/spec/unit/rom/configuration_spec.rb +1 -1
- data/spec/unit/rom/container_spec.rb +11 -5
- data/spec/unit/rom/create_container_spec.rb +1 -1
- data/spec/unit/rom/enumerable_dataset_spec.rb +15 -0
- data/spec/unit/rom/gateway_spec.rb +1 -1
- data/spec/unit/rom/mapper_registry_spec.rb +1 -1
- data/spec/unit/rom/memory/commands_spec.rb +1 -1
- data/spec/unit/rom/memory/dataset_spec.rb +1 -1
- data/spec/unit/rom/memory/{repository_spec.rb → gateway_spec.rb} +1 -1
- data/spec/unit/rom/memory/inheritance_spec.rb +32 -0
- data/spec/unit/rom/memory/relation_spec.rb +15 -3
- data/spec/unit/rom/memory/storage_spec.rb +1 -1
- data/spec/unit/rom/plugin_spec.rb +1 -1
- data/spec/unit/rom/plugins/relation/key_inference_spec.rb +1 -1
- data/spec/unit/rom/registry_spec.rb +86 -0
- data/spec/unit/rom/relation/attribute_reader_spec.rb +17 -0
- data/spec/unit/rom/relation/composite_spec.rb +1 -1
- data/spec/unit/rom/relation/graph_spec.rb +1 -1
- data/spec/unit/rom/relation/lazy/combine_spec.rb +1 -1
- data/spec/unit/rom/relation/lazy_spec.rb +1 -1
- data/spec/unit/rom/relation/loaded_spec.rb +1 -1
- data/spec/unit/rom/relation/schema_spec.rb +10 -6
- data/spec/unit/rom/relation/view_spec.rb +112 -0
- data/spec/unit/rom/relation_spec.rb +16 -2
- data/spec/unit/rom/schema/accessing_attributes_spec.rb +52 -0
- data/spec/unit/rom/schema/exclude_spec.rb +15 -0
- data/spec/unit/rom/schema/finalize_spec.rb +59 -0
- data/spec/unit/rom/schema/key_predicate_spec.rb +15 -0
- data/spec/unit/rom/schema/merge_spec.rb +17 -0
- data/spec/unit/rom/schema/prefix_spec.rb +16 -0
- data/spec/unit/rom/schema/project_spec.rb +15 -0
- data/spec/unit/rom/schema/rename_spec.rb +22 -0
- data/spec/unit/rom/schema/type_spec.rb +49 -0
- data/spec/unit/rom/schema/wrap_spec.rb +17 -0
- data/spec/unit/rom/schema_spec.rb +2 -2
- metadata +69 -17
- data/lib/rom/plugins/relation/view/dsl.rb +0 -32
- data/lib/rom/plugins/relation/view.rb +0 -95
- data/spec/unit/rom/plugins/relation/view_spec.rb +0 -51
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rom/auto_curry'
|
2
|
+
|
3
|
+
RSpec.describe ROM::AutoCurry do
|
4
|
+
subject(:object) { klass.new }
|
5
|
+
|
6
|
+
let(:klass) do
|
7
|
+
Class.new do
|
8
|
+
extend ROM::AutoCurry
|
9
|
+
|
10
|
+
def self.curried(*)
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(*)
|
15
|
+
end
|
16
|
+
|
17
|
+
def arity_0
|
18
|
+
0
|
19
|
+
end
|
20
|
+
|
21
|
+
def arity_1(x)
|
22
|
+
x
|
23
|
+
end
|
24
|
+
|
25
|
+
def arity_2(x, y)
|
26
|
+
[x,y]
|
27
|
+
end
|
28
|
+
|
29
|
+
def arity_many(*args)
|
30
|
+
args
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def leave_me_alone(foo)
|
36
|
+
foo
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'registers auto-curried methods' do
|
42
|
+
expect(object.class.auto_curried_methods).to eql(%i[arity_1 arity_2 arity_many])
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'auto-curries method with arity == 0' do
|
46
|
+
expect(object.arity_0).to be(0)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'auto-curries method with arity == 1' do
|
50
|
+
expect(object.arity_1).to be_instance_of(klass)
|
51
|
+
expect(object.arity_1(1)).to be(1)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'auto-curries method with arity > 0' do
|
55
|
+
expect(object.arity_2).to be_instance_of(klass)
|
56
|
+
expect(object.arity_2(1, 2)).to eql([1, 2])
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'auto-curries method with arity < 0' do
|
60
|
+
expect(object.arity_many).to eql([])
|
61
|
+
expect(object.arity_many(1, 2)).to eql([1, 2])
|
62
|
+
end
|
63
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'dry-struct'
|
3
3
|
|
4
|
-
describe ROM::Commands::Graph do
|
4
|
+
RSpec.describe ROM::Commands::Graph do
|
5
5
|
shared_examples_for 'a persisted graph' do
|
6
6
|
it 'returns nested results' do
|
7
7
|
expect(command.call).to match_array([
|
@@ -75,17 +75,19 @@ describe ROM::Commands::Graph do
|
|
75
75
|
configuration.commands(:tasks) do
|
76
76
|
define(:create) do
|
77
77
|
result :one
|
78
|
+
before :associate
|
78
79
|
|
79
|
-
def
|
80
|
-
|
80
|
+
def associate(task, user)
|
81
|
+
task.merge(user: user[:name])
|
81
82
|
end
|
82
83
|
end
|
83
84
|
|
84
85
|
define(:create) do
|
85
86
|
register_as :create_many
|
87
|
+
before :associate
|
86
88
|
|
87
|
-
def
|
88
|
-
|
89
|
+
def associate(tasks, user)
|
90
|
+
tasks.map { |t| t.merge(user: user[:name]) }
|
89
91
|
end
|
90
92
|
end
|
91
93
|
end
|
@@ -93,13 +95,12 @@ describe ROM::Commands::Graph do
|
|
93
95
|
configuration.commands(:tags) do
|
94
96
|
define(:create) do
|
95
97
|
register_as :create_many
|
98
|
+
before :associate
|
96
99
|
|
97
|
-
def
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
}.flatten
|
102
|
-
)
|
100
|
+
def associate(tags, tasks)
|
101
|
+
Array([tasks]).flatten.map { |task|
|
102
|
+
tags.map { |tag| tag.merge(task: task[:title]) }
|
103
|
+
}.flatten
|
103
104
|
end
|
104
105
|
end
|
105
106
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ROM::Commands::Lazy do
|
3
|
+
RSpec.describe ROM::Commands::Lazy do
|
4
4
|
include_context 'container'
|
5
5
|
|
6
6
|
let(:create_user) { container.command(:users).create }
|
@@ -56,16 +56,19 @@ describe ROM::Commands::Lazy do
|
|
56
56
|
end
|
57
57
|
|
58
58
|
define(:create_many, type: :create) do
|
59
|
-
|
60
|
-
|
59
|
+
before :associate
|
60
|
+
|
61
|
+
def associate(tuples, user)
|
62
|
+
tuples.map { |tuple| tuple.merge(user: user[:name]) }
|
61
63
|
end
|
62
64
|
end
|
63
65
|
|
64
66
|
define(:update) do
|
65
67
|
result :one
|
68
|
+
before :associate
|
66
69
|
|
67
|
-
def
|
68
|
-
|
70
|
+
def associate(tuple, user)
|
71
|
+
tuple.merge(user: user[:name])
|
69
72
|
end
|
70
73
|
end
|
71
74
|
end
|
@@ -0,0 +1,269 @@
|
|
1
|
+
require 'rom/command'
|
2
|
+
|
3
|
+
RSpec.describe ROM::Command, 'before/after hooks' do
|
4
|
+
let(:relation) do
|
5
|
+
spy(:relation)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#before' do
|
9
|
+
subject(:command) do
|
10
|
+
Class.new(ROM::Command) do
|
11
|
+
def prepare(*)
|
12
|
+
end
|
13
|
+
end.build(relation)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns a new command with configured before hooks' do
|
17
|
+
expect(command.before(:prepare).before_hooks).to include(:prepare)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#after' do
|
22
|
+
subject(:command) do
|
23
|
+
Class.new(ROM::Command) do
|
24
|
+
def prepare(*)
|
25
|
+
end
|
26
|
+
end.build(relation)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns a new command with configured after hooks' do
|
30
|
+
expect(command.after(:prepare).after_hooks).to include(:prepare)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'without curried args' do
|
35
|
+
subject(:command) do
|
36
|
+
Class.new(ROM::Command) do
|
37
|
+
result :many
|
38
|
+
before :prepare
|
39
|
+
after :finalize
|
40
|
+
|
41
|
+
def execute(tuples)
|
42
|
+
input = tuples.map.with_index { |tuple, idx| tuple.merge(id: idx + 1) }
|
43
|
+
relation.insert(input)
|
44
|
+
input
|
45
|
+
end
|
46
|
+
|
47
|
+
def prepare(tuples)
|
48
|
+
tuples.map { |tuple| tuple.merge(prepared: true) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def finalize(tuples)
|
52
|
+
tuples.map { |tuple| tuple.merge(finalized: true) }
|
53
|
+
end
|
54
|
+
end.build(relation)
|
55
|
+
end
|
56
|
+
|
57
|
+
let(:tuples) do
|
58
|
+
[{ name: 'Jane' }, { name: 'Joe' }]
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'applies before/after hooks' do
|
62
|
+
insert_tuples = [
|
63
|
+
{ id: 1, name: 'Jane', prepared: true },
|
64
|
+
{ id: 2, name: 'Joe', prepared: true }
|
65
|
+
]
|
66
|
+
|
67
|
+
result = [
|
68
|
+
{ id: 1, name: 'Jane', prepared: true, finalized: true },
|
69
|
+
{ id: 2, name: 'Joe', prepared: true, finalized: true }
|
70
|
+
]
|
71
|
+
|
72
|
+
expect(command.call(tuples)).to eql(result)
|
73
|
+
|
74
|
+
expect(relation).to have_received(:insert).with(insert_tuples)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'with curried args' do
|
79
|
+
subject(:command) do
|
80
|
+
Class.new(ROM::Command) do
|
81
|
+
result :many
|
82
|
+
before :prepare
|
83
|
+
after :finalize
|
84
|
+
|
85
|
+
def execute(tuples)
|
86
|
+
input = tuples.map.with_index { |tuple, idx| tuple.merge(id: idx + 1) }
|
87
|
+
relation.insert(input)
|
88
|
+
input
|
89
|
+
end
|
90
|
+
|
91
|
+
def prepare(tuples, name)
|
92
|
+
tuples.map.with_index { |tuple, idx| tuple.merge(name: "#{name} #{idx + 1}") }
|
93
|
+
end
|
94
|
+
|
95
|
+
def finalize(tuples, *)
|
96
|
+
tuples.map { |tuple| tuple.merge(finalized: true) }
|
97
|
+
end
|
98
|
+
end.build(relation)
|
99
|
+
end
|
100
|
+
|
101
|
+
let(:tuples) do
|
102
|
+
[{ email: 'user-1@test.com' }, { email: 'user-2@test.com' }]
|
103
|
+
end
|
104
|
+
|
105
|
+
let(:relation) do
|
106
|
+
spy(:relation)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'applies before/after hooks' do
|
110
|
+
insert_tuples = [
|
111
|
+
{ id: 1, email: 'user-1@test.com', name: 'User 1' },
|
112
|
+
{ id: 2, email: 'user-2@test.com', name: 'User 2' }
|
113
|
+
]
|
114
|
+
|
115
|
+
result = [
|
116
|
+
{ id: 1, email: 'user-1@test.com', name: 'User 1', finalized: true },
|
117
|
+
{ id: 2, email: 'user-2@test.com', name: 'User 2', finalized: true }
|
118
|
+
]
|
119
|
+
|
120
|
+
expect(command.with(tuples).call('User')).to eql(result)
|
121
|
+
|
122
|
+
expect(relation).to have_received(:insert).with(insert_tuples)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'with pre-set opts' do
|
127
|
+
subject(:command) do
|
128
|
+
Class.new(ROM::Command) do
|
129
|
+
result :many
|
130
|
+
before [prepare: { prepared: true }]
|
131
|
+
after [finalize: { finalized: true }]
|
132
|
+
|
133
|
+
def execute(tuples)
|
134
|
+
input = tuples.map.with_index { |tuple, idx| tuple.merge(id: idx + 1) }
|
135
|
+
relation.insert(input)
|
136
|
+
input
|
137
|
+
end
|
138
|
+
|
139
|
+
def prepare(tuples, opts)
|
140
|
+
tuples.map { |tuple| tuple.merge(opts) }
|
141
|
+
end
|
142
|
+
|
143
|
+
def finalize(tuples, opts)
|
144
|
+
tuples.map { |tuple| tuple.merge(opts) }
|
145
|
+
end
|
146
|
+
end.build(relation)
|
147
|
+
end
|
148
|
+
|
149
|
+
let(:tuples) do
|
150
|
+
[{ name: 'Jane' }, { name: 'Joe' }]
|
151
|
+
end
|
152
|
+
|
153
|
+
let(:relation) do
|
154
|
+
spy(:relation)
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'applies before/after hooks' do
|
158
|
+
insert_tuples = [
|
159
|
+
{ id: 1, name: 'Jane', prepared: true },
|
160
|
+
{ id: 2, name: 'Joe', prepared: true }
|
161
|
+
]
|
162
|
+
|
163
|
+
result = [
|
164
|
+
{ id: 1, name: 'Jane', prepared: true, finalized: true },
|
165
|
+
{ id: 2, name: 'Joe', prepared: true, finalized: true }
|
166
|
+
]
|
167
|
+
|
168
|
+
expect(command.call(tuples)).to eql(result)
|
169
|
+
|
170
|
+
expect(relation).to have_received(:insert).with(insert_tuples)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context 'with pre-set opts for a curried command' do
|
175
|
+
subject(:command) do
|
176
|
+
Class.new(ROM::Command) do
|
177
|
+
result :many
|
178
|
+
before [prepare: { prepared: true }]
|
179
|
+
after [finalize: { finalized: true }]
|
180
|
+
|
181
|
+
def execute(tuples)
|
182
|
+
input = tuples.map.with_index { |tuple, idx| tuple.merge(id: idx + 1) }
|
183
|
+
relation.insert(input)
|
184
|
+
input
|
185
|
+
end
|
186
|
+
|
187
|
+
def prepare(tuples, parent, opts)
|
188
|
+
tuples.map { |tuple| tuple.merge(opts).merge(parent_size: parent.size) }
|
189
|
+
end
|
190
|
+
|
191
|
+
def finalize(tuples, parent, opts)
|
192
|
+
tuples.map { |tuple| tuple.merge(opts).merge(user_id: parent[:id]) }
|
193
|
+
end
|
194
|
+
end.build(relation)
|
195
|
+
end
|
196
|
+
|
197
|
+
let(:tuples) do
|
198
|
+
[{ name: 'Jane' }, { name: 'Joe' }]
|
199
|
+
end
|
200
|
+
|
201
|
+
let(:relation) do
|
202
|
+
spy(:relation)
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'applies before/after hooks' do
|
206
|
+
insert_tuples = [
|
207
|
+
{ id: 1, name: 'Jane', parent_size: 1, prepared: true },
|
208
|
+
{ id: 2, name: 'Joe', parent_size: 1, prepared: true }
|
209
|
+
]
|
210
|
+
|
211
|
+
result = [
|
212
|
+
{ id: 1, name: 'Jane', parent_size: 1, user_id: 1, prepared: true, finalized: true },
|
213
|
+
{ id: 2, name: 'Joe', parent_size: 1, user_id: 1, prepared: true, finalized: true }
|
214
|
+
]
|
215
|
+
|
216
|
+
expect(command.with(tuples).call(id: 1)).to eql(result)
|
217
|
+
|
218
|
+
expect(relation).to have_received(:insert).with(insert_tuples)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'calling with multiple args' do
|
223
|
+
subject(:command) do
|
224
|
+
Class.new(ROM::Command) do
|
225
|
+
result :many
|
226
|
+
before [prepare: { prepared: true }]
|
227
|
+
after [finalize: { finalized: true }]
|
228
|
+
|
229
|
+
def execute(tuples)
|
230
|
+
input = tuples.map.with_index { |tuple, idx| tuple.merge(id: idx + 1) }
|
231
|
+
relation.insert(input)
|
232
|
+
input
|
233
|
+
end
|
234
|
+
|
235
|
+
def prepare(tuples, parent, opts)
|
236
|
+
tuples.map { |tuple| tuple.merge(opts).merge(parent_size: parent.size) }
|
237
|
+
end
|
238
|
+
|
239
|
+
def finalize(tuples, parent, opts)
|
240
|
+
tuples.map { |tuple| tuple.merge(opts).merge(user_id: parent[:id]) }
|
241
|
+
end
|
242
|
+
end.build(relation)
|
243
|
+
end
|
244
|
+
|
245
|
+
let(:tuples) do
|
246
|
+
[{ name: 'Jane' }, { name: 'Joe' }]
|
247
|
+
end
|
248
|
+
|
249
|
+
let(:relation) do
|
250
|
+
spy(:relation)
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'applies before/after hooks' do
|
254
|
+
insert_tuples = [
|
255
|
+
{ id: 1, name: 'Jane', parent_size: 1, prepared: true },
|
256
|
+
{ id: 2, name: 'Joe', parent_size: 1, prepared: true }
|
257
|
+
]
|
258
|
+
|
259
|
+
result = [
|
260
|
+
{ id: 1, name: 'Jane', parent_size: 1, user_id: 1, prepared: true, finalized: true },
|
261
|
+
{ id: 2, name: 'Joe', parent_size: 1, user_id: 1, prepared: true, finalized: true }
|
262
|
+
]
|
263
|
+
|
264
|
+
expect(command.call(tuples, id: 1)).to eql(result)
|
265
|
+
|
266
|
+
expect(relation).to have_received(:insert).with(insert_tuples)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe 'Commands' do
|
3
|
+
RSpec.describe 'Commands' do
|
4
4
|
include_context 'gateway only'
|
5
5
|
include_context 'users and tasks'
|
6
6
|
|
@@ -99,8 +99,14 @@ describe 'Commands' do
|
|
99
99
|
}.build(users)
|
100
100
|
|
101
101
|
create_task = Class.new(ROM::Commands::Create) {
|
102
|
-
|
103
|
-
|
102
|
+
before :associate
|
103
|
+
|
104
|
+
def associate(task_input, user_tuple)
|
105
|
+
task_input.merge(user_id: user_tuple[:user_id])
|
106
|
+
end
|
107
|
+
|
108
|
+
def execute(task_input)
|
109
|
+
relation.insert(task_input)
|
104
110
|
end
|
105
111
|
}.build(tasks)
|
106
112
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ROM::Container do
|
3
|
+
RSpec.describe ROM::Container do
|
4
4
|
include_context 'container'
|
5
5
|
include_context 'users and tasks'
|
6
6
|
|
@@ -8,16 +8,22 @@ describe ROM::Container do
|
|
8
8
|
configuration
|
9
9
|
|
10
10
|
users = Class.new(ROM::Relation[:memory]) do
|
11
|
-
|
12
|
-
|
11
|
+
schema(:users) do
|
12
|
+
attribute :name, ROM::Types::String
|
13
|
+
attribute :email, ROM::Types::String
|
14
|
+
end
|
15
|
+
|
13
16
|
def by_name(name)
|
14
17
|
restrict(name: name).project(:name)
|
15
18
|
end
|
16
19
|
end
|
17
20
|
|
18
21
|
tasks = Class.new(ROM::Relation[:memory]) do
|
19
|
-
|
20
|
-
|
22
|
+
schema(:tasks) do
|
23
|
+
attribute :name, ROM::Types::String
|
24
|
+
attribute :title, ROM::Types::String
|
25
|
+
attribute :priority, ROM::Types::Int
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
create_user = Class.new(ROM::Commands::Create[:memory]) do
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rom/enumerable_dataset'
|
2
|
+
|
3
|
+
RSpec.describe ROM::EnumerableDataset do
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include ROM::EnumerableDataset
|
7
|
+
|
8
|
+
def self.row_proc
|
9
|
+
-> i { i.each_with_object({}) { |(k,v),h| h[k.to_sym] = v } }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it_behaves_like 'an enumerable dataset'
|
15
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rom/memory'
|
2
|
+
|
3
|
+
RSpec.describe ROM::Memory::Relation, '.inherited' do
|
4
|
+
subject(:relation) do
|
5
|
+
Class.new(ROM::Relation[:test]) do
|
6
|
+
schema(:users) do
|
7
|
+
attribute :name, ROM::Types::String
|
8
|
+
end
|
9
|
+
end.new([])
|
10
|
+
end
|
11
|
+
|
12
|
+
before do
|
13
|
+
module Test
|
14
|
+
module AnotherAdapter
|
15
|
+
class Relation < ROM::Memory::Relation
|
16
|
+
adapter :test
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ROM.register_adapter(:test, Test::AnotherAdapter)
|
22
|
+
end
|
23
|
+
|
24
|
+
after do
|
25
|
+
ROM.adapters.delete(:test)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'extends subclass with core methods' do
|
29
|
+
expect(relation.name.to_sym).to be(:users)
|
30
|
+
expect(relation.schema.map(&:name)).to eql(%i[name])
|
31
|
+
end
|
32
|
+
end
|
@@ -4,8 +4,16 @@ require 'rom/lint/spec'
|
|
4
4
|
require 'rom/memory/dataset'
|
5
5
|
require 'rom/memory/relation'
|
6
6
|
|
7
|
-
describe ROM::Memory::Relation do
|
8
|
-
subject(:relation)
|
7
|
+
RSpec.describe ROM::Memory::Relation do
|
8
|
+
subject(:relation) do
|
9
|
+
Class.new(ROM::Memory::Relation) do
|
10
|
+
schema do
|
11
|
+
attribute :name, ROM::Types::String
|
12
|
+
attribute :email, ROM::Types::String
|
13
|
+
attribute :age, ROM::Types::Int
|
14
|
+
end
|
15
|
+
end.new(dataset)
|
16
|
+
end
|
9
17
|
|
10
18
|
let(:dataset) do
|
11
19
|
ROM::Memory::Dataset.new([
|
@@ -30,7 +38,11 @@ describe ROM::Memory::Relation do
|
|
30
38
|
|
31
39
|
describe '#project' do
|
32
40
|
it 'projects tuples with the provided keys' do
|
33
|
-
|
41
|
+
projected = relation.project(:name, :age)
|
42
|
+
|
43
|
+
expect(projected.schema).to eql(relation.schema.project(:name, :age))
|
44
|
+
|
45
|
+
expect(projected).to match_array([
|
34
46
|
{ name: 'Jane', age: 10 },
|
35
47
|
{ name: 'Jade', age: 11 },
|
36
48
|
{ name: 'Joe', age: 12 },
|
@@ -58,7 +58,7 @@ RSpec.describe ROM::Plugins::Relation::KeyInference do
|
|
58
58
|
use :key_inference
|
59
59
|
|
60
60
|
schema :posts do
|
61
|
-
attribute :author_id, ROM::Types::Int.meta(foreign_key: true,
|
61
|
+
attribute :author_id, ROM::Types::Int.meta(foreign_key: true, target: :users)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
end
|