horza 0.3.2 → 0.3.3
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/lib/horza/adapters/active_record.rb +3 -2
- data/lib/horza/adapters/class_methods.rb +19 -1
- data/lib/horza/configuration.rb +5 -1
- data/lib/horza/errors.rb +3 -0
- data/spec/active_record_spec.rb +58 -4
- data/spec/horza_spec.rb +22 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0883d16fa80f945d84b29826a535c76dadbfddbe
|
4
|
+
data.tar.gz: 75f469f6ac54c23051223aa6eb017dff8bda177f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92889e2bc57bab4165a62b1ad6d9440233593127018efb198ccc491272c2fa6de56591ed1276b4784c1116fe1c0aecdc37fd876b12a99c583648d20af7190941
|
7
|
+
data.tar.gz: 6c7c8d10d9c2cc2ef85710055418a872db213caea2fb95e5c034c505ce4e1065fec4cb112733a54c006c325a4a6f48c8702ebdb362451ca15a61ad01c5338907
|
@@ -2,12 +2,13 @@ module Horza
|
|
2
2
|
module Adapters
|
3
3
|
class ActiveRecord < AbstractAdapter
|
4
4
|
INVALID_ANCESTRY_MSG = 'Invalid relation. Ensure that the plurality of your associations is correct.'
|
5
|
+
CONTEXT_NAMESPACE = ::ActiveRecord::Base
|
5
6
|
|
6
7
|
class << self
|
7
8
|
def entity_context_map
|
8
9
|
# Rails doesn't preload classes in development mode, caching doesn't make sense
|
9
|
-
return ::Horza.descendants_map(
|
10
|
-
@map ||= ::Horza.descendants_map(
|
10
|
+
return ::Horza.descendants_map(CONTEXT_NAMESPACE) if ::Horza.configuration.development_mode
|
11
|
+
@map ||= ::Horza.descendants_map(CONTEXT_NAMESPACE)
|
11
12
|
end
|
12
13
|
|
13
14
|
def expected_errors_map
|
@@ -14,7 +14,25 @@ module Horza
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def context_for_entity(entity)
|
17
|
-
entity_context_map[entity]
|
17
|
+
context = entity_context_map[entity]
|
18
|
+
return context if context
|
19
|
+
|
20
|
+
lazy_load_model(entity)
|
21
|
+
end
|
22
|
+
|
23
|
+
def lazy_load_model(entity)
|
24
|
+
raise Horza::Errors::NoContextForEntity.new unless Horza.configuration.development_mode
|
25
|
+
const = entity.to_s.camelize
|
26
|
+
|
27
|
+
[Object].concat(Horza.configuration.namespaces).each do |namespace|
|
28
|
+
begin
|
29
|
+
return namespace.const_get(const)
|
30
|
+
rescue NameError
|
31
|
+
next
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
raise Horza::Errors::NoContextForEntity.new
|
18
36
|
end
|
19
37
|
|
20
38
|
def not_implemented_error
|
data/lib/horza/configuration.rb
CHANGED
data/lib/horza/errors.rb
CHANGED
data/spec/active_record_spec.rb
CHANGED
@@ -42,6 +42,14 @@ else
|
|
42
42
|
belongs_to :employer
|
43
43
|
end
|
44
44
|
end
|
45
|
+
|
46
|
+
class SimpleRecord < ActiveRecord::Base
|
47
|
+
end
|
48
|
+
|
49
|
+
module Lazy
|
50
|
+
class LazyRecord < ActiveRecord::Base
|
51
|
+
end
|
52
|
+
end
|
45
53
|
end
|
46
54
|
|
47
55
|
describe Horza do
|
@@ -60,8 +68,22 @@ describe Horza do
|
|
60
68
|
end
|
61
69
|
|
62
70
|
context '#context_for_entity' do
|
63
|
-
|
64
|
-
|
71
|
+
context 'when model exists' do
|
72
|
+
it 'Returns correct class' do
|
73
|
+
expect(Horza.adapter.context_for_entity(:simple_record)).to eq SimpleRecord
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'when model exists in namespace' do
|
78
|
+
it 'Returns correct class' do
|
79
|
+
expect(Horza.adapter.context_for_entity(:user)).to eq HorzaSpec::User
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when model does not exist' do
|
84
|
+
it 'throws error class' do
|
85
|
+
expect { Horza.adapter.context_for_entity(:not_a_thing) }.to raise_error Horza::Errors::NoContextForEntity
|
86
|
+
end
|
65
87
|
end
|
66
88
|
|
67
89
|
context 'in development mode' do
|
@@ -75,8 +97,40 @@ describe Horza do
|
|
75
97
|
after do
|
76
98
|
Horza.reset
|
77
99
|
end
|
78
|
-
|
79
|
-
|
100
|
+
|
101
|
+
context 'when model exists' do
|
102
|
+
it 'Returns correct class' do
|
103
|
+
expect(Horza.adapter.context_for_entity(:simple_record)).to eq SimpleRecord
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'when model does not exist' do
|
108
|
+
it 'throws error class' do
|
109
|
+
expect { Horza.adapter.context_for_entity(:not_a_thing) }.to raise_error Horza::Errors::NoContextForEntity
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when model exists but has been lazy loaded' do
|
114
|
+
context 'without namespace' do
|
115
|
+
before do
|
116
|
+
Object.send(:remove_const, :SimpleRecord)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'lazy loads' do
|
120
|
+
expect(Horza.adapter.context_for_entity(:simple_record).to_s).to eq 'SimpleRecord'
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'within namespace' do
|
125
|
+
before do
|
126
|
+
Lazy.send(:remove_const, :LazyRecord)
|
127
|
+
Object.send(:remove_const, :Lazy)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'lazy loads' do
|
131
|
+
expect(Horza.adapter.context_for_entity(:lazy_record).to_s).to eq 'Lazy::LazyRecord'
|
132
|
+
end
|
133
|
+
end
|
80
134
|
end
|
81
135
|
end
|
82
136
|
end
|
data/spec/horza_spec.rb
CHANGED
@@ -21,6 +21,28 @@ describe Horza do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
24
|
+
|
25
|
+
context '#configuration #namespaces' do
|
26
|
+
context 'when namespaces are not configured' do
|
27
|
+
before { Horza.reset }
|
28
|
+
after { Horza.reset }
|
29
|
+
it 'returns empty array' do
|
30
|
+
expect(Horza.configuration.namespaces.is_a? Array).to be true
|
31
|
+
expect(Horza.configuration.namespaces.empty?).to be true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when namespaces are configured' do
|
36
|
+
before do
|
37
|
+
Horza.reset
|
38
|
+
Horza.configure { |config| config.namespaces = [HorzaSpec] }
|
39
|
+
end
|
40
|
+
after { Horza.reset }
|
41
|
+
it 'returns configured namespaces class' do
|
42
|
+
expect(Horza.configuration.namespaces).to eq [HorzaSpec]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
24
46
|
end
|
25
47
|
|
26
48
|
describe Horza::Adapters::Options do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: horza
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Turner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|