ree_lib 1.0.14 → 1.0.16
Sign up to get free protection for your applications and to get access to all the features.
- 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/types/pg_array.rb +43 -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/packages/ree_dao/spec/ree_dao/types/pg_array_spec.rb +19 -0
- data/lib/ree_lib/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9005df5ff7af1e3f311ab2126f137560568d8e2c42bfb7da28b9a2e179675b4a
|
4
|
+
data.tar.gz: b0c9cd050d90ad8a2f66a738bfd3487c27d9561a367ce3a7d204c5ba6bf8d1cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8816a34c2a7b69a9a82d61c5fc2f69055216cfe6ff02241c78850b9940fe37d09c1df87b49e0f137342f84ac304f6e3b0dfc4075af92f29a86fb4e77a92a981c
|
7
|
+
data.tar.gz: 9bd5bce19e5803ae000b362efcad7721802bc1c816dc1af26eefb80fc04c893b4b8c505471b033456c4b500a84b76de9b1ea319869adfab945c75ae2c52c675c
|
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,43 @@
|
|
1
|
+
require "sequel/extensions/pg_array"
|
2
|
+
|
3
|
+
class ReeDao::PgArray < ReeMapper::AbstractType
|
4
|
+
contract(
|
5
|
+
ReeEnum::Value,
|
6
|
+
Kwargs[
|
7
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
8
|
+
] => String
|
9
|
+
)
|
10
|
+
def serialize(value, role: nil)
|
11
|
+
raise ArgumentError.new("not supported")
|
12
|
+
end
|
13
|
+
|
14
|
+
contract(
|
15
|
+
Any,
|
16
|
+
Kwargs[
|
17
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
18
|
+
] => ReeEnum::Value
|
19
|
+
).throws(ReeMapper::CoercionError)
|
20
|
+
def cast(value, role: nil)
|
21
|
+
raise ArgumentError.new("not supported")
|
22
|
+
end
|
23
|
+
|
24
|
+
contract(
|
25
|
+
ArrayOf[Any],
|
26
|
+
Kwargs[
|
27
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
28
|
+
] => Sequel::Postgres::PGArray
|
29
|
+
)
|
30
|
+
def db_dump(value, role: nil)
|
31
|
+
Sequel.pg_array(value)
|
32
|
+
end
|
33
|
+
|
34
|
+
contract(
|
35
|
+
Sequel::Postgres::PGArray,
|
36
|
+
Kwargs[
|
37
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
38
|
+
] => ArrayOf[Any]
|
39
|
+
).throws(ReeMapper::TypeError)
|
40
|
+
def db_load(value, role: nil)
|
41
|
+
value.to_a
|
42
|
+
end
|
43
|
+
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
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal = true
|
2
|
+
package_require("ree_dao/types/pg_array")
|
3
|
+
|
4
|
+
RSpec.describe ReeDao::PgArray do
|
5
|
+
it {
|
6
|
+
type = ReeDao::PgArray.new
|
7
|
+
|
8
|
+
result = type.db_dump([1], role: nil)
|
9
|
+
expect(result).to be_a(Sequel::Postgres::PGArray)
|
10
|
+
}
|
11
|
+
|
12
|
+
it {
|
13
|
+
type = ReeDao::PgArray.new
|
14
|
+
|
15
|
+
result = type.db_dump([1], role: nil)
|
16
|
+
result = type.db_load(result, role: nil)
|
17
|
+
expect(result).to eq([1])
|
18
|
+
}
|
19
|
+
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.16
|
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,18 @@ 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
|
252
|
+
- lib/ree_lib/packages/ree_dao/package/ree_dao/types/pg_array.rb
|
251
253
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/beans/connections.schema.json
|
252
254
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_connection.schema.json
|
253
255
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_dao.schema.json
|
254
256
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_pg_connection.schema.json
|
255
257
|
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_sqlite_connection.schema.json
|
258
|
+
- lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/persist_assoc.schema.json
|
256
259
|
- lib/ree_lib/packages/ree_dao/spec/package_schema_spec.rb
|
257
260
|
- lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/build_sqlite_connection_spec.rb
|
261
|
+
- lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/persist_assoc_spec.rb
|
262
|
+
- lib/ree_lib/packages/ree_dao/spec/ree_dao/types/pg_array_spec.rb
|
258
263
|
- lib/ree_lib/packages/ree_dao/spec/spec_helper.rb
|
259
264
|
- lib/ree_lib/packages/ree_date/.gitignore
|
260
265
|
- lib/ree_lib/packages/ree_date/.rspec
|