Class: AbideDevUtils::XCCDF::Utils::FileNameData

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/xccdf/utils.rb

Overview

Parses XCCDF file names into labeled parts

Constant Summary collapse

CIS_PATTERN =
/^CIS_(?<subject>[A-Za-z0-9._()-]+?)(?<stig>_STIG)?_Benchmark_v(?<version>[0-9.]+)-xccdf$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileNameData

Returns a new instance of FileNameData.



47
48
49
50
51
# File 'lib/abide_dev_utils/xccdf/utils.rb', line 47

def initialize(path)
  @path = path
  @name = File.basename(path, '.xml')
  @labeled_parts = File.basename(name, '.xml').match(CIS_PATTERN)&.named_captures
end

Instance Attribute Details

#labeled_partsObject (readonly)

Returns the value of attribute labeled_parts.



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

def labeled_parts
  @labeled_parts
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#fuzzy_match?(label, value) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# File 'lib/abide_dev_utils/xccdf/utils.rb', line 70

def fuzzy_match?(label, value)
  return false unless has?(label)

  this_val = normalize_char_array(send(label.to_sym).chars)
  other_val = normalize_char_array(value.chars)
  other_val.each_with_index do |c, idx|
    return false unless this_val[idx] == c
  end
  true
end

#has?(label) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/abide_dev_utils/xccdf/utils.rb', line 65

def has?(label)
  val = send(label.to_sym)
  !val.nil? && !val.empty?
end

#stigObject



57
58
59
# File 'lib/abide_dev_utils/xccdf/utils.rb', line 57

def stig
  @stig ||= labeled_parts&.fetch('subject', nil)
end

#subjectObject



53
54
55
# File 'lib/abide_dev_utils/xccdf/utils.rb', line 53

def subject
  @subject ||= labeled_parts&.fetch('subject', nil)
end

#versionObject



61
62
63
# File 'lib/abide_dev_utils/xccdf/utils.rb', line 61

def version
  @version ||= labeled_parts&.fetch('version', nil)
end