aptible-resource 1.0.0 → 1.0.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 +4 -4
- data/lib/aptible/resource/base.rb +21 -8
- data/lib/aptible/resource/version.rb +1 -1
- data/spec/aptible/resource/base_spec.rb +55 -2
- data/spec/fixtures/api.rb +1 -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: ee0f90532f2a22b297d63c81281cc0640ea3d3ec
|
4
|
+
data.tar.gz: f65e4bb4d3d6d72b01c326ed21a170f359189281
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90c8d6bbb0c37f2167a84cb14c2580232fa165ec5cb032bcddb9a5af261cd558aba7a31d4f6b43285df2c7d712d4f1659fd455df54d45151feca933ec1b04c53
|
7
|
+
data.tar.gz: 4b0132d4eb1e9b9d80a6c747725895c5de39351bafe23cd7f34a5543ff64afd2749ec800b3f1b782c524048e3f249125e2247181b728d74da5d059516c21451b
|
@@ -37,7 +37,8 @@ module Aptible
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def self.each_page(options = {})
|
40
|
-
return unless block_given?
|
40
|
+
return enum_for(:each_page, options) unless block_given?
|
41
|
+
|
41
42
|
href = options[:href] || collection_href
|
42
43
|
while href
|
43
44
|
# TODO: Breaking here is consistent with the existing behavior of
|
@@ -101,7 +102,7 @@ module Aptible
|
|
101
102
|
# rubocop:enable PredicateName
|
102
103
|
|
103
104
|
def self.embeds_many(relation)
|
104
|
-
|
105
|
+
define_embeds_many_getters(relation)
|
105
106
|
define_has_many_setter(relation)
|
106
107
|
end
|
107
108
|
|
@@ -147,11 +148,16 @@ module Aptible
|
|
147
148
|
end
|
148
149
|
end
|
149
150
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
151
|
+
iterator_method = "each_#{relation.to_s.singularize}".to_sym
|
152
|
+
|
153
|
+
define_method iterator_method do |&block|
|
154
|
+
next enum_for(iterator_method) if block.nil?
|
155
|
+
|
156
|
+
self.class.each_page(
|
157
|
+
href: links[relation].base_href,
|
158
|
+
token: token,
|
159
|
+
headers: headers
|
160
|
+
) do |page|
|
155
161
|
page.each { |entry| block.call entry }
|
156
162
|
end
|
157
163
|
end
|
@@ -166,11 +172,18 @@ module Aptible
|
|
166
172
|
end
|
167
173
|
end
|
168
174
|
|
169
|
-
def self.
|
175
|
+
def self.define_embeds_many_getters(relation)
|
170
176
|
define_method relation do
|
171
177
|
get unless loaded
|
172
178
|
objects[relation].entries
|
173
179
|
end
|
180
|
+
|
181
|
+
iterator_method = "each_#{relation.to_s.singularize}".to_sym
|
182
|
+
|
183
|
+
define_method iterator_method do |&block|
|
184
|
+
next enum_for(iterator_method) if block.nil?
|
185
|
+
send(relation).each(&block)
|
186
|
+
end
|
174
187
|
end
|
175
188
|
|
176
189
|
# rubocop:disable MethodLength
|
@@ -130,6 +130,14 @@ describe Aptible::Resource::Base do
|
|
130
130
|
Api::Mainframe.each_page { break }
|
131
131
|
expect(calls).to eq(['/mainframes'])
|
132
132
|
end
|
133
|
+
|
134
|
+
it 'should return an enum if no block is given' do
|
135
|
+
e = Api::Mainframe.each_page
|
136
|
+
pages = 0
|
137
|
+
e.each { pages += 1 }
|
138
|
+
expect(pages).to eq(2)
|
139
|
+
expect(calls).to eq(urls)
|
140
|
+
end
|
133
141
|
end
|
134
142
|
|
135
143
|
describe '.create' do
|
@@ -362,8 +370,53 @@ describe Aptible::Resource::Base do
|
|
362
370
|
expect(calls).to eq([urls[0]])
|
363
371
|
end
|
364
372
|
|
365
|
-
it 'should
|
366
|
-
subject.each_mainframe
|
373
|
+
it 'should return an enum if no block is given' do
|
374
|
+
e = subject.each_mainframe
|
375
|
+
records = []
|
376
|
+
e.each { |m| records << m }
|
377
|
+
expect(records.size).to eq(2)
|
378
|
+
expect(calls).to eq(urls)
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
context '.embeds_many' do
|
384
|
+
let(:m1) { Api::Mainframe.new }
|
385
|
+
let(:m2) { Api::Mainframe.new }
|
386
|
+
|
387
|
+
before { subject.stub(:loaded) { true } }
|
388
|
+
before { subject.stub(:objects) { { embedded_mainframes: [m1, m2] } } }
|
389
|
+
before { m1.stub(id: 1) }
|
390
|
+
before { m2.stub(id: 2) }
|
391
|
+
|
392
|
+
describe '#{relation}s' do
|
393
|
+
it 'should return all records' do
|
394
|
+
records = subject.embedded_mainframes
|
395
|
+
|
396
|
+
expect(records.size).to eq(2)
|
397
|
+
expect(records.first.id).to eq(1)
|
398
|
+
expect(records.second.id).to eq(2)
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
describe 'each_#{relation}' do
|
403
|
+
it 'should iterate over all records' do
|
404
|
+
records = []
|
405
|
+
subject.each_embedded_mainframe { |mainframe| records << mainframe }
|
406
|
+
|
407
|
+
expect(records.size).to eq(2)
|
408
|
+
expect(records.first.id).to eq(1)
|
409
|
+
expect(records.second.id).to eq(2)
|
410
|
+
end
|
411
|
+
|
412
|
+
it 'should return an enum if no block is given' do
|
413
|
+
e = subject.each_embedded_mainframe
|
414
|
+
records = []
|
415
|
+
e.each { |mainframe| records << mainframe }
|
416
|
+
|
417
|
+
expect(records.size).to eq(2)
|
418
|
+
expect(records.first.id).to eq(1)
|
419
|
+
expect(records.second.id).to eq(2)
|
367
420
|
end
|
368
421
|
end
|
369
422
|
end
|
data/spec/fixtures/api.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aptible-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank Macreery
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uri_template
|