frenetic 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Frenetic::StructureRegistry do
4
+ let(:retriever_class) { described_class::Retriever }
5
+
6
+ subject(:instance) do
7
+ Frenetic::StructureRegistry.new(retriever_class:retriever_class)
8
+ end
9
+
10
+ describe '#construct' do
11
+ let(:resource) { Class.new }
12
+ let(:attributes) do
13
+ {
14
+ 'id' => 123
15
+ }
16
+ end
17
+ let(:key) { 'MockKey' }
18
+
19
+ subject { super().construct(resource, attributes, key) }
20
+
21
+ it 'returns an instantiated Structure class' do
22
+ expect(subject).to be_a Struct::MockKey
23
+ end
24
+
25
+ it 'assigns the specified attributes to the Struct instance' do
26
+ expect(subject.id).to eq 123
27
+ end
28
+
29
+ it 'registers the Struct' do
30
+ subject
31
+ expect(instance.signatures).to_not be_empty
32
+ end
33
+ end
34
+
35
+ describe '#fetch' do
36
+ let(:mock_retriever) { double('MockRetriever') }
37
+ let(:retriever_class) { double('MockRetrieverClass', new: mock_retriever) }
38
+ let(:resource) { Class.new }
39
+ let(:attributes) do
40
+ {
41
+ 'id' => 123
42
+ }
43
+ end
44
+ let(:key) { 'MockKey' }
45
+
46
+ subject { super().fetch(resource, attributes, key) }
47
+
48
+ it 'invokes Frenetic::StructureRegistry::Retriever#call' do
49
+ allow(mock_retriever).to receive(:call)
50
+ subject
51
+ expect(mock_retriever).to have_received(:call)
52
+ end
53
+ end
54
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frenetic
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Lindahl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-28 00:00:00.000000000 Z
11
+ date: 2015-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 3.0.0
89
+ version: 3.3.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 3.0.0
96
+ version: 3.3.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rack-cache
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -209,7 +209,8 @@ files:
209
209
  - lib/frenetic/concerns/hal_linked.rb
210
210
  - lib/frenetic/concerns/member_rest_methods.rb
211
211
  - lib/frenetic/concerns/persistence.rb
212
- - lib/frenetic/concerns/structured.rb
212
+ - lib/frenetic/concerns/related.rb
213
+ - lib/frenetic/concerns/structure_method_definer.rb
213
214
  - lib/frenetic/connection.rb
214
215
  - lib/frenetic/errors.rb
215
216
  - lib/frenetic/hypermedia_link.rb
@@ -218,13 +219,17 @@ files:
218
219
  - lib/frenetic/resource.rb
219
220
  - lib/frenetic/resource_collection.rb
220
221
  - lib/frenetic/resource_mockery.rb
222
+ - lib/frenetic/structure_registry.rb
223
+ - lib/frenetic/structure_registry/rebuilder.rb
224
+ - lib/frenetic/structure_registry/retriever.rb
221
225
  - lib/frenetic/version.rb
222
226
  - spec/behaviors/alternate_string_identifier_spec.rb
223
227
  - spec/briefly_memoizable_spec.rb
224
228
  - spec/concerns/hal_linked_spec.rb
225
229
  - spec/concerns/member_rest_methods_spec.rb
226
230
  - spec/concerns/persistence_spec.rb
227
- - spec/concerns/structured_spec.rb
231
+ - spec/concerns/related_spec.rb
232
+ - spec/concerns/structure_method_definer_spec.rb
228
233
  - spec/connection_spec.rb
229
234
  - spec/fixtures/test_api_requests.rb
230
235
  - spec/frenetic_spec.rb
@@ -235,6 +240,9 @@ files:
235
240
  - spec/resource_mockery_spec.rb
236
241
  - spec/resource_spec.rb
237
242
  - spec/spec_helper.rb
243
+ - spec/structure_registry/rebuilder_spec.rb
244
+ - spec/structure_registry/retriever_spec.rb
245
+ - spec/structure_registry_spec.rb
238
246
  - spec/support/i18n.rb
239
247
  - spec/support/rspec.rb
240
248
  - spec/support/timecop.rb
@@ -271,7 +279,8 @@ test_files:
271
279
  - spec/concerns/hal_linked_spec.rb
