Class: Inspec::InputRegistry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/inspec/input_registry.rb,
lib/inspec/errors.rb

Overview

The InputRegistry’s responsibilities include:

- maintaining a list of Input objects that are bound to profiles
- assisting in the lookup and creation of Inputs

Defined Under Namespace

Classes: Error, InputLookupError, ProfileLookupError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInputRegistry

Returns a new instance of InputRegistry.



22
23
24
25
26
27
28
# File 'lib/inspec/input_registry.rb', line 22

def initialize
  # Keyed on String profile_name => Hash of String input_name => Input object
  @inputs_by_profile = {}

  # this is a list of optional profile name overrides set in the inspec.yml
  @profile_aliases = {}
end

Instance Attribute Details

#inputs_by_profileObject (readonly)

Returns the value of attribute inputs_by_profile.



15
16
17
# File 'lib/inspec/input_registry.rb', line 15

def inputs_by_profile
  @inputs_by_profile
end

#profile_aliasesObject (readonly)

Returns the value of attribute profile_aliases.



15
16
17
# File 'lib/inspec/input_registry.rb', line 15

def profile_aliases
  @profile_aliases
end

Instance Method Details

#__resetObject

Used in testing



206
207
208
209
# File 'lib/inspec/input_registry.rb', line 206

def __reset
  @inputs_by_profile = {}
  @profile_aliases = {}
end

#bind_profile_inputs(profile_name, sources = {}) ⇒ Object

This method is called by the Profile as soon as it has enough context to allow binding inputs to it.



91
92
93
94
95
96
97
98
99
100
# File 'lib/inspec/input_registry.rb', line 91

def bind_profile_inputs(profile_name, sources = {})
  inputs_by_profile[profile_name] ||= {}

  # In a more perfect world, we could let the core plugins choose
  # self-determine what to do; but as-is, the APIs that call this
  # are a bit over-constrained.
  (profile_name, sources[:profile_metadata])
  bind_inputs_from_input_files(profile_name, sources[:cli_input_files])
  bind_inputs_from_runner_api(profile_name, sources[:runner_api])
end

#find_or_register_input(input_name, profile_name, options = {}) ⇒ Object

————————————————————-#

Support for Individual Inputs

————————————————————-#



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/inspec/input_registry.rb', line 47

def find_or_register_input(input_name, profile_name, options = {})
  if profile_alias?(profile_name)
    alias_name = profile_name
    profile_name = profile_aliases[profile_name]
    handle_late_arriving_alias(alias_name, profile_name) if profile_known?(alias_name)
  end

  inputs_by_profile[profile_name] ||= {}
  if inputs_by_profile[profile_name].key?(input_name)
    inputs_by_profile[profile_name][input_name].update(options)
  else
    inputs_by_profile[profile_name][input_name] = Inspec::Input.new(input_name, options)
  end

  inputs_by_profile[profile_name][input_name]
end

#handle_late_arriving_alias(alias_name, profile_name) ⇒ Object

It is possible for a wrapper profile to create an input in metadata, referring to the child profile by an alias that has not yet been registered. The registry will then store the inputs under the alias, as if the alias were a true profile. If that happens and the child profile also mentions the input, we will need to move some things - all inputs should be stored under the true profile name, and no inputs should be stored under the alias.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/inspec/input_registry.rb', line 71

def handle_late_arriving_alias(alias_name, profile_name)
  inputs_by_profile[profile_name] ||= {}
  inputs_by_profile[alias_name].each do |input_name, input_from_alias|
    # Move the inpuut, or if it exists, merge events
    existing = inputs_by_profile[profile_name][input_name]
    if existing
      existing.events.concat(input_from_alias.events)
    else
      inputs_by_profile[profile_name][input_name] = input_from_alias
    end
  end
  # Finally, delete the (now copied-out) entry for the alias
  inputs_by_profile.delete(alias_name)
end

#list_inputs_for_profile(profile) ⇒ Object



38
39
40
41
# File 'lib/inspec/input_registry.rb', line 38

def list_inputs_for_profile(profile)
  inputs_by_profile[profile] = {} unless profile_known?(profile)
  inputs_by_profile[profile]
end

#register_profile_alias(name, alias_name) ⇒ Object

————————————————————-#

Support for Profiles

————————————————————-#



34
35
36
# File 'lib/inspec/input_registry.rb', line 34

def register_profile_alias(name, alias_name)
  @profile_aliases[name] = alias_name
end