ree_lib 1.0.18 → 1.0.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64fb753de0bc41d6229e85e080565a7eb3cabce192a6cd89a3e07deb0add91f7
4
- data.tar.gz: b40c7415b989ea2ce5dd9c46b0a986a4672f8c91764b7ba9dc0604c3ab451e5c
3
+ metadata.gz: 1d8cd14e79d5afde9de80c29a5965a4ee4b6d4ca38c125b13395ffb3a5df478a
4
+ data.tar.gz: 41644392ed50e289e66d53e81d6b319d584baf9de2b16b81fb89b5678a4977f6
5
5
  SHA512:
6
- metadata.gz: f426600c0940004ac34514221aeac311edacd1e40221dcc030990350aa379fc43444a37995d2c1f3933929ecc4d611514e8127a66d6590518cb33f9a6011bc23
7
- data.tar.gz: dfc8728bf7b03c7826ba050ca1e992ef700236a79bf34116ccf30e66d2d8850cfb2e81161387aa512b731ba61c7a59bf7857fbce7e913d6e56d365636266fc9a
6
+ metadata.gz: 726b87bca18fbe71a47d1795da1cff4ca146a414d960aee747676710ad6003ae02b3fdc968fd37ef17e9ebba0d5af749620011d2664d06a0162b524d7f03e524
7
+ data.tar.gz: d46cf728e250cfd710bd9f651e4817e0dbf8f3a7a44fe5d797026d924c93b9808a6ef8ccd2887109015a2e656ffda86427ba335f8754385caf3b023d2cb95995
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ree_lib (1.0.18)
4
+ ree_lib (1.0.19)
5
5
  binding_of_caller (~> 1.0.0)
6
6
  i18n (~> 1.12.0)
7
7
  loofah (~> 2.18.0)
@@ -7,6 +7,9 @@
7
7
  "ree_dao"
8
8
  ],
9
9
  "depends_on": [
10
+ {
11
+ "name": "ree_array"
12
+ },
10
13
  {
11
14
  "name": "ree_dto"
12
15
  },
@@ -44,6 +47,14 @@
44
47
  "name": "connections",
45
48
  "schema": "packages/ree_dao/schemas/ree_dao/beans/connections.schema.json"
46
49
  },
