Class: Inspec::Plugin::V2::Registry
- Inherits:
-
Object
- Object
- Inspec::Plugin::V2::Registry
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/inspec/plugin/v2/registry.rb
Instance Attribute Summary collapse
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
-
#__reset ⇒ Object
Provided for test support.
- #any_load_failures? ⇒ Boolean
-
#find_activator(filters = {}) ⇒ Object
Convenience method for when you expect exactly one.
-
#find_activators(filters = {}) ⇒ Object
Finds Activators matching criteria (all optional) you specify as a Hash.
- #find_status_by_class(klass) ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #known_count ⇒ Object
- #loaded_count ⇒ Object
- #loaded_plugin?(name) ⇒ Boolean
- #loaded_plugin_names ⇒ Object
- #path_based_plugin?(name) ⇒ Boolean
- #register(name, status) ⇒ Object (also: #[]=)
Constructor Details
#initialize ⇒ Registry
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
#registry ⇒ Object (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
#__reset ⇒ Object
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
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.
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_count ⇒ Object
41 42 43 |
# File 'lib/inspec/plugin/v2/registry.rb', line 41 def known_count registry.values.count end |
#loaded_count ⇒ Object
37 38 39 |
# File 'lib/inspec/plugin/v2/registry.rb', line 37 def loaded_count loaded_plugin_names.count end |
#loaded_plugin?(name) ⇒ 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_names ⇒ Object
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
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 |