Class: Chef::Compliance::Waiver

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/compliance/waiver.rb

Overview

Chef object that represents a single waiver file in the compliance segment of a cookbook

Constant Summary collapse

HIDDEN_IVARS =
[ :@events ].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events, data, path, cookbook_name) ⇒ Waiver

Returns a new instance of Waiver.



48
49
50
51
52
53
54
55
# File 'lib/chef/compliance/waiver.rb', line 48

def initialize(events, data, path, cookbook_name)
  @events = events
  @data = data
  @cookbook_name = cookbook_name
  @path = path
  @pathname = File.basename(path, File.extname(path)) unless path.nil?
  disable!
end

Instance Attribute Details

#cookbook_nameString (readonly)

Returns The name of the cookbook that the waiver is in.

Returns:

  • (String)

    The name of the cookbook that the waiver is in



31
32
33
# File 'lib/chef/compliance/waiver.rb', line 31

def cookbook_name
  @cookbook_name
end

#dataObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
# File 'lib/chef/compliance/waiver.rb', line 40

def data
  @data
end

#enabledBoolean (readonly)

Returns if the waiver has been enabled.

Returns:

  • (Boolean)

    if the waiver has been enabled



28
29
30
# File 'lib/chef/compliance/waiver.rb', line 28

def enabled
  @enabled
end

#eventsChef::EventDispatch::Dispatcher

Event dispatcher for this run.



46
47
48
# File 'lib/chef/compliance/waiver.rb', line 46

def events
  @events
end

#pathString (readonly)

Returns The full path on the host to the waiver yml file.

Returns:

  • (String)

    The full path on the host to the waiver yml file



34
35
36
# File 'lib/chef/compliance/waiver.rb', line 34

def path
  @path
end

#pathnameString (readonly)

Returns the pathname in the cookbook.

Returns:

  • (String)

    the pathname in the cookbook



37
38
39
# File 'lib/chef/compliance/waiver.rb', line 37

def pathname
  @pathname
end

Class Method Details

.from_file(events, filename, cookbook_name = nil) ⇒ Object

Parameters:

  • filename (String)

    full path to the yml file in the cookbook

  • cookbook_name (String) (defaults to: nil)

    cookbook that the waiver is in



110
111
112
# File 'lib/chef/compliance/waiver.rb', line 110

def self.from_file(events, filename, cookbook_name = nil)
  from_yaml(events, IO.read(filename), filename, cookbook_name)
end

.from_hash(events, hash, path = nil, cookbook_name = nil) ⇒ Object

Helper to construct a waiver object from a hash. Since the path and cookbook_name are required this is probably not externally useful.



96
97
98
# File 'lib/chef/compliance/waiver.rb', line 96

def self.from_hash(events, hash, path = nil, cookbook_name = nil)
  new(events, hash, path, cookbook_name)
end

.from_yaml(events, string, path = nil, cookbook_name = nil) ⇒ Object

Helper to construct a waiver object from a yaml string. Since the path and cookbook_name are required this is probably not externally useful.



103
104
105
# File 'lib/chef/compliance/waiver.rb', line 103

def self.from_yaml(events, string, path = nil, cookbook_name = nil)
  from_hash(events, YAML.safe_load(string, permitted_classes: [Date]), path, cookbook_name)
end

Instance Method Details

#disable!Object

Set the waiver as being disabled



72
73
74
# File 'lib/chef/compliance/waiver.rb', line 72

def disable!
  @enabled = false
end

#enable!Object

Set the waiver to being enabled



65
66
67
68
# File 'lib/chef/compliance/waiver.rb', line 65

def enable!
  events.compliance_waiver_enabled(self)
  @enabled = true
end

#enabled?Boolean

Returns if the waiver has been enabled.

Returns:

  • (Boolean)

    if the waiver has been enabled



59
60
61
# File 'lib/chef/compliance/waiver.rb', line 59

def enabled?
  !!@enabled
end

#inspec_dataObject

Render the waiver in a way that it can be consumed by inspec



78
79
80
# File 'lib/chef/compliance/waiver.rb', line 78

def inspec_data
  data
end

#inspectObject

Omit the event object from error output



86
87
88
89
90
91
# File 'lib/chef/compliance/waiver.rb', line 86

def inspect
  ivar_string = (instance_variables.map(&:to_sym) - HIDDEN_IVARS).map do |ivar|
    "#{ivar}=#{instance_variable_get(ivar).inspect}"
  end.join(", ")
  "#<#{self.class}:#{object_id} #{ivar_string}>"
end