ree_lib 1.0.14 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/ree_lib/packages/ree_dao/Package.schema.json +4 -0
- data/lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb +13 -1
- data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb +57 -0
- data/lib/ree_lib/packages/ree_dao/package/ree_dao.rb +2 -2
- data/lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/persist_assoc.schema.json +50 -0
- data/lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/persist_assoc_spec.rb +162 -0
- data/lib/ree_lib/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a15d911dfb9f1955ac1829941bdb259c1423867dabb407221a1d55232dde3e50
|
4
|
+
data.tar.gz: b76069a3bd3760240ea5232de630f7ff4b372a767b5098ed9a91d7e224f73f7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39b0b379f130624a30c254bc569d3cf8a3942974d036b4bebfc37d1a9c2087917644d61e946b7471b97102b832c43c951d8c209d1f64d9d2d7198b18082f0eed
|
7
|
+
data.tar.gz: 4628fd0c89f2a3670dfb7895decc76aae447057e5d7b0b3591c4942ca1be910b50653d41089589a29a48d794c703921232b4776266721982b30fd78881515ade
|
data/Gemfile.lock
CHANGED
@@ -28,7 +28,7 @@ module ReeDao::DSL
|
|
28
28
|
dao = build_dao(
|
29
29
|
connection: db,
|
30
30
|
table_name: self.class.instance_variable_get(:@table),
|
31
|
-
mapper:
|
31
|
+
mapper: get_schema_mapper,
|
32
32
|
primary_key: self.class.instance_variable_get(:@primary_key),
|
33
33
|
default_select_columns: self.class.instance_variable_get(:@default_select_columns),
|
34
34
|
)
|
@@ -37,6 +37,18 @@ module ReeDao::DSL
|
|
37
37
|
|
38
38
|
dao
|
39
39
|
end
|
40
|
+
|
41
|
+
def get_schema_mapper
|
42
|
+
mapper = self
|
43
|
+
.class
|
44
|
+
.instance_variable_get(:@schema_mapper)
|
45
|
+
|
46
|
+
if mapper.nil?
|
47
|
+
raise Ree::Error.new("Dao schema mapper is not set. Use `schema` DSL to define it", :invalid_dsl_usage)
|
48
|
+
end
|
49
|
+
|
50
|
+
mapper
|
51
|
+
end
|
40
52
|
end
|
41
53
|
|
42
54
|
module ClassMethods
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class ReeDao::PersistAssoc
|
2
|
+
include Ree::FnDSL
|
3
|
+
|
4
|
+
fn :persist_assoc do
|
5
|
+
link :demodulize, from: :ree_string
|
6
|
+
link :underscore, from: :ree_string
|
7
|
+
end
|
8
|
+
|
9
|
+
contract(
|
10
|
+
-> (v) {
|
11
|
+
v.class.ancestors.include?(ReeDto::EntityDSL)
|
12
|
+
},
|
13
|
+
Sequel::Dataset,
|
14
|
+
Ksplat[
|
15
|
+
root_setter?: Symbol,
|
16
|
+
child_assoc?: Symbol,
|
17
|
+
] => nil
|
18
|
+
)
|
19
|
+
def call(agg_root, assoc_dao, **opts)
|
20
|
+
setter_method = if opts[:root_setter].nil?
|
21
|
+
name = underscore(demodulize(agg_root.class.name))
|
22
|
+
"#{name}_id="
|
23
|
+
else
|
24
|
+
"#{opts[:root_setter]}"
|
25
|
+
end
|
26
|
+
|
27
|
+
assoc_name = if opts[:child_assoc].nil?
|
28
|
+
dto_class = assoc_dao
|
29
|
+
.opts[:schema_mapper]
|
30
|
+
.strategies
|
31
|
+
.detect {_1 .method == :db_load }
|
32
|
+
.output
|
33
|
+
.dto
|
34
|
+
|
35
|
+
name = underscore(demodulize(dto_class.name))
|
36
|
+
"#{name}s"
|
37
|
+
else
|
38
|
+
opts[:child_assoc]
|
39
|
+
end
|
40
|
+
|
41
|
+
agg_root.send(assoc_name).each do |child|
|
42
|
+
if !child.respond_to?(setter_method)
|
43
|
+
raise ArgumentError.new("#{child.class} does not respond to `#{setter_method}` method")
|
44
|
+
end
|
45
|
+
|
46
|
+
child.send(setter_method, agg_root.id)
|
47
|
+
|
48
|
+
if child.id
|
49
|
+
assoc_dao.update(child)
|
50
|
+
else
|
51
|
+
assoc_dao.put(child)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
{
|
2
|
+
"schema_type": "object",
|
3
|
+
"schema_version": "1.0",
|
4
|
+
"name": "persist_assoc",
|
5
|
+
"path": "packages/ree_dao/package/ree_dao/functions/persist_assoc.rb",
|
6
|
+
"mount_as": "fn",
|
7
|
+
"class": "ReeDao::PersistAssoc",
|
8
|
+
"factory": null,
|
9
|
+
"methods": [
|
10
|
+
{
|
11
|
+
"doc": "",
|
12
|
+
"throws": [
|
13
|
+
|
14
|
+
],
|
15
|
+
"return": "Any",
|
16
|
+
"args": [
|
17
|
+
{
|
18
|
+
"arg": "agg_root",
|
19
|
+
"type": "Proc#call"
|
20
|
+
},
|
21
|
+
{
|
22
|
+
"arg": "assoc_dao",
|
23
|
+
"type": "Sequel::Dataset"
|
24
|
+
},
|
25
|
+
{
|
26
|
+
"arg": "opts",
|
27
|
+
"type": "Ksplat[:root_setter? => Symbol, :child_assoc? => Symbol]"
|
28
|
+
}
|
29
|
+
]
|
30
|
+
}
|
31
|
+
],
|
32
|
+
"links": [
|
33
|
+
{
|
34
|
+
"target": "demodulize",
|
35
|
+
"package_name": "ree_string",
|
36
|
+
"as": "demodulize",
|
37
|
+
"imports": [
|
38
|
+
|
39
|
+
]
|
40
|
+
},
|
41
|
+
{
|
42
|
+
"target": "underscore",
|
43
|
+
"package_name": "ree_string",
|
44
|
+
"as": "underscore",
|
45
|
+
"imports": [
|
46
|
+
|
47
|
+
]
|
48
|
+
}
|
49
|
+
]
|
50
|
+
}
|
@@ -0,0 +1,162 @@
|
|
1
|
+
# frozen_string_literal = true
|
2
|
+
|
3
|
+
RSpec.describe :persist_assoc do
|
4
|
+
link :persist_assoc, from: :ree_dao
|
5
|
+
link :build_sqlite_connection, from: :ree_dao
|
6
|
+
|
7
|
+
let(:connection) { build_sqlite_connection({database: 'sqlite_db'}) }
|
8
|
+
|
9
|
+
before :all do
|
10
|
+
Ree.enable_irb_mode
|
11
|
+
end
|
12
|
+
|
13
|
+
before :each do
|
14
|
+
[:projects, :project_users].each do |name|
|
15
|
+
if connection.table_exists?(name)
|
16
|
+
connection.drop_table(name)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
connection.create_table :projects do
|
21
|
+
primary_key :id
|
22
|
+
end
|
23
|
+
|
24
|
+
connection.create_table :project_users do
|
25
|
+
primary_key :id
|
26
|
+
foreign_key :project_id, :projects, null: false, on_delete: :cascade
|
27
|
+
end
|
28
|
+
|
29
|
+
connection.disconnect
|
30
|
+
end
|
31
|
+
|
32
|
+
after do
|
33
|
+
Ree.disable_irb_mode
|
34
|
+
connection.disconnect
|
35
|
+
end
|
36
|
+
|
37
|
+
it {
|
38
|
+
module TestPersistAssoc
|
39
|
+
include Ree::PackageDSL
|
40
|
+
|
41
|
+
package do
|
42
|
+
depends_on :ree_dao
|
43
|
+
end
|
44
|
+
|
45
|
+
class Db
|
46
|
+
include Ree::BeanDSL
|
47
|
+
|
48
|
+
bean :db do
|
49
|
+
singleton
|
50
|
+
factory :build
|
51
|
+
|
52
|
+
link :build_sqlite_connection, from: :ree_dao
|
53
|
+
end
|
54
|
+
|
55
|
+
def build
|
56
|
+
build_sqlite_connection({database: 'sqlite_db'})
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Project
|
61
|
+
include ReeDto::EntityDSL
|
62
|
+
|
63
|
+
properties(
|
64
|
+
id: Nilor[Integer]
|
65
|
+
)
|
66
|
+
|
67
|
+
def project_users
|
68
|
+
@project_users ||= []
|
69
|
+
end
|
70
|
+
|
71
|
+
def add_project_user(pu)
|
72
|
+
project_users.push(pu)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class ProjectUser
|
77
|
+
include ReeDto::EntityDSL
|
78
|
+
|
79
|
+
properties(
|
80
|
+
id: Nilor[Integer],
|
81
|
+
project_id: Nilor[Integer]
|
82
|
+
)
|
83
|
+
|
84
|
+
contract Integer => Integer
|
85
|
+
def project_id=(id)
|
86
|
+
@project_id = id
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class ProjectsDao
|
91
|
+
include ReeDao::DSL
|
92
|
+
|
93
|
+
dao :projects_dao do
|
94
|
+
link :db
|
95
|
+
end
|
96
|
+
|
97
|
+
schema Project do
|
98
|
+
integer :id, null: true
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class ProjectUsersDao
|
103
|
+
include ReeDao::DSL
|
104
|
+
|
105
|
+
dao :project_users_dao do
|
106
|
+
link :db
|
107
|
+
end
|
108
|
+
|
109
|
+
schema ProjectUser do
|
110
|
+
integer :id, null: true
|
111
|
+
integer :project_id
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
project = TestPersistAssoc::Project.new(id: 1)
|
117
|
+
|
118
|
+
project.add_project_user(
|
119
|
+
TestPersistAssoc::ProjectUser.new
|
120
|
+
)
|
121
|
+
|
122
|
+
project.add_project_user(
|
123
|
+
TestPersistAssoc::ProjectUser.new
|
124
|
+
)
|
125
|
+
|
126
|
+
TestPersistAssoc::ProjectsDao.new.put(project)
|
127
|
+
|
128
|
+
persist_assoc(project, TestPersistAssoc::ProjectUsersDao.new)
|
129
|
+
|
130
|
+
expect(project.id).to be_a(Integer)
|
131
|
+
expect(project.project_users.first.id).to be_a(Integer)
|
132
|
+
expect(project.project_users.first.project_id).to eq(project.id)
|
133
|
+
expect(project.project_users.last.id).to be_a(Integer)
|
134
|
+
expect(project.project_users.last.project_id).to eq(project.id)
|
135
|
+
|
136
|
+
# test opts
|
137
|
+
project = TestPersistAssoc::Project.new(id: 2)
|
138
|
+
|
139
|
+
project.add_project_user(
|
140
|
+
TestPersistAssoc::ProjectUser.new
|
141
|
+
)
|
142
|
+
|
143
|
+
project.add_project_user(
|
144
|
+
TestPersistAssoc::ProjectUser.new
|
145
|
+
)
|
146
|
+
|
147
|
+
TestPersistAssoc::ProjectsDao.new.put(project)
|
148
|
+
|
149
|
+
persist_assoc(
|
150
|
+
project,
|
151
|
+
TestPersistAssoc::ProjectUsersDao.new,
|
152
|
+
root_setter: :project_id=,
|
153
|
+
child_assoc: :project_users,
|
154
|
+
)
|
155
|
+
|
156
|
+
expect(project.id).to be_a(Integer)
|
157
|
+
expect(project.project_users.first.id).to be_a(Integer)
|
158
|
+
expect(project.project_users.first.project_id).to eq(project.id)
|
159
|
+
expect(project.project_users.last.id).to be_a(Integer)
|
160
|
+
expect(project.project_users.last.project_id).to eq(project.id)
|
161
|
+
}
|
162
|
+
end
|
data/lib/ree_lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ree_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ree
|
@@ -248,13 +248,16 @@ files:
|
|
248
248
|
- lib/ree_lib/packages/ree_dao/package/ree_dao/functions/build_dao.rb
|
249
249
|
- lib/ree_lib/packages/ree_dao/package/ree_dao/functions/build_pg_connection.rb
|
250
250
|
- lib/ree_lib/packages/ree_dao/package/ree_dao/functions/build_sqlite_connection.rb
|
251
|
+
- lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
|
251
252
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/beans/connections.schema.json
|
252
253
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_connection.schema.json
|
253
254
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_dao.schema.json
|
254
255
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_pg_connection.schema.json
|
255
256
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_sqlite_connection.schema.json
|
257
|
+
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/persist_assoc.schema.json
|
256
258
|
- lib/ree_lib/packages/ree_dao/spec/package_schema_spec.rb
|
257
259
|
- lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/build_sqlite_connection_spec.rb
|
260
|
+
- lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/persist_assoc_spec.rb
|
258
261
|
- lib/ree_lib/packages/ree_dao/spec/spec_helper.rb
|
259
262
|
- lib/ree_lib/packages/ree_date/.gitignore
|
260
263
|
- lib/ree_lib/packages/ree_date/.rspec
|