Class: ComplianceEngine::Component
- Inherits:
-
Object
- Object
- ComplianceEngine::Component
- Defined in:
- lib/compliance_engine/component.rb
Overview
A generic compliance engine data component
Instance Attribute Summary collapse
-
#component ⇒ Object
Returns the value of attribute component.
-
#enforcement_tolerance ⇒ Object
Returns the value of attribute enforcement_tolerance.
-
#environment_data ⇒ Object
Returns the value of attribute environment_data.
-
#facts ⇒ Object
Returns the value of attribute facts.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return a single key from the component.
-
#add(filename, value) ⇒ Object
Adds a value to the fragments array of the component.
-
#ces ⇒ Array, Hash
Returns the ces of the component.
-
#controls ⇒ Hash
Returns the controls of the component.
-
#description ⇒ String
Returns the description of the component.
-
#identifiers ⇒ Hash
Returns the identifiers of the component.
-
#initialize(name, data: nil) ⇒ Component
constructor
A generic compliance engine data component.
-
#initialize_copy(_source) ⇒ NilClass
Ensure that cloned/duped objects get independent fragment stores.
-
#invalidate_cache(data = nil) ⇒ NilClass
Invalidate the cache of computed data.
-
#key ⇒ String
Returns the key of the component.
-
#oval_ids ⇒ Array
Returns the oval ids of the component.
-
#title ⇒ String
Returns the title of the component.
-
#to_a ⇒ Array
Returns an array of fragments from the component.
-
#to_h ⇒ Hash
Returns the merged data from the component.
Constructor Details
#initialize(name, data: nil) ⇒ Component
A generic compliance engine data component
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
#component ⇒ Object
Returns the value of attribute component.
17 18 19 |
# File 'lib/compliance_engine/component.rb', line 17 def component @component end |
#enforcement_tolerance ⇒ Object
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_data ⇒ Object
Returns the value of attribute environment_data.
17 18 19 |
# File 'lib/compliance_engine/component.rb', line 17 def environment_data @environment_data end |
#facts ⇒ Object
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
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.
56 57 58 |
# File 'lib/compliance_engine/component.rb', line 56 def add(filename, value) component[:fragments][filename] = value end |
#ces ⇒ Array, Hash
This returns an Array for checks and a Hash for other components
Returns the ces of the component
120 121 122 |
# File 'lib/compliance_engine/component.rb', line 120 def ces element['ces'] end |
#controls ⇒ Hash
Returns the controls of the component
105 106 107 |
# File 'lib/compliance_engine/component.rb', line 105 def controls element['controls'] end |
#description ⇒ String
Returns the description of the component
91 92 93 |
# File 'lib/compliance_engine/component.rb', line 91 def description element['description'] end |
#identifiers ⇒ Hash
Returns 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.
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
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 |
#key ⇒ String
Returns the key of the component
77 78 79 |
# File 'lib/compliance_engine/component.rb', line 77 def key component[:key] end |
#oval_ids ⇒ Array
Returns the oval ids of the component
98 99 100 |
# File 'lib/compliance_engine/component.rb', line 98 def oval_ids element['oval-ids'] end |
#title ⇒ String
Returns the title of the component
84 85 86 |
# File 'lib/compliance_engine/component.rb', line 84 def title element['title'] end |
#to_a ⇒ Array
Returns an array of fragments from the component
63 64 65 |
# File 'lib/compliance_engine/component.rb', line 63 def to_a component[:fragments].values end |
#to_h ⇒ Hash
Returns the merged data from the component
70 71 72 |
# File 'lib/compliance_engine/component.rb', line 70 def to_h element end |