rom-repository 0.3.1 → 1.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 +11 -13
- data/CHANGELOG.md +25 -0
- data/Gemfile +13 -3
- data/lib/rom/repository.rb +57 -19
- data/lib/rom/repository/changeset.rb +89 -26
- data/lib/rom/repository/changeset/create.rb +34 -0
- data/lib/rom/repository/changeset/delete.rb +15 -0
- data/lib/rom/repository/changeset/pipe.rb +11 -4
- data/lib/rom/repository/changeset/update.rb +11 -1
- data/lib/rom/repository/command_compiler.rb +51 -30
- data/lib/rom/repository/command_proxy.rb +3 -1
- data/lib/rom/repository/header_builder.rb +3 -3
- data/lib/rom/repository/mapper_builder.rb +2 -2
- data/lib/rom/repository/relation_proxy.rb +26 -35
- data/lib/rom/repository/relation_proxy/combine.rb +59 -27
- data/lib/rom/repository/root.rb +4 -6
- data/lib/rom/repository/session.rb +55 -0
- data/lib/rom/repository/struct_builder.rb +29 -17
- data/lib/rom/repository/version.rb +1 -1
- data/lib/rom/struct.rb +11 -20
- data/rom-repository.gemspec +4 -3
- data/spec/integration/command_macros_spec.rb +5 -2
- data/spec/integration/command_spec.rb +0 -6
- data/spec/integration/multi_adapter_spec.rb +8 -5
- data/spec/integration/repository_spec.rb +58 -2
- data/spec/integration/root_repository_spec.rb +9 -2
- data/spec/integration/typed_structs_spec.rb +31 -0
- data/spec/shared/database.rb +5 -1
- data/spec/shared/relations.rb +3 -1
- data/spec/shared/repo.rb +13 -1
- data/spec/shared/structs.rb +39 -0
- data/spec/spec_helper.rb +7 -5
- data/spec/support/mutant.rb +10 -0
- data/spec/unit/changeset/map_spec.rb +42 -0
- data/spec/unit/changeset_spec.rb +32 -6
- data/spec/unit/relation_proxy_spec.rb +27 -9
- data/spec/unit/repository/changeset_spec.rb +125 -0
- data/spec/unit/repository/inspect_spec.rb +18 -0
- data/spec/unit/repository/session_spec.rb +251 -0
- data/spec/unit/session_spec.rb +54 -0
- data/spec/unit/struct_builder_spec.rb +45 -1
- metadata +41 -17
- data/lib/rom/repository/struct_attributes.rb +0 -46
- data/spec/unit/header_builder_spec.rb +0 -73
- data/spec/unit/plugins/view_spec.rb +0 -29
- data/spec/unit/sql/relation_spec.rb +0 -54
- data/spec/unit/struct_spec.rb +0 -22
@@ -1,46 +0,0 @@
|
|
1
|
-
module ROM
|
2
|
-
class Repository
|
3
|
-
# @api private
|
4
|
-
class StructAttributes < Module
|
5
|
-
def initialize(attributes)
|
6
|
-
super()
|
7
|
-
|
8
|
-
define_constructor(attributes)
|
9
|
-
|
10
|
-
module_eval do
|
11
|
-
include Dry::Equalizer.new(*attributes)
|
12
|
-
|
13
|
-
attr_reader(*attributes)
|
14
|
-
|
15
|
-
define_method(:to_h) do
|
16
|
-
attributes.each_with_object({}) do |attribute, h|
|
17
|
-
h[attribute] = __send__(attribute)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def define_constructor(attributes)
|
24
|
-
module_eval do
|
25
|
-
def __missing_keyword__(keyword)
|
26
|
-
raise ArgumentError.new("missing keyword: #{keyword}")
|
27
|
-
end
|
28
|
-
private :__missing_keyword__
|
29
|
-
end
|
30
|
-
|
31
|
-
kwargs = attributes.map { |a| "#{a}: __missing_keyword__(:#{a})" }.join(', ')
|
32
|
-
|
33
|
-
ivs = attributes.map { |a| "@#{a}" }.join(', ')
|
34
|
-
values = attributes.join(', ')
|
35
|
-
|
36
|
-
assignment = attributes.size > 0 ? "#{ivs} = #{values}" : EMPTY_STRING
|
37
|
-
|
38
|
-
module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
39
|
-
def initialize(#{kwargs})
|
40
|
-
#{assignment}
|
41
|
-
end
|
42
|
-
RUBY
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
RSpec.describe 'header builder', '#call' do
|
2
|
-
subject(:builder) { ROM::Repository::HeaderBuilder.new }
|
3
|
-
|
4
|
-
let(:user_struct) do
|
5
|
-
builder.struct_builder[:users, [:header, [[:attribute, :id], [:attribute, :name]]]]
|
6
|
-
end
|
7
|
-
|
8
|
-
let(:task_struct) do
|
9
|
-
builder.struct_builder[:tasks, [:header, [[:attribute, :user_id], [:attribute, :title]]]]
|
10
|
-
end
|
11
|
-
|
12
|
-
let(:tag_struct) do
|
13
|
-
builder.struct_builder[:tags, [:header, [[:attribute, :user_id], [:attribute, :tag]]]]
|
14
|
-
end
|
15
|
-
|
16
|
-
describe 'with a relation' do
|
17
|
-
let(:ast) do
|
18
|
-
[:relation, [
|
19
|
-
:users,
|
20
|
-
{ dataset: :users, combine_name: :users },
|
21
|
-
[:header, [[:attribute, :id], [:attribute, :name]]]
|
22
|
-
]]
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'produces a valid header' do
|
26
|
-
header = ROM::Header.coerce([[:id], [:name]], model: user_struct)
|
27
|
-
|
28
|
-
expect(builder[ast]).to eql(header)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe 'with a graph' do
|
33
|
-
let(:ast) do
|
34
|
-
[:relation, [
|
35
|
-
:users,
|
36
|
-
{ dataset: :users, combine_name: :users },
|
37
|
-
[
|
38
|
-
:header, [
|
39
|
-
[:attribute, :id],
|
40
|
-
[:attribute, :name],
|
41
|
-
[:relation, [
|
42
|
-
:tasks,
|
43
|
-
{ dataset: :tasks, keys: { id: :user_id }, combine_type: :many, combine_name: :tasks },
|
44
|
-
[:header, [[:attribute, :user_id], [:attribute, :title]]]
|
45
|
-
]],
|
46
|
-
[:relation, [
|
47
|
-
:tags,
|
48
|
-
{ dataset: :tags, keys: { id: :user_id }, combine_type: :many, combine_name: :tags },
|
49
|
-
[:header, [[:attribute, :user_id], [:attribute, :tag]]]
|
50
|
-
]]
|
51
|
-
]]
|
52
|
-
]
|
53
|
-
]
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'produces a valid header' do
|
57
|
-
attributes = [
|
58
|
-
[:id],
|
59
|
-
[:name],
|
60
|
-
[:tasks, combine: true, type: :array, keys: { id: :user_id },
|
61
|
-
header: ROM::Header.coerce([[:user_id], [:title]], model: task_struct)],
|
62
|
-
[:tags, combine: true, type: :array, keys: { id: :user_id },
|
63
|
-
header: ROM::Header.coerce([[:user_id], [:tag]], model: tag_struct)]
|
64
|
-
]
|
65
|
-
|
66
|
-
header = ROM::Header.coerce(attributes,
|
67
|
-
model: builder.struct_builder[:users, ast[1][2]]
|
68
|
-
)
|
69
|
-
|
70
|
-
expect(builder[ast]).to eql(header)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'rom/memory'
|
2
|
-
|
3
|
-
RSpec.describe ROM::Plugins::Relation::View do
|
4
|
-
subject(:relation) { relation_class.new([]) }
|
5
|
-
|
6
|
-
let(:relation_class) do
|
7
|
-
Class.new(ROM::Memory::Relation) do
|
8
|
-
use :view
|
9
|
-
|
10
|
-
view(:base, [:id, :name]) do
|
11
|
-
self
|
12
|
-
end
|
13
|
-
|
14
|
-
view(:ids, [:id]) do
|
15
|
-
self
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe '#attributes' do
|
21
|
-
it 'returns base view attributes by default' do
|
22
|
-
expect(relation.attributes).to eql([:id, :name])
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'returns attributes for a configured view' do
|
26
|
-
expect(relation.ids.attributes).to eql([:id])
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
RSpec.describe 'SQL Relation extensions' do
|
2
|
-
include_context 'database'
|
3
|
-
|
4
|
-
shared_context 'valid view' do
|
5
|
-
let(:users) { rom.relation(:users) }
|
6
|
-
|
7
|
-
it 'has valid column names' do
|
8
|
-
expect(users.attributes).to eql([:id, :name])
|
9
|
-
|
10
|
-
expect(users.by_pk.attributes).to eql([:name])
|
11
|
-
expect(users.by_pk(1).attributes).to eql([:name])
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '.view' do
|
16
|
-
context 'using short syntax' do
|
17
|
-
before do
|
18
|
-
configuration.relation(:users) do
|
19
|
-
view(:by_pk, [:name]) do |name|
|
20
|
-
where(name: name).select(:name)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
include_context 'valid view'
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'with multi-block syntax' do
|
29
|
-
before do
|
30
|
-
configuration.relation(:users) do
|
31
|
-
view(:by_pk) do
|
32
|
-
header [:name]
|
33
|
-
|
34
|
-
relation do |name|
|
35
|
-
where(name: name).select(:name)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
include_context 'valid view'
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'with multi-block when first block has args' do
|
45
|
-
it 'raises error' do
|
46
|
-
expect {
|
47
|
-
configuration.relation(:users) do
|
48
|
-
view(:by_pk) { |args| }
|
49
|
-
end
|
50
|
-
}.to raise_error(ArgumentError)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
data/spec/unit/struct_spec.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
RSpec.describe ROM::Struct do
|
2
|
-
subject(:struct) do
|
3
|
-
Class.new(ROM::Struct) do
|
4
|
-
attr_reader :id, :name
|
5
|
-
|
6
|
-
def initialize(id, name)
|
7
|
-
@id, @name = id, name
|
8
|
-
end
|
9
|
-
|
10
|
-
def id
|
11
|
-
@id.to_i
|
12
|
-
end
|
13
|
-
end.new("1", "Jane")
|
14
|
-
end
|
15
|
-
|
16
|
-
describe '#[]' do
|
17
|
-
it 'reads an attribute value' do
|
18
|
-
expect(struct.id).to be(1)
|
19
|
-
expect(struct.name).to eql("Jane")
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|