Skip to content

Commit 3a6b10d

Browse files
author
Kevin Paulisse
committed
Rename method and make code more efficient
1 parent 2e33024 commit 3a6b10d

2 files changed

Lines changed: 13 additions & 15 deletions

File tree

lib/octocatalog-diff/catalog.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def validate_references
190190
@options[:validate_references].each do |r|
191191
next unless x.key?('parameters')
192192
next unless x['parameters'].key?(r)
193-
missing_resources = remove_existing_resources(x['parameters'][r])
193+
missing_resources = resources_missing_from_catalog(x['parameters'][r])
194194
next unless missing_resources.any?
195195
missing << missing_resources.map { |missing_target| { source: x, target_type: r, target_value: missing_target } }
196196
end
@@ -222,20 +222,18 @@ def validate_references
222222

223223
private
224224

225-
# Private method: Determine if a catalog contains resource or resources, which may
226-
# have been passed in as an array or a string. Return the references to resources
227-
# that are missing from the catalog. (An empty array would indicate all references
228-
# are present.)
225+
# Private method: Given a list of resources to check, return the references from
226+
# that list that are missing from the catalog. (An empty array returned would indicate
227+
# all references are present in the catalog.)
229228
# @param resources_to_check [String / Array] Resources to check
230229
# @return [Array] References that are missing from catalog
231-
def remove_existing_resources(resources_to_check)
232-
rtc_array = resources_to_check.is_a?(Array) ? resources_to_check : [resources_to_check]
233-
rtc_array.map do |res|
230+
def resources_missing_from_catalog(resources_to_check)
231+
[resources_to_check].flatten.select do |res|
234232
unless res =~ /\A([\w:]+)\[(.+)\]\z/
235233
raise ArgumentError, "Resource #{res} is not in the expected format"
236234
end
237-
resource(type: Regexp.last_match(1), title: Regexp.last_match(2)).nil? ? res : nil
238-
end.compact
235+
resource(type: Regexp.last_match(1), title: Regexp.last_match(2)).nil?
236+
end
239237
end
240238

241239
# Private method: Choose backend based on passed-in options

spec/octocatalog-diff/tests/catalog_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@
410410
end
411411

412412
describe OctocatalogDiff::Catalog do
413-
describe '#remove_existing_resources' do
413+
describe '#resources_missing_from_catalog' do
414414
let(:catalog) do
415415
opts = {
416416
compare_file_text: false,
@@ -422,30 +422,30 @@
422422

423423
it 'should raise error if resource is not in expected format' do
424424
test_arg = ['Foo-Bar']
425-
expect { catalog.send(:remove_existing_resources, test_arg) }.to raise_error(ArgumentError, /Resource Foo-Bar /)
425+
expect { catalog.send(:resources_missing_from_catalog, test_arg) }.to raise_error(ArgumentError, /Resource Foo-Bar /)
426426
end
427427

428428
it 'should return full array when no matches' do
429429
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(nil)
430430
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(nil)
431431
test_arg = ['Foo[bar]', 'Baz[biff]']
432-
result = catalog.send(:remove_existing_resources, test_arg)
432+
result = catalog.send(:resources_missing_from_catalog, test_arg)
433433
expect(result).to eq(['Foo[bar]', 'Baz[biff]'])
434434
end
435435

436436
it 'should remove matching entries' do
437437
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(nil)
438438
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(true)
439439
test_arg = ['Foo[bar]', 'Baz[biff]']
440-
result = catalog.send(:remove_existing_resources, test_arg)
440+
result = catalog.send(:resources_missing_from_catalog, test_arg)
441441
expect(result).to eq(['Foo[bar]'])
442442
end
443443

444444
it 'should return empty array with all matches' do
445445
allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(true)
446446
allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(true)
447447
test_arg = ['Foo[bar]', 'Baz[biff]']
448-
result = catalog.send(:remove_existing_resources, test_arg)
448+
result = catalog.send(:resources_missing_from_catalog, test_arg)
449449
expect(result).to eq([])
450450
end
451451
end

0 commit comments

Comments
 (0)