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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1078563399bcc7f0a38c4e7d50484ff651d9eb3
4
- data.tar.gz: 215a2cc8a937f9374e2cf47fd3cb3afb61a63935
3
+ metadata.gz: ee0f90532f2a22b297d63c81281cc0640ea3d3ec
4
+ data.tar.gz: f65e4bb4d3d6d72b01c326ed21a170f359189281
5
5
  SHA512:
6
- metadata.gz: 15d3b191e78476558901b7ef51cf0e81b7478b320da2c0c622c8d52e8373a995c2cc73605ca79ca0efbb258ea1e62789e98e8c437f059b885a978be7c71780ab
7
- data.tar.gz: 0fdbf8f5e075619dfe9468b205ded4bcb7865eb9e3ef61aff69fb49b84e695a6c11d37f7c201d4c28cef8035f43ed228d5d122aef9c7ecf9393a97ccd0a0d417
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
- define_embeds_many_getter(relation)
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
- define_method "each_#{relation.to_s.singularize}" do |&block|
151
- return if block.nil?
152
- self.class.each_page(href: links[relation].base_href,
153
- token: token,
154
- headers: headers) do |page|
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.define_embeds_many_getter(relation)
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
@@ -1,5 +1,5 @@
1
1
  module Aptible
2
2
  module Resource
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.0.1'.freeze
4
4
  end
5
5
  end
@@ -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 not fail if not passed a block' do
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
@@ -2,6 +2,7 @@ require 'aptible/resource'
2
2
 
3
3
  class Api < Aptible::Resource::Base
4
4
  has_many :mainframes
5
+ embeds_many :embedded_mainframes
5
6
  embeds_one :best_mainframe
6
7
 
7
8
  def namespace
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.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-09-27 00:00:00.000000000 Z
11
+ date: 2017-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uri_template