Class: Inspec::Plugins::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/plugins/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__register(name, obj) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inspec/plugins/resource.rb', line 24

def self.__register(name, obj)
  # rubocop:disable Lint/NestedMethodDefinition, Lint/DuplicateMethods
  cl = Class.new(obj) do
    # add some common methods
    include Inspec::Plugins::ResourceCommon
    def initialize(backend, name, *args)
      # attach the backend to this instance
      @__backend_runner__ = backend
      @__resource_name__ = name
      # call the resource initializer
      super(*args)
    end

    def self.desc(description = nil)
      return @description if description.nil?
      @description = description
    end

    def self.example(example = nil)
      return @example if example.nil?
      @example = example
    end

    def inspec
      @__backend_runner__
    end
  end
  # rubocop:enable Lint/NestedMethodDefinition

  # add the resource to the registry by name with a newly-named registry class
  klass_name = name.split('_').map(&:capitalize).join
  Inspec::Resource::Registry.const_set(klass_name, cl)
  Inspec::Resource.registry[name] = Inspec::Resource::Registry.const_get(klass_name)
end

.desc(description = nil) ⇒ Object



14
15
16
17
# File 'lib/inspec/plugins/resource.rb', line 14

def self.desc(description = nil)
  return if description.nil?
  Inspec::Resource.registry[@name].desc(description)
end

.example(example = nil) ⇒ Object



19
20
21
22
# File 'lib/inspec/plugins/resource.rb', line 19

def self.example(example = nil)
  return if example.nil?
  Inspec::Resource.registry[@name].example(example)
end

.name(name = nil) ⇒ Object



8
9
10
11
12
# File 'lib/inspec/plugins/resource.rb', line 8

def self.name(name = nil)
  return if name.nil?
  @name = name
  Inspec::Plugins::Resource.__register(name, self)
end

Instance Method Details

#inspectString

Overwrite inspect to provide better output to RSpec results.

Returns:

  • (String)

    full name of the resource



70
71
72
# File 'lib/inspec/plugins/resource.rb', line 70

def inspect
  to_s
end

#to_sObject

Print the name of the resource



63
64
65
# File 'lib/inspec/plugins/resource.rb', line 63

def to_s
  @__resource_name__
end