272
280
  - spec/concerns/member_rest_methods_spec.rb
273
281
  - spec/concerns/persistence_spec.rb
274
- - spec/concerns/structured_spec.rb
282
+ - spec/concerns/related_spec.rb
283
+ - spec/concerns/structure_method_definer_spec.rb
275
284
  - spec/connection_spec.rb
276
285
  - spec/fixtures/test_api_requests.rb
277
286
  - spec/frenetic_spec.rb
@@ -282,6 +291,9 @@ test_files:
282
291
  - spec/resource_mockery_spec.rb
283
292
  - spec/resource_spec.rb
284
293
  - spec/spec_helper.rb
294
+ - spec/structure_registry/rebuilder_spec.rb
295
+ - spec/structure_registry/retriever_spec.rb
296
+ - spec/structure_registry_spec.rb
285
297
  - spec/support/i18n.rb
286
298
  - spec/support/rspec.rb
287
299
  - spec/support/timecop.rb
@@ -1,48 +0,0 @@
1
- class Frenetic
2
- module Structured
3
- # Stores the unique signature of each Resource Structure
4
- # Used to determine when a Structure has changed and thus
5
- # needs to be redefined.
6
- @@signatures = {}
7
-
8
- def struct_key
9
- "#{self.class}::FreneticResourceStruct".gsub '::', ''
10
- end
11
-
12
- def signature
13
- @attrs.keys.sort.join('')
14
- end
15
-
16
- def structure
17
- if structure_expired?
18
- rebuild_structure!
19
- else
20
- fetch_structure
21
- end
22
- end
23
-
24
- def fetch_structure
25
- Struct.const_get struct_key
26
- end
27
-
28
- def rebuild_structure!
29
- destroy_structure!
30
- @@signatures[struct_key] = signature
31
- Struct.new(struct_key, *@attrs.keys)
32
- end
33
-
34
- def structure_expired?
35
- @@signatures[struct_key] != signature
36
- end
37
-
38
- def structure_defined?
39
- Struct.constants.include? struct_key.to_sym
40
- end
41
-
42
- def destroy_structure!
43
- return unless structure_defined?
44
- @@signatures.delete struct_key
45
- Struct.send :remove_const, struct_key
46
- end
47
- end
48
- end
@@ -1,210 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Frenetic::Structured do
4
- let(:my_temp_resource) do
5
- Class.new do
6
- def initialize(attrs = {})
7
- @attrs = attrs
8
- end
9
- end
10
- end
11
-
12
- let(:signatures) do
13
- described_class.class_variable_get('@@signatures')
14
- end
15
-
16
- let(:signature) do
17
- { 'MyTempResourceFreneticResourceStruct' => 'barfoo' }
18
- end
19
-
20
- before do
21
- stub_const 'MyTempResource', my_temp_resource
22
- MyTempResource.send :include, described_class
23
- end
24
-
25
- after { instance.destroy_structure! }
26
-
27
- subject(:instance) { MyTempResource.new(foo:'foo', bar:'bar') }
28
-
29
- describe '#struct_key' do
30
- subject { super().struct_key }
31
-
32
- it 'returns a valid, unique Ruby constant name' do
33
- expect(subject).to eq 'MyTempResourceFreneticResourceStruct'
34
- end
35
- end
36
-
37
- describe '#signature' do
38
- subject { super().signature }
39
-
40
- it 'returns a unique and predictable key' do
41
- expect(subject).to eq 'barfoo'
42
- end
43
- end
44
-
45
- describe '#structure' do
46
- subject { super().structure }
47
-
48
- context 'with no previously defined signature' do
49
- before do
50
- allow(instance).to receive(:structure_expired?).and_return(true)
51
- end
52
-
53
- it 'rebuilds the structure' do
54
- allow(instance).to receive(:rebuild_structure!).and_call_original
55
- subject
56
- expect(instance).to have_received(:rebuild_structure!)
57
- end
58
- end
59
-
60
- context 'with a fresh signature' do
61
- before do
62
- instance.structure
63
- allow(instance).to receive(:structure_expired?).and_return(false)
64
- end
65
-
66
- it 'does not rebuild the structure' do
67
- allow(instance).to receive(:rebuild_structure!)
68
- subject
69
- expect(instance).to_not have_received(:rebuild_structure!)
70
- end
71
- end
72
-
73
- context 'with an expired signature' do
74
- before do
75
- instance.structure
76
- allow(instance).to receive(:structure_expired?).and_return(true)
77
- end
78
-
79
- it 'rebuilds the structure' do
80
- allow(instance).to receive(:rebuild_structure!)
81
- subject
82
- expect(instance).to have_received(:rebuild_structure!)
83
- end
84
- end
85
- end
86
-
87
- describe '#fetch_structure' do
88
- before { instance.structure }
89
-
90
- subject { super().fetch_structure }
91
-
92
- it 'returns the Struct class of the resource' do
93
- expect(subject).to eq Struct::MyTempResourceFreneticResourceStruct
94
- end
95
- end
96
-
97
- describe '#rebuild_structure!' do
98
- before{ instance.structure }
99
-
100
- subject { super().rebuild_structure! }
101
-
102
- it 'destroys the previous Struct' do
103
- allow(instance).to receive(:destroy_structure!).and_call_original
104
- subject
105
- expect(instance).to have_received(:destroy_structure!)
106
- end
107
-
108
- it 'caches the signature of the resource' do
109
- subject
110
- expect(signatures).to include signature
111
- end
112
-
113
- it 'builds the Struct resource' do
114
- subject
115
- expect(instance.fetch_structure.members).to eq [:foo, :bar]
116
- end
117
- end
118
-
119
- describe '#structure_expired?' do
120
- subject { super().structure_expired? }
121
-
122
- before do
123
- allow(instance).to receive(:signature).and_return(new_sig)
124
- described_class.class_variable_set(
125
- '@@signatures',
126
- 'MyTempResourceFreneticResourceStruct' => old_sig
127
- )
128
- end
129
-
130
- context 'with a fresh signature' do
131
- let(:old_sig) { 'fresh' }
132
- let(:new_sig) { 'fresh' }
133
-
134
- it 'return FALSE' do
135
- expect(subject).to eq false
136
- end
137
- end
138
-
139
- context 'with no predefined signature' do
140
- let(:old_sig) { nil }
141
- let(:new_sig) { 'new' }
142
-
143
- it 'returns TRUE' do
144
- expect(subject).to eq true
145
- end
146
- end
147
-
148
- context 'with an expired signature' do
149
- let(:old_sig) { 'old' }
150
- let(:new_sig) { 'new' }
151
-
152
- it 'returns TRUE' do
153
- expect(subject).to eq true
154
- end
155
- end
156
- end
157
-
158
- describe '#structure_defined?' do
159
- let(:consts) { Struct.constants || [] }
160
-
161
- before do
162
- allow(Struct).to receive(:constants).and_return(consts)
163
- end
164
-
165
- subject { super().structure_defined? }
166
-
167
- it 'checks if the structure is defined' do
168
- allow(instance).to receive(:struct_key).and_return('Foo')
169
- allow(consts).to receive(:include?)
170
- subject
171
- expect(consts).to have_received(:include?).with(:Foo)
172
- end
173
- end
174
-
175
- describe '#destroy_structure!' do
176
- before { instance.structure }
177
-
178
- subject { super().destroy_structure! }
179
-
180
- context 'with an undefined structure' do
181
- before { instance.destroy_structure! }
182
-
183
- it 'does not attempt to remove the constant from the Struct' do
184
- allow(Struct).to receive(:remove_const)
185
- subject
186
- expect(Struct).to_not have_received(:remove_const)
187
- end
188
-
189
- it 'does not remove the signature from the cache' do
190
- allow(signatures).to receive(:delete)
191
- subject
192
- expect(signatures).to_not receive(:delete)
193
- end
194
- end
195
-
196
- context 'with an predefined structure' do
197
- it 'removes the constant' do
198
- expect(Struct.constants).to include instance.struct_key.to_sym
199
- subject
200
- expect(Struct.constants).to_not include instance.struct_key.to_sym
201
- end
202
-
203
- it 'removes the signature from the cache' do
204
- expect(signatures).to include signature
205
- subject
206
- expect(signatures).to_not include signature
207
- end
208
- end
209
- end
210
- end