mirror of
https://github.com/inspec/inspec
synced 2024-11-10 15:14:23 +00:00
Makes JSON resource enumerable, despite method_missing magic (#2910)
Signed-off-by: David Alexander <opensource@thelonelyghost.com>
This commit is contained in:
parent
29573f7c37
commit
72925a7145
3 changed files with 21 additions and 0 deletions
|
@ -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`.
|
||||
|
|
9
lib/utils/enumerable_delegation.rb
Normal file
9
lib/utils/enumerable_delegation.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
# encoding: utf-8
|
||||
|
||||
module EnumerableDelegation
|
||||
include Enumerable
|
||||
|
||||
def each(&block)
|
||||
@params.each(&block)
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue