Skip to content

Commit c257268

Browse files
committed
wip
1 parent 6767cfd commit c257268

File tree

18 files changed

+99
-244
lines changed

18 files changed

+99
-244
lines changed

Gemfile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ if ENV["USE_DRY_INITIALIZER_MASTER"].eql?("true")
1212
gem "dry-initializer", github: "dry-rb/dry-initializer", branch: "master"
1313
end
1414

15-
if ENV["USE_ROM_SQL_MASTER"].eql?("true")
16-
gem "rom-sql", github: "rom-rb/rom-sql", branch: "master"
17-
else
18-
gem "rom-sql", "~> 3.3", ">= 3.3.1"
19-
end
20-
2115
group :sql do
16+
gem "rom-sql", github: "rom-rb/rom-sql", branch: "decouple-commands-from-relations"
17+
# TODO: >= 5.32.0 breaks mysql schema inference in some cases
2218
gem "dry-monitor"
2319
gem "jdbc-postgres", platforms: :jruby
2420
gem "jdbc-sqlite3", platforms: :jruby

lib/rom/command.rb

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,22 @@ class Command
194194

195195
# @!attribute [r] relation
196196
# @return [Relation] Command's relation
197-
param :relation
197+
param :dataset
198198

199199
CommandType = Types::Strict::Symbol.enum(:create, :update, :delete)
200200
Result = Types::Strict::Symbol.enum(:one, :many)
201201

202+
# @!attribute [r] schema
203+
# @return [Schema] Relation's schema
204+
option :schema, optional: true
205+
202206
# @!attribute [r] type
203207
# @return [Symbol] The command type, one of :create, :update or :delete
204208
option :type, type: CommandType, optional: true
205209

206210
# @!attribute [r] source
207-
# @return [Relation] The source relation
208-
option :source, default: -> { relation }
211+
# @return [Dataset] The source dataset
212+
option :source, default: -> { dataset }
209213

210214
# @!attribute [r] result
211215
# @return [Symbol] Result type, either :one or :many
@@ -227,26 +231,18 @@ class Command
227231
# @return [Array<Hash>] An array with after hooks configuration
228232
option :after, Types::Coercible::Array, reader: false, default: -> { self.class.after }
229233

230-
input Hash
231-
result :many
232-
233-
# Return name of this command's relation
234-
#
235-
# @return [ROM::Relation::Name]
236-
#
234+
# !@attribute :name
235+
# @return [ROM::Relation::Name] Return name of this command's relation
237236
# @api public
238-
def name
239-
relation.name
240-
end
237+
option :name, optional: true
241238

242-
# Return gateway of this command's relation
243-
#
244-
# @return [Symbol]
245-
#
239+
# !@attribute :gateway
240+
# @return [Symbol] Return gateway of this command's relation
246241
# @api public
247-
def gateway
248-
relation.gateway
249-
end
242+
option :gateway, optional: true
243+
244+
input Hash
245+
result :many
250246

251247
# Execute the command
252248
#
@@ -314,7 +310,7 @@ def curry(*args)
314310
if curry_args.empty? && args.first.is_a?(Graph::InputEvaluator)
315311
Lazy[self].new(self, *args)
316312
else
317-
self.class.build(relation, **options, curry_args: args)
313+
self.class.build(dataset, **options, curry_args: args)
318314
end
319315
end
320316

@@ -346,7 +342,7 @@ def curried?
346342
#
347343
# @api public
348344
def before(*hooks)
349-
self.class.new(relation, **options, before: before_hooks + hooks)
345+
self.class.new(dataset, **options, before: before_hooks + hooks)
350346
end
351347

352348
# Return a new command with appended after hooks
@@ -357,7 +353,7 @@ def before(*hooks)
357353
#
358354
# @api public
359355
def after(*hooks)
360-
self.class.new(relation, **options, after: after_hooks + hooks)
356+
self.class.new(dataset, **options, after: after_hooks + hooks)
361357
end
362358

363359
# List of before hooks
@@ -378,15 +374,15 @@ def after_hooks
378374
options[:after]
379375
end
380376

381-
# Return a new command with other source relation
377+
# Return a new command with other source dataset
382378
#
383-
# This can be used to restrict command with a specific relation
379+
# This can be used to restrict command with a specific dataset
384380
#
385381
# @return [Command]
386382
#
387383
# @api public
388-
def new(new_relation)
389-
self.class.build(new_relation, **options, source: relation)
384+
def new(new_dataset)
385+
self.class.build(new_dataset, **options, source: dataset)
390386
end
391387

392388
# Check if this command has any hooks
@@ -432,22 +428,13 @@ def many?
432428
result.equal?(:many)
433429
end
434430

435-
# Check if this command is restrictible through relation
436-
#
437-
# @return [TrueClass,FalseClass]
438-
#
439-
# @api private
440-
def restrictible?
441-
self.class.restrictable.equal?(true)
442-
end
443-
444431
# Yields tuples for insertion or return an enumerator
445432
#
446433
# @api private
447434
def map_input_tuples(tuples, &mapper)
448435
return enum_for(:with_input_tuples, tuples) unless mapper
449436

