Module: Inspec::DescribeDslLazyLoader

Included in:
RSpec::Core::ExampleGroup
Defined in:
lib/inspec/rspec_extensions.rb

Overview

This module exists to intercept the method_missing class method on RSpec::Core::ExampleGroup and is part of support for DSL plugintypes

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Support for Describe DSL plugins



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/inspec/rspec_extensions.rb', line 13

def method_missing(method_name, *arguments, &block)
  begin
    backend = [:backend] # populated via Inspec::Runner#add_resource
    resource = Inspec::DSL.method_missing_resource(backend, method_name, *arguments)
    return resource if resource
  rescue LoadError
    # pass through
  end

  # Check to see if there is a describe_dsl plugin activator hook with the method name
  registry = Inspec::Plugin::V2::Registry.instance
  hook = registry.find_activators(plugin_type: :describe_dsl, activator_name: method_name).first

  if hook
    # OK, load the hook if it hasn't been already.  We'll then know a module,
    # which we can then inject into the context
    hook.activate

    # Inject the module's methods into the example group contexts.
    # implementation_class is the field name, but this is actually a module.
    # RSpec works by having these helper methods defined as class methods
    # (see the definition of `let` as an example)
    # So, we use extend to inject the new DSL methods.
    RSpec::Core::ExampleGroup.extend(hook.implementation_class)

    # We still haven't called the method we were looking for, so do so now.
    send(method_name, *arguments, &block)
  else
    super
  end
end