class Chef::Audit::ControlGroupData

Attributes

controls[R]
metadata[R]
name[R]
number_failed[R]
number_succeeded[R]
status[R]

Public Class Methods

new(name, metadata = {}) click to toggle source
# File lib/chef/audit/control_group_data.rb, line 52
def initialize(name, metadata = {})
  @status = "success"
  @controls = []
  @number_succeeded = 0
  @number_failed = 0
  @name = name
  @metadata = metadata
end

Public Instance Methods

example_failure(control_data, details) click to toggle source
# File lib/chef/audit/control_group_data.rb, line 69
def example_failure(control_data, details)
  @number_failed += 1
  @status = "failure"
  control = create_control(control_data)
  control.details = details if details
  control.status = "failure"
  controls << control
  control
end
example_success(control_data) click to toggle source
# File lib/chef/audit/control_group_data.rb, line 61
def example_success(control_data)
  @number_succeeded += 1
  control = create_control(control_data)
  control.status = "success"
  controls << control
  control
end
to_hash() click to toggle source
# File lib/chef/audit/control_group_data.rb, line 79
def to_hash
  # We sort it so the examples appear in the output in the same order
  # they appeared in the recipe
  controls.sort! { |x, y| x.line_number <=> y.line_number }
  h = {
        :name => name,
        :status => status,
        :number_succeeded => number_succeeded,
        :number_failed => number_failed,
        :controls => controls.collect { |c| c.to_hash },
  }
  # If there is a duplicate key, metadata will overwrite it
  add_display_only_data(h).merge(metadata)
end

Private Instance Methods

add_display_only_data(group) click to toggle source

The id and control sequence number are ephemeral data - they are not needed to be persisted and can be regenerated at will. They are only needed for display purposes.

# File lib/chef/audit/control_group_data.rb, line 103
def add_display_only_data(group)
  group[:id] = SecureRandom.uuid
  group[:controls].collect!.with_index do |c, i|
    # i is zero-indexed, and we want the display one-indexed
    c[:sequence_number] = i + 1
    c
  end
  group
end
create_control(control_data) click to toggle source
# File lib/chef/audit/control_group_data.rb, line 96
def create_control(control_data)
  ControlData.new(control_data)
end