dbee 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/dbee/base.rb +27 -49
- data/lib/dbee/dsl/inflectable.rb +28 -0
- data/lib/dbee/dsl/reflectable.rb +25 -0
- data/lib/dbee/version.rb +1 -1
- data/spec/fixtures/models.rb +7 -2
- data/spec/fixtures/models.yaml +5 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e099b2ff5d651a4bc97562314e0216e07afa778cc7b99cc775a2461f744ab3a
|
4
|
+
data.tar.gz: 3d9ea4d091416484bdd5186caf02f9fec23e6d15d91fb9528965c9eff07f9669
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a31b7129beab7271692bc7ff3534ce53977d80a017d5fee250e13fa1f5c586d51cd867c39e72aa646265287b2ae152a5b8d824bdd849459fedfb67f6b240ae6
|
7
|
+
data.tar.gz: 5d0149dedb9a61efc00df03e37e454bfb53755a7ecdb8f8440751779e352b30e2f1f910a97f60df7bbbd3cd7f8111dbf2af1d2b93635a2dfc255e735cd3e2ebc
|
data/CHANGELOG.md
CHANGED
data/lib/dbee/base.rb
CHANGED
@@ -7,25 +7,29 @@
|
|
7
7
|
# LICENSE file in the root directory of this source tree.
|
8
8
|
#
|
9
9
|
|
10
|
+
require_relative 'dsl/inflectable'
|
11
|
+
require_relative 'dsl/reflectable'
|
12
|
+
|
10
13
|
module Dbee
|
11
14
|
# Instead of using the configuration-first approach, you could use this super class for
|
12
15
|
# Model declaration.
|
13
16
|
class Base
|
17
|
+
extend Dsl::Inflectable
|
18
|
+
extend Dsl::Reflectable
|
19
|
+
|
20
|
+
BASE_CLASS_CONSTANT = Dbee::Base
|
21
|
+
|
14
22
|
class << self
|
15
23
|
def partitioner(name, value)
|
16
24
|
partitioners << { name: name, value: value }
|
17
25
|
end
|
18
26
|
|
19
27
|
def table(name)
|
20
|
-
@table_name = name.to_s
|
21
|
-
|
22
|
-
self
|
28
|
+
tap { @table_name = name.to_s }
|
23
29
|
end
|
24
30
|
|
25
31
|
def association(name, opts = {})
|
26
|
-
associations_by_name[name.to_s] = opts.merge(name: name)
|
27
|
-
|
28
|
-
self
|
32
|
+
tap { associations_by_name[name.to_s] = opts.merge(name: name) }
|
29
33
|
end
|
30
34
|
|
31
35
|
# This method is cycle-resistant due to the fact that it is a requirement to send in a
|
@@ -34,7 +38,7 @@ module Dbee
|
|
34
38
|
# of a Query. This is not true for configuration-first Model definitions because, in that
|
35
39
|
# case, cycles do not exist since the nature of the configuration is flat.
|
36
40
|
def to_model(key_chain, name = nil, constraints = [], path_parts = [])
|
37
|
-
derived_name =
|
41
|
+
derived_name = name.to_s.empty? ? tableize(self.name) : name.to_s
|
38
42
|
key = [key_chain, derived_name, constraints, path_parts]
|
39
43
|
|
40
44
|
to_models[key] ||= Model.make(
|
@@ -64,79 +68,53 @@ module Dbee
|
|
64
68
|
end
|
65
69
|
|
66
70
|
def inherited_table_name
|
67
|
-
subclasses.find(&:table_name?)&.table_name ||
|
71
|
+
subclasses(BASE_CLASS_CONSTANT).find(&:table_name?)&.table_name ||
|
72
|
+
tableize(reversed_subclasses(BASE_CLASS_CONSTANT).first.name)
|
68
73
|
end
|
69
74
|
|
70
|
-
def
|
71
|
-
reversed_subclasses.each_with_object({}) do |subclass, memo|
|
75
|
+
def inherited_associations
|
76
|
+
reversed_subclasses(BASE_CLASS_CONSTANT).each_with_object({}) do |subclass, memo|
|
72
77
|
memo.merge!(subclass.associations_by_name)
|
73
|
-
end
|
78
|
+
end.values
|
74
79
|
end
|
75
80
|
|
76
81
|
def inherited_partitioners
|
77
|
-
reversed_subclasses.inject([]) do |memo, subclass|
|
82
|
+
reversed_subclasses(BASE_CLASS_CONSTANT).inject([]) do |memo, subclass|
|
78
83
|
memo + subclass.partitioners
|
79
84
|
end
|
80
85
|
end
|
81
86
|
|
82
87
|
private
|
83
88
|
|
84
|
-
def subclasses
|
85
|
-
ancestors.select { |a| a < Dbee::Base }
|
86
|
-
end
|
87
|
-
|
88
|
-
def reversed_subclasses
|
89
|
-
subclasses.reverse
|
90
|
-
end
|
91
|
-
|
92
89
|
def model_config(key_chain, name, constraints, path_parts)
|
93
90
|
{
|
94
91
|
constraints: constraints,
|
95
|
-
partitioners: inherited_partitioners,
|
96
92
|
models: associations(key_chain, path_parts),
|
97
93
|
name: name,
|
98
|
-
|
94
|
+
partitioners: inherited_partitioners,
|
95
|
+
table: inherited_table_name
|
99
96
|
}
|
100
97
|
end
|
101
98
|
|
102
|
-
def derive_name(name)
|
103
|
-
name.to_s.empty? ? inflected_name : name.to_s
|
104
|
-
end
|
105
|
-
|
106
|
-
def derive_table
|
107
|
-
inherited_table = inherited_table_name
|
108
|
-
|
109
|
-
inherited_table.empty? ? inflected_name : inherited_table
|
110
|
-
end
|
111
|
-
|
112
99
|
def associations(key_chain, path_parts)
|
113
|
-
|
114
|
-
|
115
|
-
.each_with_object([]) do |config, memo|
|
100
|
+
inherited_associations.select { |c| key_chain.ancestor_path?(path_parts, c[:name]) }
|
101
|
+
.each_with_object([]) do |config, memo|
|
116
102
|
model_constant = constantize(config[:model])
|
117
103
|
associated_constraints = config[:constraints]
|
118
104
|
name = config[:name]
|
119
105
|
|
120
|
-
memo << model_constant.to_model(
|
106
|
+
memo << model_constant.to_model(
|
107
|
+
key_chain,
|
108
|
+
name,
|
109
|
+
associated_constraints,
|
110
|
+
path_parts
|
111
|
+
)
|
121
112
|
end
|
122
113
|
end
|
123
114
|
|
124
115
|
def to_models
|
125
116
|
@to_models ||= {}
|
126
117
|
end
|
127
|
-
|
128
|
-
def inflected_name
|
129
|
-
name.split('::')
|
130
|
-
.last
|
131
|
-
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
132
|
-
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
133
|
-
.tr('-', '_')
|
134
|
-
.downcase
|
135
|
-
end
|
136
|
-
|
137
|
-
def constantize(value)
|
138
|
-
value.is_a?(String) || value.is_a?(Symbol) ? Object.const_get(value) : value
|
139
|
-
end
|
140
118
|
end
|
141
119
|
end
|
142
120
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
|
5
|
+
#
|
6
|
+
# This source code is licensed under the MIT license found in the
|
7
|
+
# LICENSE file in the root directory of this source tree.
|
8
|
+
#
|
9
|
+
|
10
|
+
module Dbee
|
11
|
+
module Dsl
|
12
|
+
# Provides methods for dealing with naming grammar.
|
13
|
+
module Inflectable
|
14
|
+
def constantize(value)
|
15
|
+
value.is_a?(String) || value.is_a?(Symbol) ? Object.const_get(value) : value
|
16
|
+
end
|
17
|
+
|
18
|
+
def tableize(value)
|
19
|
+
value.split('::')
|
20
|
+
.last
|
21
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
22
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
23
|
+
.tr('-', '_')
|
24
|
+
.downcase
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
|
5
|
+
#
|
6
|
+
# This source code is licensed under the MIT license found in the
|
7
|
+
# LICENSE file in the root directory of this source tree.
|
8
|
+
#
|
9
|
+
|
10
|
+
module Dbee
|
11
|
+
module Dsl
|
12
|
+
# Provide methods for dealing with introspection of class hierarchies.
|
13
|
+
module Reflectable
|
14
|
+
# Start at child, end with parent
|
15
|
+
def subclasses(base_class_constant)
|
16
|
+
ancestors.select { |a| a < base_class_constant }
|
17
|
+
end
|
18
|
+
|
19
|
+
# Start at parent, end with child
|
20
|
+
def reversed_subclasses(base_class_constant)
|
21
|
+
subclasses(base_class_constant).reverse
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/dbee/version.rb
CHANGED
data/spec/fixtures/models.rb
CHANGED
@@ -23,6 +23,8 @@ module Models
|
|
23
23
|
end
|
24
24
|
|
25
25
|
class MembersBase < Dbee::Base
|
26
|
+
table 'members'
|
27
|
+
|
26
28
|
association :demos, model: Demographics,
|
27
29
|
constraints: { type: :reference, name: :member_id, parent: :id }
|
28
30
|
|
@@ -55,6 +57,8 @@ module Models
|
|
55
57
|
end
|
56
58
|
|
57
59
|
class Theaters < TheatersBase
|
60
|
+
table 'theaters'
|
61
|
+
|
58
62
|
association :parent_theater, model: self, constraints: [
|
59
63
|
{ type: :reference, name: :id, parent: :parent_theater_id }
|
60
64
|
]
|
@@ -148,9 +152,10 @@ module PartitionerExamples
|
|
148
152
|
}
|
149
153
|
end
|
150
154
|
|
151
|
-
class
|
152
|
-
|
155
|
+
class Animals < Dbee::Base
|
156
|
+
end
|
153
157
|
|
158
|
+
class Dogs < Animals
|
154
159
|
partitioner :type, 'Dog'
|
155
160
|
|
156
161
|
partitioner :deleted, false
|
data/spec/fixtures/models.yaml
CHANGED
@@ -162,6 +162,7 @@ Readme:
|
|
162
162
|
value: fax
|
163
163
|
Cycle Example:
|
164
164
|
name: a
|
165
|
+
table: base_a
|
165
166
|
models:
|
166
167
|
- name: b1
|
167
168
|
table: b
|
@@ -169,18 +170,22 @@ Cycle Example:
|
|
169
170
|
- name: c
|
170
171
|
models:
|
171
172
|
- name: a
|
173
|
+
table: base_a
|
172
174
|
- name: d
|
173
175
|
models:
|
174
176
|
- name: a
|
177
|
+
table: base_a
|
175
178
|
- name: b2
|
176
179
|
table: b
|
177
180
|
models:
|
178
181
|
- name: c
|
179
182
|
models:
|
180
183
|
- name: a
|
184
|
+
table: base_a
|
181
185
|
- name: d
|
182
186
|
models:
|
183
187
|
- name: a
|
188
|
+
table: base_a
|
184
189
|
models:
|
185
190
|
- name: b1
|
186
191
|
table: b
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dbee
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Ruggio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: acts_as_hashable
|
@@ -155,6 +155,8 @@ files:
|
|
155
155
|
- dbee.gemspec
|
156
156
|
- lib/dbee.rb
|
157
157
|
- lib/dbee/base.rb
|
158
|
+
- lib/dbee/dsl/inflectable.rb
|
159
|
+
- lib/dbee/dsl/reflectable.rb
|
158
160
|
- lib/dbee/key_chain.rb
|
159
161
|
- lib/dbee/key_path.rb
|
160
162
|
- lib/dbee/model.rb
|