50
+ {
51
+ "name": "one_to_many",
52
+ "schema": "packages/ree_dao/schemas/ree_dao/functions/one_to_many.schema.json"
53
+ },
54
+ {
55
+ "name": "one_to_one",
56
+ "schema": "packages/ree_dao/schemas/ree_dao/functions/one_to_one.schema.json"
57
+ },
47
58
  {
48
59
  "name": "persist_assoc",
49
60
  "schema": "packages/ree_dao/schemas/ree_dao/functions/persist_assoc.schema.json"
@@ -0,0 +1,51 @@
1
+ class ReeDao::OneToMany
2
+ include Ree::FnDSL
3
+
4
+ fn :one_to_many do
5
+ link :demodulize, from: :ree_string
6
+ link :underscore, from: :ree_string
7
+ link :group_by, from: :ree_array
8
+ end
9
+
10
+ contract(
11
+ ArrayOf[ -> (v) { v.class.ancestors.include?(ReeDto::EntityDSL) } ],
12
+ Sequel::Dataset,
13
+ Ksplat[
14
+ foreign_key?: Symbol,
15
+ assoc_setter?: Symbol
16
+ ] => nil
17
+ )
18
+ def call(list, assoc_dao, **opts)
19
+ return if list.empty?
20
+ root_ids = list.map(&:id)
21
+
22
+ assoc_setter = if opts.key?(:assoc_setter)
23
+ opts[:assoc_setter]
24
+ else
25
+ dto_class = assoc_dao
26
+ .opts[:schema_mapper]
27
+ .strategies
28
+ .detect {_1 .method == :db_load }
29
+ .output
30
+ .dto
31
+
32
+ name = underscore(demodulize(dto_class.name))
33
+ "set_#{name}s".to_sym
34
+ end
35
+
36
+ foreign_key = if opts.key?(:foreign_key)
37
+ opts[:foreign_key]
38
+ else
39
+ name = underscore(demodulize(list.first.class.name))
40
+ "#{name}_id".to_sym
41
+ end
42
+
43
+ assoc_by_group = group_by(assoc_dao.where(foreign_key => root_ids).all) { _1.send(foreign_key) }
44
+
45
+ list.each do |item|
46
+ item.send(assoc_setter, assoc_by_group[item.id] || [])
47
+ end
48
+
49
+ nil
50
+ end
51
+ end
@@ -0,0 +1,63 @@
1
+ class ReeDao::OneToOne
2
+ include Ree::FnDSL
3
+
4
+ fn :one_to_one do
5
+ link :demodulize, from: :ree_string
6
+ link :underscore, from: :ree_string
7
+ link :index_by, from: :ree_array
8
+ end
9
+
10
+ contract(
11
+ ArrayOf[ -> (v) { v.class.ancestors.include?(ReeDto::EntityDSL) } ],
12
+ Sequel::Dataset,
13
+ Ksplat[
14
+ reverse?: Bool,
15
+ foreign_key?: Symbol,
16
+ assoc_setter?: Symbol
17
+ ] => nil
18
+ )
19
+ def call(list, assoc_dao, **opts)
20
+ return if list.empty?
21
+
22
+ dto_class = assoc_dao
23
+ .opts[:schema_mapper]
24
+ .strategies
25
+ .detect {_1 .method == :db_load }
26
+ .output
27
+ .dto
28
+
29
+ assoc_name = underscore(demodulize(dto_class.name))
30
+ reverse = opts[:reverse]
31
+
32
+ assoc_setter = if opts.key?(:assoc_setter)
33
+ opts[:assoc_setter]
34
+ else
35
+ "set_#{assoc_name}"
36
+ end
37
+
38
+ foreign_key = if opts.key?(:foreign_key)
39
+ opts[:foreign_key]
40
+ else
41
+ if reverse
42
+ name = underscore(demodulize(list.first.class.name))
43
+ "#{name}_id".to_sym
44
+ else
45
+ :id
46
+ end
47
+ end
48
+
49
+ root_ids = if reverse
50
+ list.map(&:id)
51
+ else
52
+ list.map(&:"#{foreign_key}")
53
+ end
54
+
55
+ assoc_by_fk = index_by(assoc_dao.where(foreign_key => root_ids).all) { _1.send(foreign_key) }
56
+
57
+ list.each do |item|
58
+ item.send(assoc_setter, assoc_by_fk[item.id])
59
+ end
60
+
61
+ nil
62
+ end
63
+ end
@@ -6,6 +6,7 @@ module ReeDao
6
6
  include Ree::PackageDSL
7
7
 
8
8
  package do
9
+ depends_on :ree_array
9
10
  depends_on :ree_dto
10
11
  depends_on :ree_enum
11
12
  depends_on :ree_mapper
@@ -0,0 +1,58 @@
1
+ {
2
+ "schema_type": "object",
3
+ "schema_version": "1.0",
4
+ "name": "one_to_many",
5
+ "path": "packages/ree_dao/package/ree_dao/functions/one_to_many.rb",
6
+ "mount_as": "fn",
7
+ "class": "ReeDao::OneToMany",
8
+ "factory": null,
9
+ "methods": [
10
+ {
11
+ "doc": "",
12
+ "throws": [
13
+
14
+ ],
15
+ "return": "Any",
16
+ "args": [
17
+ {
18
+ "arg": "list",
19
+ "type": "ArrayOf[Proc#call]"
20
+ },
21
+ {
22
+ "arg": "assoc_dao",
23
+ "type": "Sequel::Dataset"
24
+ },
25
+ {
26
+ "arg": "opts",
27
+ "type": "Ksplat[:foreign_key? => Symbol, :assoc_setter? => 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": "group_by",
43
+ "package_name": "ree_array",
44
+ "as": "group_by",
45
+ "imports": [
46
+
47
+ ]
48
+ },
49
+ {
50
+ "target": "underscore",
51
+ "package_name": "ree_string",
52
+ "as": "underscore",
53
+ "imports": [
54
+
55
+ ]
56
+ }
57
+ ]
58
+ }
@@ -0,0 +1,58 @@
1
+ {
2
+ "schema_type": "object",
3
+ "schema_version": "1.0",
4
+ "name": "one_to_one",
5
+ "path": "packages/ree_dao/package/ree_dao/functions/one_to_one.rb",
6
+ "mount_as": "fn",
7
+ "class": "ReeDao::OneToOne",
8
+ "factory": null,
9
+ "methods": [
10
+ {
11
+ "doc": "",
12
+ "throws": [
13
+
14
+ ],
15
+ "return": "Any",
16
+ "args": [
17
+ {
18
+ "arg": "list",
19
+ "type": "ArrayOf[Proc#call]"
20
+ },
21
+ {
22
+ "arg": "assoc_dao",
23
+ "type": "Sequel::Dataset"
24
+ },
25
+ {
26
+ "arg": "opts",
27
+ "type": "Ksplat[:reverse? => Bool, :foreign_key? => Symbol, :assoc_setter? => 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": "index_by",
43
+ "package_name": "ree_array",
44
+ "as": "index_by",
45
+ "imports": [
46
+
47
+ ]
48
+ },
49
+ {
50
+ "target": "underscore",
51
+ "package_name": "ree_string",
52
+ "as": "underscore",
53
+ "imports": [
54
+
55
+ ]
56
+ }
57
+ ]
58
+ }
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal = true
2
+
3
+ RSpec.describe :one_to_many do
4
+ link :one_to_many, from: :ree_dao
5
+ link :persist_assoc, from: :ree_dao
6
+ link :build_sqlite_connection, from: :ree_dao
7
+
8
+ let(:connection) { build_sqlite_connection({database: 'sqlite_db'}) }
9
+
10
+ before :all do
11
+ Ree.enable_irb_mode
12
+ end
13
+
14
+ before :each do
15
+ [:projects, :project_users].each do |name|
16
+ if connection.table_exists?(name)
17
+ connection.drop_table(name)
18
+ end
19
+ end
20
+
21
+ connection.create_table :projects do
22
+ primary_key :id
23
+ end
24
+
25
+ connection.create_table :project_users do
26
+ primary_key :id
27
+ foreign_key :project_id, :projects, null: false, on_delete: :cascade
28
+ end
29
+
30
+ connection.disconnect
31
+ end
32
+
33
+ after do
34
+ Ree.disable_irb_mode
35
+ connection.disconnect
36
+ end
37
+
38
+ it {
39
+ module TestOneToMany
40
+ include Ree::PackageDSL
41
+
42
+ package do
43
+ depends_on :ree_dao
44
+ end
45
+
46
+ class Db
47
+ include Ree::BeanDSL
48
+
49
+ bean :db do
50
+ singleton
51
+ factory :build
52
+
53
+ link :build_sqlite_connection, from: :ree_dao
54
+ end
55
+
56
+ def build
57
+ build_sqlite_connection({database: 'sqlite_db'})
58
+ end
59
+ end
60
+
61
+ class Project
62
+ include ReeDto::EntityDSL
63
+
64
+ properties(
65
+ id: Nilor[Integer]
66
+ )
67
+
68
+ def project_users
69
+ @project_users ||= []
70
+ end
71
+
72
+ def set_project_users(list)
73
+ @project_users = list
74
+ end
75
+
76
+ def add_project_user(pu)
77
+ project_users.push(pu)
78
+ end
79
+ end
80
+
81
+ class ProjectUser
82
+ include ReeDto::EntityDSL
83
+
84
+ properties(
85
+ id: Nilor[Integer],
86
+ project_id: Nilor[Integer]
87
+ )
88
+
89
+ contract Integer => Integer
90
+ def project_id=(id)
91
+ @project_id = id
92
+ end
93
+ end
94
+
95
+ class ProjectsDao
96
+ include ReeDao::DSL
97
+
98
+ dao :projects_dao do
99
+ link :db
100
+ end
101
+
102
+ schema Project do
103
+ integer :id, null: true
104
+ end
105
+ end
106
+
107
+ class ProjectUsersDao
108
+ include ReeDao::DSL
109
+
110
+ dao :project_users_dao do
111
+ link :db
112
+ end
113
+
114
+ schema ProjectUser do
115
+ integer :id, null: true
116
+ integer :project_id
117
+ end
118
+ end
119
+ end
120
+
121
+ project = TestOneToMany::Project.new(id: 1)
122
+
123
+ project.add_project_user(
124
+ TestOneToMany::ProjectUser.new
125
+ )
126
+
127
+ project.add_project_user(
128
+ TestOneToMany::ProjectUser.new
129
+ )
130
+
131
+ TestOneToMany::ProjectsDao.new.put(project)
132
+ persist_assoc(project, TestOneToMany::ProjectUsersDao.new)
133
+
134
+ project_users_dao = TestOneToMany::ProjectUsersDao.new
135
+
136
+ project.set_project_users([])
137
+ one_to_many([project], project_users_dao)
138
+
139
+ expect(project.project_users.size).to eq(2)
140
+
141
+ project.set_project_users([])
142
+
143
+ one_to_many(
144
+ [project],
145
+ project_users_dao,
146
+ foreign_key: :project_id,
147
+ assoc_setter: :set_project_users
148
+ )
149
+
150
+ expect(project.project_users.size).to eq(2)
151
+ }
152
+ end
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal = true
2
+
3
+ RSpec.describe :one_to_one do
4
+ link :one_to_one, from: :ree_dao
5
+ link :persist_assoc, from: :ree_dao
6
+ link :build_sqlite_connection, from: :ree_dao
7
+
8
+ let(:connection) { build_sqlite_connection({database: 'sqlite_db'}) }
9
+
10
+ before :all do
11
+ Ree.enable_irb_mode
12
+ end
13
+
14
+ before :each do
15
+ [:projects, :project_users].each do |name|
16
+ if connection.table_exists?(name)
17
+ connection.drop_table(name)
18
+ end
19
+ end
20
+
21
+ connection.create_table :projects do
22
+ primary_key :id
23
+ end
24
+
25
+ connection.create_table :project_users do
26
+ primary_key :id
27
+ foreign_key :project_id, :projects, null: false, on_delete: :cascade
28
+ end
29
+
30
+ connection.alter_table :projects do
31
+ add_foreign_key :project_user_id, :project_users, on_delete: :cascade
32
+ end
33
+
34
+ connection.disconnect
35
+ end
36
+
37
+ after do
38
+ Ree.disable_irb_mode
39
+ connection.disconnect
40
+ end
41
+
42
+ it {
43
+ module TestOneToOne
44
+ include Ree::PackageDSL
45
+
46
+ package do
47
+ depends_on :ree_dao
48
+ end
49
+
50
+ class Db
51
+ include Ree::BeanDSL
52
+
53
+ bean :db do
54
+ singleton
55
+ factory :build
56
+
57
+ link :build_sqlite_connection, from: :ree_dao
58
+ end
59
+
60
+ def build
61
+ build_sqlite_connection({database: 'sqlite_db'})
62
+ end
63
+ end
64
+
65
+ class Project
66
+ include ReeDto::EntityDSL
67
+
68
+ properties(
69
+ id: Nilor[Integer]
70
+ )
71
+
72
+ def project_user
73
+ @project_user
74
+ end
75
+
76
+ def project_user_id=(id)
77
+ @project_user_id = id
78
+ end
79
+
80
+ attr_reader :project_user_id
81
+
82
+ def set_project_user(pu)
83
+ @project_user = pu
84
+ end
85
+
86
+ def project_users
87
+ [@project_user]
88
+ end
89
+ end
90
+
91
+ class ProjectUser
92
+ include ReeDto::EntityDSL
93
+
94
+ properties(
95
+ id: Nilor[Integer],
96
+ project_id: Nilor[Integer]
97
+ )
98
+
99
+ contract Integer => Integer
100
+ def project_id=(id)
101
+ @project_id = id
102
+ end
103
+ end
104
+
105
+ class ProjectsDao
106
+ include ReeDao::DSL
107
+
108
+ dao :projects_dao do
109
+ link :db
110
+ end
111
+
112
+ schema Project do
113
+ integer :id, null: true
114
+ integer :project_user_id, null: true
115
+ end
116
+ end
117
+
118
+ class ProjectUsersDao
119
+ include ReeDao::DSL
120
+
121
+ dao :project_users_dao do
122
+ link :db
123
+ end
124
+
125
+ schema ProjectUser do
126
+ integer :id, null: true
127
+ integer :project_id
128
+ end
129
+ end
130
+ end
131
+
132
+ project = TestOneToOne::Project.new(id: 1)
133
+
134
+ project.set_project_user(
135
+ TestOneToOne::ProjectUser.new
136
+ )
137
+
138
+ TestOneToOne::ProjectsDao.new.put(project)
139
+ persist_assoc(project, TestOneToOne::ProjectUsersDao.new)
140
+
141
+ project.project_user_id = project.project_users.first.id
142
+ persist_assoc(project, TestOneToOne::ProjectUsersDao.new)
143
+
144
+ project_users_dao = TestOneToOne::ProjectUsersDao.new
145
+
146
+ project.set_project_user(nil)
147
+ one_to_one([project], project_users_dao)
148
+
149
+ expect(project.project_user.project_id).to eq(project.id)
150
+
151
+ project.set_project_user(nil)
152
+
153
+ one_to_one(
154
+ [project],
155
+ project_users_dao,
156
+ reverse: true,
157
+ foreign_key: :project_id,
158
+ assoc_setter: :set_project_user
159
+ )
160
+
161
+ expect(project.project_user.project_id).to eq(project.id)
162
+
163
+ project.set_project_user(nil)
164
+
165
+ one_to_one(
166
+ [project],
167
+ project_users_dao,
168
+ reverse: true,
169
+ assoc_setter: :set_project_user
170
+ )
171
+
172
+ expect(project.project_user.project_id).to eq(project.id)
173
+ }
174
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReeLib
4
- VERSION = "1.0.18"
4
+ VERSION = "1.0.19"
5
5
  end
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.18
4
+ version: 1.0.19
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-19 00:00:00.000000000 Z
11
+ date: 2022-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ree
@@ -248,15 +248,21 @@ 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/one_to_many.rb
252
+ - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/one_to_one.rb
251
253
  - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
252
254
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/beans/connections.schema.json
253
255
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_connection.schema.json
254
256
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_dao.schema.json
255
257
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_pg_connection.schema.json
256
258
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_sqlite_connection.schema.json
259
+ - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/one_to_many.schema.json
260
+ - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/one_to_one.schema.json
257
261
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/persist_assoc.schema.json
258
262
  - lib/ree_lib/packages/ree_dao/spec/package_schema_spec.rb
259
263
  - lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/build_sqlite_connection_spec.rb
264
+ - lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/one_to_many_spec.rb
265
+ - lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/one_to_one_spec.rb
260
266
  - lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/persist_assoc_spec.rb
261
267
  - lib/ree_lib/packages/ree_dao/spec/spec_helper.rb
262
268
  - lib/ree_lib/packages/ree_date/.gitignore