dbee 1.2.0 → 1.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae136319b1de6f828e34e6e0546b7c6663848efecbf8bfb42f8fe6204bebb20c
4
- data.tar.gz: 1690b0f58befeade514c2949163d37ada74f673235ab64a49ec18f7ffde58e1d
3
+ metadata.gz: 4e099b2ff5d651a4bc97562314e0216e07afa778cc7b99cc775a2461f744ab3a
4
+ data.tar.gz: 3d9ea4d091416484bdd5186caf02f9fec23e6d15d91fb9528965c9eff07f9669
5
5
  SHA512:
6
- metadata.gz: 82c42cf23dbaed18644da3b2e02bc406833a65ca6429c8d31d824de44cf08fe2b503c4af32c6199646fbe797abe7af9dc434d75acff91b1fb7b502d2f7eb6d35
7
- data.tar.gz: '07482747b169ccfde19405381d5128bd07ee269374e75c0f577ee34c26b3a0d877654c70da3a0b1bdd8ce31a1e591a4d0d8c11050390b3a5be18e06f7b312b11'
6
+ metadata.gz: 7a31b7129beab7271692bc7ff3534ce53977d80a017d5fee250e13fa1f5c586d51cd867c39e72aa646265287b2ae152a5b8d824bdd849459fedfb67f6b240ae6
7
+ data.tar.gz: 5d0149dedb9a61efc00df03e37e454bfb53755a7ecdb8f8440751779e352b30e2f1f910a97f60df7bbbd3cd7f8111dbf2af1d2b93635a2dfc255e735cd3e2ebc
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 1.2.1 (September 1st, 2019)
2
+
3
+ Fixes:
4
+
5
+ * Dbee::Base table name should default to the most-parent table name (if none are explicitly defined.) Overriding still takes most child sub-class precedence.
6
+
1
7
  # 1.2.0 (August 29th, 2019)
2
8
 
3
9
  Additions:
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 = derive_name(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 inherited_associations_by_name
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
- table: derive_table
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
- inherited_associations_by_name.values
114
- .select { |c| key_chain.ancestor_path?(path_parts, c[:name]) }
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(key_chain, name, associated_constraints, path_parts)
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
@@ -8,5 +8,5 @@
8
8
  #
9
9
 
10
10
  module Dbee
11
- VERSION = '1.2.0'
11
+ VERSION = '1.2.1'
12
12
  end
@@ -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 Dogs < Dbee::Base
152
- table 'animals'
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
@@ -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.0
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-08-29 00:00:00.000000000 Z
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