Class: AbideDevUtils::CEM::Mapping::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/cem/mapping/mapper.rb

Overview

Handles interacting with mapping data

Constant Summary collapse

MAP_TYPES =
%w[hiera_title_num number hiera_title vulnid title].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_name, framework, map_data) ⇒ Mapper

Returns a new instance of Mapper.



12
13
14
15
16
17
18
19
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 12

def initialize(module_name, framework, map_data)
  @module_name = module_name
  @framework = framework
  load_framework(@framework)
  @map_data = map_data
  @cache = {}
  @rule_cache = {}
end

Instance Attribute Details

#frameworkObject (readonly)

Returns the value of attribute framework.



10
11
12
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 10

def framework
  @framework
end

#map_dataObject (readonly)

Returns the value of attribute map_data.



10
11
12
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 10

def map_data
  @map_data
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



10
11
12
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 10

def module_name
  @module_name
end

Instance Method Details

#each_like(identifier) ⇒ Object



29
30
31
32
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 29

def each_like(identifier)
  mtype, mtop = map_type_and_top_key(identifier)
  map_data[mtype][mtop].each { |key, val| yield key, val }
end

#each_with_array_like(identifier) ⇒ Object



34
35
36
37
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 34

def each_with_array_like(identifier)
  mtype, mtop = map_type_and_top_key(identifier)
  map_data[mtype][mtop].each_with_object([]) { |(key, val), ary| yield [key, val], ary }
end

#get(control_id, level: nil, profile: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 39

def get(control_id, level: nil, profile: nil)
  return cache_get(control_id, level, profile) if cached?(control_id, level, profile)

  value = get_map(control_id, level: level, profile: profile)
  return if value.nil? || value.empty?

  cache_set(value, control_id, level, profile)
  value
end

#map_type(control_id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 49

def map_type(control_id)
  return control_id if MAP_TYPES.include?(control_id)

  case control_id
  when %r{^c[0-9_]+$}
    'hiera_title_num'
  when %r{^[0-9][0-9.]*$}
    'number'
  when %r{^[a-z][a-z0-9_]+$}
    'hiera_title'
  when %r{^V-[0-9]{6}$}
    'vulnid'
  else
    'title'
  end
end

#titleObject



21
22
23
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 21

def title
  @title ||= benchmark_data['title']
end

#versionObject



25
26
27
# File 'lib/abide_dev_utils/cem/mapping/mapper.rb', line 25

def version
  @version ||= benchmark_data['version']
end