Class: AbideDevUtils::XCCDF::Diff::BenchmarkDiff

Inherits:
Object
  • Object
show all
Includes:
BenchmarkPropertyDiff
Defined in:
lib/abide_dev_utils/xccdf/diff.rb,
lib/abide_dev_utils/xccdf/diff/benchmark.rb

Overview

Class for benchmark diffs

Constant Summary collapse

DEFAULT_OPTS =
{
  only_classes: %w[rule],
}.freeze
LVL_PROF_DEFAULT =

Used for filtering by level and profile

[nil, nil].freeze

Constants included from BenchmarkPropertyDiff

AbideDevUtils::XCCDF::Diff::BenchmarkPropertyDiff::DEFAULT_PROPERTY_DIFF_OPTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BenchmarkPropertyDiff

#add_other_rule_val, #add_rule_val, #add_self_rule_val, #find_most_similar, #max_digest_similarities, #maxed_digest_similarities, #most_similar, #other_rule_vals, #rule_property_similarity, #safe_rule_prop, #same_rule?, #self_rule_vals

Constructor Details

#initialize(xml1, xml2, opts = {}) ⇒ BenchmarkDiff

Returns a new instance of BenchmarkDiff.

Parameters:

  • xml1 (String)

    path to the first benchmark XCCDF xml file

  • xml2 (String)

    path to the second benchmark XCCDF xml file

  • opts (Hash) (defaults to: {})

    options hash



27
28
29
30
31
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 27

def initialize(benchmark_a, benchmark_b, opts = {})
  @benchmark_a = benchmark_a
  @benchmark_b = benchmark_b
  @opts = opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 35

def method_missing(method_name, *args, &block)
  if opts.key?(method_name)
    opts[method_name]
  elsif @diff&.key?(method_name)
    @diff[method_name]
  else
    super
  end
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



15
16
17
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 15

def opts
  @opts
end

#otherObject (readonly)

Returns the value of attribute other.



15
16
17
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 15

def other
  @other
end

#selfObject (readonly)

Returns the value of attribute self.



15
16
17
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 15

def self
  @self
end

Instance Method Details

#diff(new: false) ⇒ Object

Returns a diff of the changes from the “self” xml (xml1) to the “other” xml (xml2) This function is memoized because the diff operation is expensive. To run the diff operation again, set the `new` parameter to `true` return [Hash] the diff in hash format

Parameters:

  • new (Boolean) (defaults to: false)

    Set to `true` to force a new diff operation



103
104
105
106
107
108
109
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 103

def diff(new: false)
  return @diff if @diff && !new

  @diff = {}
  @diff[:number_title] = number_title_diff
  { from: from_benchmark, to: to_benchmark, diff: @diff }
end

#from_benchmarkObject

Hash of data about the “self” benchmark and the diff parameters



79
80
81
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 79

def from_benchmark
  @from_benchmark ||= from_to_hash(@self)
end

#levelsObject

All levels that numbered children have been filtered on



89
90
91
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 89

def levels
  @levels.flatten.uniq.empty? ? [:all] : @levels.flatten.uniq
end

#number_title_diffObject

Returns the output of a NumberTitleDiff object's diff function based on self_numbered_children and other_numbered_children



74
75
76
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 74

def number_title_diff
  NumberTitleDiff.new(self_numbered_children, other_numbered_children).diff
end

#numbered_children_title_diffObject

Basic title diff



66
67
68
69
70
71
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 66

def numbered_children_title_diff
  {
    self: self_numbered_children.map { |c| c.title.to_s } - other_numbered_children.map { |c| c.title.to_s },
    other: other_numbered_children.map { |c| c.title.to_s } - self_numbered_children.map { |c| c.title.to_s },
  }
end

#other_numbered_childrenObject

Memoized getter for all “numbered” children for the “other” benchmark based on optional filters in opts



58
59
60
61
62
63
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 58

def other_numbered_children
  @other_numbered_children ||= find_all_numbered_children(@other,
                                                          only_classes: opts[:only_classes],
                                                          level: opts[:level],
                                                          profile: opts[:profile])
end

#profilesObject

All profiles that numbered children have been filtered on



94
95
96
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 94

def profiles(profile: nil)
  @profiles ||= diff_profiles(profile: profile)
end

#properties_to_diffObject



172
173
174
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 172

def properties_to_diff
  @properties_to_diff ||= %i[title version profiles]
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 45

def respond_to_missing?(method_name, include_private = false)
  opts.key?(method_name) || @diff&.key?(method_name) || super
end

#self_numbered_childrenObject

Memoized getter for all “numbered” children for the “self” benchmark based on optional filters in opts



50
51
52
53
54
55
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 50

def self_numbered_children
  @self_numbered_children ||= find_all_numbered_children(@self,
                                                         only_classes: opts[:only_classes],
                                                         level: opts[:level],
                                                         profile: opts[:profile])
end

#title_versionObject



176
177
178
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 176

def title_version
  @title_version ||= diff_title_version
end

#to_benchmarkObject

Hash of data about the “other” benchmark and the diff parameters



84
85
86
# File 'lib/abide_dev_utils/xccdf/diff/benchmark.rb', line 84

def to_benchmark
  @to_benchmark ||= from_to_hash(@other)
end