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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6ac47a52b8609da227092dccc5f1f27ce93d75e
4
- data.tar.gz: 22e4b62eb4932aa58c60382cd9a85a4120cc80d2
3
+ metadata.gz: 0883d16fa80f945d84b29826a535c76dadbfddbe
4
+ data.tar.gz: 75f469f6ac54c23051223aa6eb017dff8bda177f
5
5
  SHA512:
6
- metadata.gz: 376fe6bdeb618ac8952027c399a0ba0065269472e888c72b9e6cfe1c74e746e7186b23a7786bec125d54fbf3b42a26cbdfb24b0938591c70a8983a5006261b12
7
- data.tar.gz: 3aa106021cdb7be6251a9d05ee48492611fe1d78dbac73efee1a38ef26d6382843ac442b471cbd909ec4d65ff029154e4aa621c95124176bbd8a06b52c07e9b3
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(::ActiveRecord::Base) if ::Horza.configuration.development_mode
10
- @map ||= ::Horza.descendants_map(::ActiveRecord::Base)
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
@@ -26,6 +26,10 @@ module Horza
26
26
  end
27
27
 
28
28
  class Config
29
- attr_accessor :adapter, :development_mode
29
+ attr_accessor :adapter, :development_mode, :namespaces
30
+
31
+ def namespaces
32
+ return @namespaces || []
33
+ end
30
34
  end
31
35
  end
data/lib/horza/errors.rb CHANGED
@@ -23,5 +23,8 @@ module Horza
23
23
 
24
24
  class InvalidOption < StandardError
25
25
  end
26
+
27
+ class NoContextForEntity < StandardError
28
+ end
26
29
  end
27
30
  end
@@ -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
- it 'Returns correct class' do
64
- expect(Horza.adapter.context_for_entity(:user)).to eq HorzaSpec::User
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
- it 'Returns correct class' do
79
- expect(Horza.adapter.context_for_entity(:user)).to eq HorzaSpec::User
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.2
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-08 00:00:00.000000000 Z
11
+ date: 2015-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie