Class: ComplianceEngine::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/compliance_engine/component.rb

Overview

A generic compliance engine data component

Direct Known Subclasses

Ce, Check, Control, Profile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data: nil) ⇒ Component

A generic compliance engine data component

Parameters:



12
13
14
15
# File 'lib/compliance_engine/component.rb', line 12

def initialize(name, data: nil)
  context_variables.each { |var| instance_variable_set(var, data&.instance_variable_get(var)) }
  @component ||= { key: name, fragments: {} }
end

Instance Attribute Details

#componentObject

Returns the value of attribute component.



17
18
19
# File 'lib/compliance_engine/component.rb', line 17

def component
  @component
end

#enforcement_toleranceObject

Returns the value of attribute enforcement_tolerance.



17
18
19
# File 'lib/compliance_engine/component.rb', line 17

def enforcement_tolerance
  @enforcement_tolerance
end

#environment_dataObject

Returns the value of attribute environment_data.



17
18
19
# File 'lib/compliance_engine/component.rb', line 17

def environment_data
  @environment_data
end

#factsObject

Returns the value of attribute facts.



17
18
19
# File 'lib/compliance_engine/component.rb', line 17

def facts
  @facts
end

Instance Method Details

#[](key) ⇒ Object

Return a single key from the component

Parameters:

  • key (String)

    the key of the value to return

Returns:

  • (Object)

    the value of the key



128
129
130
# File 'lib/compliance_engine/component.rb', line 128

def [](key)
  element[key]
end

#add(filename, value) ⇒ Object

Adds a value to the fragments array of the component.

Parameters:

  • value (Object)

    The value to be added to the fragments array.

Returns:

  • (Object)


56
57
58
# File 'lib/compliance_engine/component.rb', line 56

def add(filename, value)
  component[:fragments][filename] = value
end

#cesArray, Hash

Note:

This returns an Array for checks and a Hash for other components

Returns the ces of the component

Returns:

  • (Array, Hash)

    the ces of the component



120
121
122
# File 'lib/compliance_engine/component.rb', line 120

def ces
  element['ces']
end

#controlsHash

Returns the controls of the component

Returns:

  • (Hash)

    the controls of the component



105
106
107
# File 'lib/compliance_engine/component.rb', line 105

def controls
  element['controls']
end

#descriptionString

Returns the description of the component

Returns:

  • (String)

    the description of the component



91
92
93
# File 'lib/compliance_engine/component.rb', line 91

def description
  element['description']
end

#identifiersHash

Returns the identifiers of the component

Returns:

  • (Hash)

    the identifiers of the component



112
113
114
# File 'lib/compliance_engine/component.rb', line 112

def identifiers
  element['identifiers']
end

#initialize_copy(_source) ⇒ NilClass

Ensure that cloned/duped objects get independent fragment stores.

Ruby’s default clone/dup is a shallow copy, so @component (and the fragments hash nested within it) would be shared between the source and the copy. Calling add on either would write into the same fragments hash, making new fragments visible on both objects. Pre-computed @element and @fragments caches would also be shared, returning stale fact-filtered results on the copy even after facts are changed.

Duping @component and its inner fragments hash ensures each copy has an independent fragment store. Cache variables are cleared so each copy rebuilds them lazily from its own fragments on first access.

Returns:

  • (NilClass)


44
45
46
47
48
49
50
# File 'lib/compliance_engine/component.rb', line 44

def initialize_copy(_source)
  super
  @component = @component.dup
  @component[:fragments] = @component[:fragments].transform_values { |fragment| Marshal.load(Marshal.dump(fragment)) }
  cache_variables.each { |var| instance_variable_set(var, nil) }
  nil
end

#invalidate_cache(data = nil) ⇒ NilClass

Invalidate the cache of computed data

Parameters:

Returns:

  • (NilClass)


23
24
25
26
27
# File 'lib/compliance_engine/component.rb', line 23

def invalidate_cache(data = nil)
  context_variables.each { |var| instance_variable_set(var, data&.instance_variable_get(var)) }
  cache_variables.each { |var| instance_variable_set(var, nil) }
  nil
end

#keyString

Returns the key of the component

Returns:

  • (String)

    the key of the component



77
78
79
# File 'lib/compliance_engine/component.rb', line 77

def key
  component[:key]
end

#oval_idsArray

Returns the oval ids of the component

Returns:

  • (Array)

    the oval ids of the component



98
99
100
# File 'lib/compliance_engine/component.rb', line 98

def oval_ids
  element['oval-ids']
end

#titleString

Returns the title of the component

Returns:

  • (String)

    the title of the component



84
85
86
# File 'lib/compliance_engine/component.rb', line 84

def title
  element['title']
end

#to_aArray

Returns an array of fragments from the component

Returns:

  • (Array)

    an array of fragments



63
64
65
# File 'lib/compliance_engine/component.rb', line 63

def to_a
  component[:fragments].values
end

#to_hHash

Returns the merged data from the component

Returns:

  • (Hash)

    merged data



70
71
72
# File 'lib/compliance_engine/component.rb', line 70

def to_h
  element
end