Class: Inspec::AttributeRegistry

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

Defined Under Namespace

Classes: AttributeError, Error, ProfileError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttributeRegistry

Returns a new instance of AttributeRegistry.



34
35
36
37
38
39
40
# File 'lib/inspec/attribute_registry.rb', line 34

def initialize
  # this is a collection of profiles which have a value of attribute objects
  @list = {}

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

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



10
11
12
# File 'lib/inspec/attribute_registry.rb', line 10

def list
  @list
end

Class Method Details

.find_attribute(name, profile) ⇒ Object

These self methods are convenience methods so you dont always have to specify instance when calling the registry



18
19
20
# File 'lib/inspec/attribute_registry.rb', line 18

def self.find_attribute(name, profile)
  instance.find_attribute(name, profile)
end

.list_attributes_for_profile(profile) ⇒ Object



30
31
32
# File 'lib/inspec/attribute_registry.rb', line 30

def self.list_attributes_for_profile(profile)
  instance.list_attributes_for_profile(profile)
end

.register_attribute(name, profile, options = {}) ⇒ Object



22
23
24
# File 'lib/inspec/attribute_registry.rb', line 22

def self.register_attribute(name, profile, options = {})
  instance.register_attribute(name, profile, options)
end

.register_profile_alias(name, alias_name) ⇒ Object



26
27
28
# File 'lib/inspec/attribute_registry.rb', line 26

def self.register_profile_alias(name, alias_name)
  instance.register_profile_alias(name, alias_name)
end

Instance Method Details

#__resetObject



78
79
80
81
# File 'lib/inspec/attribute_registry.rb', line 78

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

#find_attribute(name, profile) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inspec/attribute_registry.rb', line 42

def find_attribute(name, profile)
  profile = @profile_aliases[profile] if !profile_exist?(profile) && @profile_aliases[profile]
  unless profile_exist?(profile)
    error = Inspec::AttributeRegistry::ProfileError.new
    error.profile_name = profile
    raise error, "Profile '#{error.profile_name}' does not have any attributes"
  end

  unless list[profile].key?(name)
    error = Inspec::AttributeRegistry::AttributeError.new
    error.attribute_name = name
    error.profile_name = profile
    raise error, "Profile '#{error.profile_name}' does not have an attribute with name '#{error.attribute_name}'"
  end
  list[profile][name]
end

#list_attributes_for_profile(profile) ⇒ Object



73
74
75
76
# File 'lib/inspec/attribute_registry.rb', line 73

def list_attributes_for_profile(profile)
  list[profile] = {} unless profile_exist?(profile)
  list[profile]
end

#register_attribute(name, profile, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/inspec/attribute_registry.rb', line 59

def register_attribute(name, profile, options = {})
  # check for a profile override name
  if profile_exist?(profile) && list[profile][name] && options.empty?
    list[profile][name]
  else
    list[profile] = {} unless profile_exist?(profile)
    list[profile][name] = Inspec::Attribute.new(name, options)
  end
end

#register_profile_alias(name, alias_name) ⇒ Object



69
70
71
# File 'lib/inspec/attribute_registry.rb', line 69

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