Makes JSON resource enumerable, despite method_missing magic (#2910)

Signed-off-by: David Alexander <opensource@thelonelyghost.com>
This commit is contained in:
David Alexander 2018-04-26 11:54:16 -04:00 committed by Clinton Wolfe
parent 29573f7c37
commit 72925a7145
3 changed files with 21 additions and 0 deletions

View file

@ -1,6 +1,7 @@
# encoding: utf-8
require 'utils/object_traversal'
require 'utils/enumerable_delegation'
require 'utils/file_reader'
module Inspec::Resources
@ -37,6 +38,9 @@ module Inspec::Resources
# load the raw content from the source, and then parse it
@raw_content = load_raw_content(opts)
@params = parse(@raw_content)
# If the JSON content is enumerable, make this object enumerable too
extend EnumerableDelegation if @params.respond_to?(:each)
end
# Shorthand to retrieve a parameter name via `#its`.

View file

@ -0,0 +1,9 @@
# encoding: utf-8
module EnumerableDelegation
include Enumerable
def each(&block)
@params.each(&block)
end
end

View file

@ -32,6 +32,14 @@ describe 'Inspec::Resources::JSON' do
it 'doesnt resolve symbol-notation names' do
_(resource.send(:'x.y.z')).must_be_nil
end
it 'is enumerability matches its data' do
enum = load_resource('json', content: '["a","b"]')
not_enum = load_resource('json', content: '525600')
_(enum.respond_to?(:each)).must_equal true
_(not_enum.respond_to?(:each)).must_equal false
end
end
describe 'when loading a nonexistent file' do