2019-12-30 22:54:37 +00:00
|
|
|
require "inspec/resource"
|
2019-06-11 22:24:35 +00:00
|
|
|
require "inspec/dsl_shared"
|
2016-08-31 15:48:51 +00:00
|
|
|
|
|
|
|
module Inspec
|
|
|
|
#
|
|
|
|
# LibaryEvalContext constructs an instance of an anonymous class
|
|
|
|
# that library files will be instance_exec'd against.
|
|
|
|
#
|
|
|
|
# The anonymous class ensures that `Inspec.resource(1)` will return
|
|
|
|
# an anonymouse class that is suitable as the parent class of an
|
|
|
|
# inspec resource. The class returned will have the resource
|
|
|
|
# registry used by all dsl methods bound to the resource registry
|
|
|
|
# passed into the #create constructor.
|
|
|
|
#
|
2020-02-21 21:36:47 +00:00
|
|
|
#
|
2016-08-31 15:48:51 +00:00
|
|
|
class LibraryEvalContext
|
|
|
|
def self.create(registry, require_loader)
|
2020-02-21 21:36:47 +00:00
|
|
|
c = Class.new(Inspec::Resource) do
|
|
|
|
define_singleton_method :__resource_registry do
|
|
|
|
registry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
c2 = Class.new do
|
|
|
|
define_singleton_method :resource do |version|
|
|
|
|
Inspec.validate_resource_dsl_version!(version)
|
|
|
|
c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
c3 = Class.new do
|
|
|
|
include Inspec::DSL::RequireOverride
|
|
|
|
def initialize(require_loader)
|
|
|
|
@require_loader = require_loader
|
|
|
|
@inspec_binding = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def __inspec_binding
|
|
|
|
@inspec_binding
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
c3.const_set(:Inspec, c2)
|
|
|
|
res = c3.new(require_loader)
|
|
|
|
|
|
|
|
# Provide the local binding for this context which is necessary for
|
|
|
|
# calls to `require` to create all dependent objects in the correct
|
|
|
|
# context.
|
|
|
|
res.instance_variable_set("@inspec_binding", res.instance_eval("binding"))
|
|
|
|
res
|
2016-08-31 15:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|