vorpal 0.1.0.rc2 → 0.1.0.rc3
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/README.md +1 -1
- data/lib/vorpal/configs.rb +3 -3
- data/lib/vorpal/db_driver.rb +23 -15
- data/lib/vorpal/db_loader.rb +1 -1
- data/lib/vorpal/dsl/config_builder.rb +1 -1
- data/lib/vorpal/dsl/defaults_generator.rb +2 -2
- data/lib/vorpal/loaded_objects.rb +1 -1
- data/lib/vorpal/util/array_hash.rb +13 -11
- data/lib/vorpal/util/hash_initialization.rb +7 -5
- data/lib/vorpal/util/string_utils.rb +15 -0
- data/lib/vorpal/version.rb +1 -1
- data/spec/vorpal/integration/db_driver_spec.rb +28 -2
- data/spec/vorpal/unit/dsl/defaults_generator_spec.rb +2 -2
- data/spec/vorpal/unit/util/string_utils_spec.rb +25 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 824912a10087771cda48b9ad66dac75861e80bb1
|
4
|
+
data.tar.gz: 67ff5e87b3e45c8308866036e79cfb83ae5959d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3db514381c185a0dc4b9b4a408ca35b34a244bb1fe97e899ff1b89ebb84d14cd20d5159c2a459316d77c1e3b4d81e4529e0ff110d02dfdece3f06817531653b1
|
7
|
+
data.tar.gz: 5437e2611a1ae1e75b1143cd1f9732429fa9acb0fb5e11fc105e60bb7d0330fe4e23021ca7c63b676758977fedeafaed2b39f26be24f83b1f431fc5c7ab1a0b2
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Vorpal
|
1
|
+
# Vorpal [](https://travis-ci.org/nulogy/vorpal) [](https://codeclimate.com/github/nulogy/vorpal)
|
2
2
|
|
3
3
|
Separate your domain model from your persistence mechanism. Some problems call for a really sharp tool.
|
4
4
|
|
data/lib/vorpal/configs.rb
CHANGED
@@ -266,7 +266,7 @@ module Vorpal
|
|
266
266
|
|
267
267
|
# @private
|
268
268
|
class HasManyConfig
|
269
|
-
include HashInitialization
|
269
|
+
include Util::HashInitialization
|
270
270
|
include RemoteEndConfig
|
271
271
|
include ToManyConfig
|
272
272
|
|
@@ -276,7 +276,7 @@ module Vorpal
|
|
276
276
|
|
277
277
|
# @private
|
278
278
|
class HasOneConfig
|
279
|
-
include HashInitialization
|
279
|
+
include Util::HashInitialization
|
280
280
|
include RemoteEndConfig
|
281
281
|
include ToOneConfig
|
282
282
|
|
@@ -286,7 +286,7 @@ module Vorpal
|
|
286
286
|
|
287
287
|
# @private
|
288
288
|
class BelongsToConfig
|
289
|
-
include HashInitialization
|
289
|
+
include Util::HashInitialization
|
290
290
|
include LocalEndConfig
|
291
291
|
include ToOneConfig
|
292
292
|
|
data/lib/vorpal/db_driver.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'vorpal/util/string_utils.rb'
|
2
|
+
|
1
3
|
module Vorpal
|
2
4
|
# Interface between the database and Vorpal
|
3
5
|
#
|
@@ -57,27 +59,33 @@ module Vorpal
|
|
57
59
|
|
58
60
|
# Builds an ORM Class for accessing data in the given DB table.
|
59
61
|
#
|
62
|
+
# @param model_class [Class] The PORO class that we are creating a DB interface class for.
|
60
63
|
# @param table_name [String] Name of the DB table the DB class should interface with.
|
61
64
|
# @return [Class] ActiveRecord::Base Class
|
62
|
-
def build_db_class(table_name)
|
65
|
+
def build_db_class(model_class, table_name)
|
63
66
|
db_class = Class.new(ActiveRecord::Base) do
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
67
|
+
class << self
|
68
|
+
# This is overridden for two reasons:
|
69
|
+
# 1) For anonymous classes, #name normally returns nil. Class names in Ruby come from the
|
70
|
+
# name of the constant they are assigned to.
|
71
|
+
# 2) Because the default implementation for Class#name for anonymous classes is very, very
|
72
|
+
# slow. https://bugs.ruby-lang.org/issues/11119
|
73
|
+
# Remove this override once #2 has been fixed!
|
74
|
+
def name
|
75
|
+
@name ||= "Vorpal_generated_ActiveRecord__Base_class_for_#{vorpal_model_class_name}"
|
76
|
+
end
|
77
|
+
|
78
|
+
# Overridden because, like #name, the default implementation for anonymous classes is very,
|
79
|
+
# very slow.
|
80
|
+
def to_s
|
81
|
+
name
|
82
|
+
end
|
83
|
+
|
84
|
+
attr_accessor :vorpal_model_class_name
|
78
85
|
end
|
79
86
|
end
|
80
87
|
|
88
|
+
db_class.vorpal_model_class_name = Util::StringUtils.escape_class_name(model_class.name)
|
81
89
|
db_class.table_name = table_name
|
82
90
|
db_class
|
83
91
|
end
|
data/lib/vorpal/db_loader.rb
CHANGED
@@ -12,7 +12,7 @@ module Vorpal
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def build_db_class(user_table_name)
|
15
|
-
@db_driver.build_db_class(user_table_name || table_name)
|
15
|
+
@db_driver.build_db_class(@domain_class, user_table_name || table_name)
|
16
16
|
end
|
17
17
|
|
18
18
|
def table_name
|
@@ -42,4 +42,4 @@ module Vorpal
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
-
end
|
45
|
+
end
|
@@ -1,17 +1,19 @@
|
|
1
1
|
module Vorpal
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
array_hash[key]
|
2
|
+
module Util
|
3
|
+
# @private
|
4
|
+
module ArrayHash
|
5
|
+
def add_to_hash(array_hash, key, values)
|
6
|
+
if array_hash[key].nil? || array_hash[key].empty?
|
7
|
+
array_hash[key] = []
|
8
|
+
end
|
9
|
+
array_hash[key].concat(Array(values))
|
7
10
|
end
|
8
|
-
array_hash[key].concat(Array(values))
|
9
|
-
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def pop(array_hash)
|
13
|
+
key = array_hash.first.first
|
14
|
+
values = array_hash.delete(key)
|
15
|
+
[key, values]
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
@@ -1,9 +1,11 @@
|
|
1
1
|
module Vorpal
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
attrs
|
6
|
-
|
2
|
+
module Util
|
3
|
+
# @private
|
4
|
+
module HashInitialization
|
5
|
+
def initialize(attrs)
|
6
|
+
attrs.each do |k,v|
|
7
|
+
instance_variable_set("@#{k}", v)
|
8
|
+
end
|
7
9
|
end
|
8
10
|
end
|
9
11
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Vorpal
|
2
|
+
module Util
|
3
|
+
# @private
|
4
|
+
module StringUtils
|
5
|
+
extend self
|
6
|
+
|
7
|
+
# Escapes the name of a class so that it can be embedded into the name
|
8
|
+
# of another class. This means that the result should always be `#const_defined?`
|
9
|
+
# friendly.
|
10
|
+
def escape_class_name(class_name)
|
11
|
+
class_name.gsub("::", "__")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/vorpal/version.rb
CHANGED
@@ -3,14 +3,40 @@ require 'vorpal'
|
|
3
3
|
|
4
4
|
describe Vorpal::DbDriver do
|
5
5
|
describe '#build_db_class' do
|
6
|
-
let(:db_class) { subject.build_db_class('example') }
|
6
|
+
let(:db_class) { subject.build_db_class(DbDriverSpec::Foo, 'example') }
|
7
7
|
|
8
|
-
it 'generates a
|
8
|
+
it 'generates a valid class name so that rails auto-reloading works' do
|
9
9
|
expect { Vorpal.const_defined?(db_class.name) }.to_not raise_error
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'does not let the user access the generated class' do
|
13
13
|
expect { Vorpal.const_get(db_class.name) }.to raise_error(NameError)
|
14
14
|
end
|
15
|
+
|
16
|
+
it 'isolates two POROs that map to the same db table' do
|
17
|
+
db_class1 = build_db_class(DbDriverSpec::Foo)
|
18
|
+
db_class2 = build_db_class(DbDriverSpec::Bar)
|
19
|
+
|
20
|
+
expect(db_class1).to_not eq(db_class2)
|
21
|
+
expect(db_class1.name).to_not eq(db_class2.name)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'uses the model class name to make the generated AR::Base class name unique' do
|
25
|
+
db_class = build_db_class(DbDriverSpec::Foo)
|
26
|
+
|
27
|
+
expect(db_class.name).to match("DbDriverSpec__Foo")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
module DbDriverSpec
|
34
|
+
class Foo; end
|
35
|
+
class Bar; end
|
36
|
+
end
|
37
|
+
|
38
|
+
def build_db_class(clazz)
|
39
|
+
db_driver = Vorpal::DbDriver.new
|
40
|
+
db_driver.build_db_class(clazz, 'example')
|
15
41
|
end
|
16
42
|
end
|
@@ -16,14 +16,14 @@ describe Vorpal::Dsl::DefaultsGenerator do
|
|
16
16
|
describe '#build_db_class' do
|
17
17
|
it 'derives the table_name from the domain class name' do
|
18
18
|
generator = build_generator(Tester)
|
19
|
-
expect(db_driver).to receive(:build_db_class).with("testers")
|
19
|
+
expect(db_driver).to receive(:build_db_class).with(Tester, "testers")
|
20
20
|
|
21
21
|
generator.build_db_class(nil)
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'specifies the table_name manually' do
|
25
25
|
generator = build_generator(Tester)
|
26
|
-
expect(db_driver).to receive(:build_db_class).with("override")
|
26
|
+
expect(db_driver).to receive(:build_db_class).with(Tester, "override")
|
27
27
|
|
28
28
|
generator.build_db_class("override")
|
29
29
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'unit_spec_helper'
|
2
|
+
require 'vorpal/util/string_utils'
|
3
|
+
|
4
|
+
describe Vorpal::Util::StringUtils do
|
5
|
+
|
6
|
+
describe '.escape_class_name' do
|
7
|
+
it 'does nothing when there are no :: in the class name' do
|
8
|
+
expect(escape_class_name("Foo")).to eq("Foo")
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'converts :: into __' do
|
12
|
+
expect(escape_class_name("Foo::Bar")).to eq("Foo__Bar")
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'converts multiple :: into __' do
|
16
|
+
expect(escape_class_name("Foo::Bar::Baz")).to eq("Foo__Bar__Baz")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def escape_class_name(class_name)
|
23
|
+
Vorpal::Util::StringUtils.escape_class_name(class_name)
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vorpal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.rc3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Kirby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple_serializer
|
@@ -170,6 +170,7 @@ files:
|
|
170
170
|
- lib/vorpal/loaded_objects.rb
|
171
171
|
- lib/vorpal/util/array_hash.rb
|
172
172
|
- lib/vorpal/util/hash_initialization.rb
|
173
|
+
- lib/vorpal/util/string_utils.rb
|
173
174
|
- lib/vorpal/version.rb
|
174
175
|
- spec/helpers/db_helpers.rb
|
175
176
|
- spec/helpers/profile_helpers.rb
|
@@ -184,6 +185,7 @@ files:
|
|
184
185
|
- spec/vorpal/unit/dsl/defaults_generator_spec.rb
|
185
186
|
- spec/vorpal/unit/identity_map_spec.rb
|
186
187
|
- spec/vorpal/unit/loaded_objects_spec.rb
|
188
|
+
- spec/vorpal/unit/util/string_utils_spec.rb
|
187
189
|
- vorpal.gemspec
|
188
190
|
homepage: https://github.com/nulogy/vorpal
|
189
191
|
licenses:
|
@@ -223,3 +225,4 @@ test_files:
|
|
223
225
|
- spec/vorpal/unit/dsl/defaults_generator_spec.rb
|
224
226
|
- spec/vorpal/unit/identity_map_spec.rb
|
225
227
|
- spec/vorpal/unit/loaded_objects_spec.rb
|
228
|
+
- spec/vorpal/unit/util/string_utils_spec.rb
|