Sha256: ffa233e1b13a3f4fe3020d20b41bddeb471851a45673b94d4a51230f86050126

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require "inspec/resource"
require "inspec/dsl_shared"

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.
  #
  class LibraryEvalContext
    # rubocop:disable Naming/ConstantName
    Inspec = :nope! # see #initialize below
    # rubocop:enable Naming/ConstantName

    ##
    # Include a custom `require` method that gets used when this
    # context is used to eval source. See lib/inspec/dsl_shared.rb for
    # more details.
    include ::Inspec::DSL::RequireOverride

    def self.create(registry, require_loader)
      Class.new(LibraryEvalContext).new(registry, require_loader)
    end

    # Provide the local binding for this context which is
    # necessary for calls to `require` to create all dependent
    # objects in the correct context.
    attr_accessor :__inspec_binding

    def initialize(registry, require_loader)
      @require_loader = require_loader
      # rubocop:disable Style/RedundantSelf
      self.__inspec_binding = self.instance_eval { binding }
      # rubocop:enable Style/RedundantSelf

      @res_klass = Class.new ::Inspec::Resource
      @res_klass.__resource_registry = registry

      # NOTE: this *must* be a subclass of LibraryEvalContext to work
      self.class.const_set :Inspec, self # BYPASS! See resource below
    end

    # Fake for Inspec.resource in lib/inspec/resource.rb that provides
    # our own Resource subclass that has its own private __resource_registry
    def resource(version)
      ::Inspec.validate_resource_dsl_version!(version)
      @res_klass
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inspec-core-4.18.85 lib/inspec/library_eval_context.rb