Class: Inspec::Plugin::V2::Registry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/inspec/plugin/v2/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



21
22
23
# File 'lib/inspec/plugin/v2/registry.rb', line 21

def initialize
  @registry = {}
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



13
14
15
# File 'lib/inspec/plugin/v2/registry.rb', line 13

def registry
  @registry
end

Instance Method Details

#__resetObject

Provided for test support. Purges the registry.



94
95
96
# File 'lib/inspec/plugin/v2/registry.rb', line 94

def __reset
  @registry.clear
end

#any_load_failures?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/inspec/plugin/v2/registry.rb', line 25

def any_load_failures?
  !plugin_statuses.select(&:load_exception).empty?
end

#find_activator(filters = {}) ⇒ Object

Convenience method for when you expect exactly one



72
73
74
75
76
77
78
79
80
81
# File 'lib/inspec/plugin/v2/registry.rb', line 72

def find_activator(filters = {})
  matched_plugins = find_activators(filters)
  if matched_plugins.count > 1
    raise Inspec::Plugin::V2::LoadError, "Plugin hooks search returned multiple results for filter #{filters.inspect} - use more filters, or use find_activators (plural)"
  elsif matched_plugins.empty?
    raise Inspec::Plugin::V2::LoadError, "Plugin hooks search returned zero results for filter #{filters.inspect}"
  end

  matched_plugins.first
end

#find_activators(filters = {}) ⇒ Object

Finds Activators matching criteria (all optional) you specify as a Hash.

Parameters:

  • plugin_name (Symbol)

    Restricts the search to the given plugin

  • plugin_type (Symbol)

    Restricts the search to the given plugin type

  • activator_name (Symbol)

    Name of the activator

  • implementation_class (Class)

    Implementation class returned by an already-actived plugin type



63
64
65
66
67
68
69
# File 'lib/inspec/plugin/v2/registry.rb', line 63

def find_activators(filters = {})
  plugin_statuses.map(&:activators).flatten.select do |act|
    %i{plugin_name plugin_type activator_name implementation_class}.all? do |criteria|
      !filters.key?(criteria) || act[criteria] == filters[criteria]
    end
  end
end

#find_status_by_class(klass) ⇒ Object



53
54
55
# File 'lib/inspec/plugin/v2/registry.rb', line 53

def find_status_by_class(klass)
  registry.values.detect { |status| status.plugin_class == klass }
end

#known_countObject



41
42
43
# File 'lib/inspec/plugin/v2/registry.rb', line 41

def known_count
  registry.values.count
end

#loaded_countObject



37
38
39
# File 'lib/inspec/plugin/v2/registry.rb', line 37

def loaded_count
  loaded_plugin_names.count
end

#loaded_plugin?(name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/inspec/plugin/v2/registry.rb', line 29

def loaded_plugin?(name)
  # HACK: Status is normally the source of truth for loadedness, unless it is a train plugin; then the Train::Registry is the source of truth.
  # Also, InSpec registry is keyed on Symbols; Train is keyed on Strings.
  return registry.dig(name.to_sym, :loaded) unless name.to_s.start_with?("train-")

  Train::Plugins.registry.key?(name.to_s.sub(/^train-/, ""))
end

#loaded_plugin_namesObject



45
46
47
# File 'lib/inspec/plugin/v2/registry.rb', line 45

def loaded_plugin_names
  registry.keys.select { |name| loaded_plugin?(name) }
end

#path_based_plugin?(name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/inspec/plugin/v2/registry.rb', line 49

def path_based_plugin?(name)
  known_plugin?(name.to_sym) && registry[name.to_sym].installation_type == :path
end

#register(name, status) ⇒ Object Also known as: []=



83
84
85
86
87
88
89
# File 'lib/inspec/plugin/v2/registry.rb', line 83

def register(name, status)
  if known_plugin? name
    Inspec::Log.debug "PluginLoader: refusing to re-register plugin '#{name}': an existing plugin with that name was loaded via #{registry[name].installation_type}-loading from #{registry[name].entry_point}"
  else
    registry[name.to_sym] = status
  end
end