move resource dsl creation to resource plugin

This commit is contained in:
Dominik Richter 2016-01-16 06:25:47 +01:00 committed by Stephan Renatus
parent 3486a67ca4
commit 21d9ae7e1d
2 changed files with 31 additions and 23 deletions

View file

@ -25,9 +25,8 @@ module Inspec
end end
def reload_dsl def reload_dsl
dsl = create_inner_dsl(@backend) dsl = create_dsl
outer_dsl = create_outer_dsl(dsl) ctx = create_context(dsl)
ctx = create_context(outer_dsl)
@profile_context = ctx.new @profile_context = ctx.new
end end
@ -66,36 +65,22 @@ module Inspec
private private
# Creates the inner DSL which includes all resources for
# creating tests. It is always connected to one target,
# which is specified via the backend argument.
#
# @param backend [BackendRunner] exposing the target to resources
# @return [InnerDSLModule]
def create_inner_dsl(backend)
Module.new do
Inspec::Resource.registry.each do |id, r|
define_method id.to_sym do |*args|
r.new(backend, id.to_s, *args)
end
end
end
end
# Creates the outer DSL which includes all methods for creating # Creates the outer DSL which includes all methods for creating
# tests and control structures. # tests and control structures.
# #
# @param dsl [InnerDSLModule] which contains all resources # @param backend [BackendRunner] exposing the target to resources
# @return [OuterDSLClass] # @return [OuterDSLClass]
def create_outer_dsl(dsl) def create_dsl
resources_dsl = Inspec::Resource.create_dsl(@backend)
rule_class = Class.new(Inspec::Rule) do rule_class = Class.new(Inspec::Rule) do
include RSpec::Core::DSL include RSpec::Core::DSL
include dsl include resources_dsl
end end
# rubocop:disable Lint/NestedMethodDefinition # rubocop:disable Lint/NestedMethodDefinition
Class.new do Class.new do
include dsl include resources_dsl
define_method :control do |*args, &block| define_method :control do |*args, &block|
id = args[0] id = args[0]

View file

@ -11,8 +11,31 @@ module Inspec
def self.registry def self.registry
@registry ||= {} @registry ||= {}
end end
# Creates the inner DSL which includes all resources for
# creating tests. It is always connected to one target,
# which is specified via the backend argument.
#
# @param backend [BackendRunner] exposing the target to resources
# @return [ResourceDSLModule]
def self.create_dsl(backend)
# need the local name, to use it in the module creation further down
my_registry = registry
Module.new do
my_registry.each do |id, r|
define_method id.to_sym do |*args|
r.new(backend, id.to_s, *args)
end
end
end
end
end end
# Retrieve the base class for creating a new resource.
# Create classes that inherit from this class.
#
# @param [int] version the resource version to use
# @return [Resource] base class for creating a new resource
def self.resource(version) def self.resource(version)
if version != 1 if version != 1
fail 'Only resource version 1 is supported!' fail 'Only resource version 1 is supported!'