450-
if tuples.respond_to? :merge
437+
if tuples.respond_to?(:merge)
451438
mapper[tuples]
452439
else
453440
tuples.map(&mapper)

lib/rom/command_compiler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ def register_command(rel_name, type, rel_meta, parent_relation = nil)
217217
command: klass, gateway: gateway, dataset: relation.dataset, adapter: adapter
218218
)
219219

220-
klass.extend_for_relation(relation) if klass.restrictable
220+
command_input = meta[:input] || relation.input_schema
221221

222-
registry[rel_name][type] = klass.build(relation)
222+
registry[rel_name][type] = klass.build(relation.dataset, input: command_input)
223223
end
224224
end
225225

lib/rom/commands/class_interface.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def adapter_namespace(adapter)
7171
# @return [Command]
7272
#
7373
# @api public
74-
def build(relation, **options)
75-
new(relation, **self.options, **options)
74+
def build(dataset, **options)
75+
new(dataset, **self.options, **options)
7676
end
7777

7878
# Create a command class with a specific type

lib/rom/core.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
# container factory
2626
require "rom/create_container"
2727

28-
# register known plugin types
2928
require "rom/schema_plugin"
3029

30+
# register known plugin types
3131
ROM::Plugins.register(:command)
3232
ROM::Plugins.register(:mapper)
3333
ROM::Plugins.register(:relation)
@@ -37,7 +37,6 @@
3737
# register core plugins
3838
require "rom/plugins/relation/registry_reader"
3939
require "rom/plugins/relation/instrumentation"
40-
require "rom/plugins/command/schema"
4140
require "rom/plugins/command/timestamps"
4241
require "rom/plugins/schema/timestamps"
4342

@@ -48,7 +47,6 @@ module ROM
4847
register :timestamps, ROM::Plugins::Schema::Timestamps, type: :schema
4948
register :registry_reader, ROM::Plugins::Relation::RegistryReader, type: :relation
5049
register :instrumentation, ROM::Plugins::Relation::Instrumentation, type: :relation
51-
register :schema, ROM::Plugins::Command::Schema, type: :command
5250
register :timestamps, ROM::Plugins::Command::Timestamps, type: :command
5351
end
5452
end

lib/rom/memory/commands.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ module Commands
1313
# @api public
1414
class Create < ROM::Commands::Create
1515
adapter :memory
16-
use :schema
1716

1817
# @see ROM::Commands::Create#execute
1918
def execute(tuples)
2019
Array([tuples]).flatten.map { |tuple|
2120
attributes = input[tuple]
22-
relation.insert(attributes.to_h)
21+
dataset.insert(attributes.to_h)
2322
attributes
2423
}.to_a
2524
end
@@ -30,12 +29,11 @@ def execute(tuples)
3029
# @api public
3130
class Update < ROM::Commands::Update
3231
adapter :memory
33-
use :schema
3432

3533
# @see ROM::Commands::Update#execute
3634
def execute(params)
3735
attributes = input[params]
38-
relation.map { |tuple| tuple.update(attributes.to_h) }
36+
dataset.map { |tuple| tuple.update(attributes.to_h) }
3937
end
4038
end
4139

@@ -47,7 +45,7 @@ class Delete < ROM::Commands::Delete
4745

4846
# @see ROM::Commands::Delete#execute
4947
def execute
50-
relation.to_a.map do |tuple|
48+
dataset.to_a.map do |tuple|
5149
source.delete(tuple)
5250
tuple
5351
end

lib/rom/plugins/command/schema.rb

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

lib/rom/relation/commands.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ def command(type, mapper: nil, use: EMPTY_ARRAY, plugins_options: EMPTY_HASH, **
5454
base_command
5555
end
5656

57-
if command.restrictible?
58-
command.new(self)
59-
else
60-
command
61-
end
57+
command.new(dataset)
6258
end
6359
end
6460
end

lib/rom/setup/finalize/finalize_commands.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ def run!
3939
command: klass, gateway: gateway, dataset: relation.dataset, adapter: relation.adapter
4040
)
4141

42-
klass.extend_for_relation(relation) if klass.restrictable
43-
44-
klass.build(relation)
42+
klass.build(
43+
relation.dataset,
44+
input: relation.input_schema,
45+
name: relation.name,
46+
gateway: relation.gateway
47+
)
4548
end
4649

4750
registry = Registry.new
@@ -54,7 +57,7 @@ def run!
5457
)
5558

5659
@relations.each do |(name, relation)|
57-
rel_commands = commands.select { |c| c.relation.name == relation.name }
60+
rel_commands = commands.select { |c| c.dataset.eql?(relation.dataset) }
5861

5962
rel_commands.each do |command|
6063
identifier = command.class.register_as || command.class.default_name

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
require "rom/core"
2222
require "rom-changeset"
23+
require "byebug"
2324

2425
Dir[root.join("support/**/*.rb").to_s].each do |f|
2526
require f unless f.include?("coverage")

0 commit comments

Comments
 (0)