Class: ComplianceEngine::Collection
- Inherits:
-
Object
- Object
- ComplianceEngine::Collection
- Defined in:
- lib/compliance_engine/collection.rb
Overview
A generic compliance engine data collection
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#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 value from the collection.
-
#all?(&block) ⇒ TrueClass, FalseClass
Return true if all of the values in the collection match the block.
-
#any?(&block) ⇒ TrueClass, FalseClass
Return true if any of the values in the collection match the block.
-
#each(&block) ⇒ self, Enumerator
Iterates over the collection.
-
#each_key(&block) ⇒ self, Enumerator
Iterates over keys in the collection.
-
#each_value(&block) ⇒ self, Enumerator
Iterates over values in the collection.
-
#initialize(data) ⇒ Collection
constructor
A generic compliance engine data collection.
-
#initialize_copy(_source) ⇒ NilClass
Ensure that cloned/duped objects get independent component instances.
-
#invalidate_cache(data = nil) ⇒ NilClass
Invalidate the cache of computed data.
-
#keys ⇒ Array
Returns the keys of the collection.
-
#reject(&block) ⇒ ComplianceEngine::Collection, Enumerator
Filter out values in the collection.
-
#select(&block) ⇒ ComplianceEngine::Collection, Enumerator
(also: #filter)
Select values in the collection.
-
#to_h ⇒ Hash
Converts the object to a hash representation.
-
#transform_values(&block) ⇒ Hash, Enumerator
Transform values in the collection.
-
#values ⇒ Array
Returns the values of the collection.
Constructor Details
#initialize(data) ⇒ Collection
A generic compliance engine data collection
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/compliance_engine/collection.rb', line 10 def initialize(data) context_variables.each { |var| instance_variable_set(var, data.instance_variable_get(var)) } @collection ||= {} hash_key = key data.files.each do |file| data.get(file)[hash_key]&.each do |k, v| @collection[k] ||= collected.new(k, data: self) @collection[k].add(file, v) end end end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
22 23 24 |
# File 'lib/compliance_engine/collection.rb', line 22 def collection @collection end |
#enforcement_tolerance ⇒ Object
Returns the value of attribute enforcement_tolerance.
22 23 24 |
# File 'lib/compliance_engine/collection.rb', line 22 def enforcement_tolerance @enforcement_tolerance end |
#environment_data ⇒ Object
Returns the value of attribute environment_data.
22 23 24 |
# File 'lib/compliance_engine/collection.rb', line 22 def environment_data @environment_data end |
#facts ⇒ Object
Returns the value of attribute facts.
22 23 24 |
# File 'lib/compliance_engine/collection.rb', line 22 def facts @facts end |
Instance Method Details
#[](key) ⇒ Object
Return a single value from the collection
81 82 83 |
# File 'lib/compliance_engine/collection.rb', line 81 def [](key) collection[key] end |
#all?(&block) ⇒ TrueClass, FalseClass
Return true if all of the values in the collection match the block
130 131 132 |
# File 'lib/compliance_engine/collection.rb', line 130 def all?(&block) to_h.all?(&block) end |
#any?(&block) ⇒ TrueClass, FalseClass
Return true if any of the values in the collection match the block
122 123 124 |
# File 'lib/compliance_engine/collection.rb', line 122 def any?(&block) to_h.any?(&block) end |
#each(&block) ⇒ self, Enumerator
Iterates over the collection
89 90 91 92 93 94 |
# File 'lib/compliance_engine/collection.rb', line 89 def each(&block) return to_enum(:each) unless block to_h.each(&block) self end |
#each_key(&block) ⇒ self, Enumerator
Iterates over keys in the collection
111 112 113 114 115 116 |
# File 'lib/compliance_engine/collection.rb', line 111 def each_key(&block) return to_enum(:each_key) unless block to_h.each_key(&block) self end |
#each_value(&block) ⇒ self, Enumerator
Iterates over values in the collection
100 101 102 103 104 105 |
# File 'lib/compliance_engine/collection.rb', line 100 def each_value(&block) return to_enum(:each_value) unless block to_h.each_value(&block) self end |
#initialize_copy(_source) ⇒ NilClass
Ensure that cloned/duped objects get independent component instances.
Ruby’s default clone/dup is a shallow copy, so @collection would be shared between the source and the copy, and every Component inside it would be the same object. Calling invalidate_cache on one copy would then propagate its facts into those shared components, silently affecting the other copy’s view of the data.
Duping each Component (which triggers Component#initialize_copy) gives every copy of the Collection its own independent components. Any derived caches on the Collection itself (e.g. @by_oval_id in Ces) are cleared so each copy rebuilds them from its own component set.
49 50 51 52 53 54 |
# File 'lib/compliance_engine/collection.rb', line 49 def initialize_copy(_source) super @collection = @collection.transform_values(&:dup) (instance_variables - (context_variables + [:@collection])).each { |var| instance_variable_set(var, nil) } nil end |
#invalidate_cache(data = nil) ⇒ NilClass
Invalidate the cache of computed data
28 29 30 31 32 33 |
# File 'lib/compliance_engine/collection.rb', line 28 def invalidate_cache(data = nil) context_variables.each { |var| instance_variable_set(var, data&.instance_variable_get(var)) } collection.each_value { |obj| obj.invalidate_cache(data) } (instance_variables - (context_variables + [:@collection])).each { |var| instance_variable_set(var, nil) } nil end |
#keys ⇒ Array
Returns the keys of the collection
66 67 68 |
# File 'lib/compliance_engine/collection.rb', line 66 def keys to_h.keys end |
#reject(&block) ⇒ ComplianceEngine::Collection, Enumerator
Filter out values in the collection
152 153 154 155 156 157 158 |
# File 'lib/compliance_engine/collection.rb', line 152 def reject(&block) return to_enum(:reject) unless block_given? result = dup result.collection = result.to_h.reject(&block) result end |
#select(&block) ⇒ ComplianceEngine::Collection, Enumerator Also known as: filter
Select values in the collection
138 139 140 141 142 143 144 |
# File 'lib/compliance_engine/collection.rb', line 138 def select(&block) return to_enum(:select) unless block_given? result = dup result.collection = result.to_h.select(&block) result end |
#to_h ⇒ Hash
Converts the object to a hash representation
59 60 61 |
# File 'lib/compliance_engine/collection.rb', line 59 def to_h collection.reject { |k, _| k.is_a?(Symbol) } end |
#transform_values(&block) ⇒ Hash, Enumerator
Transform values in the collection
164 165 166 |
# File 'lib/compliance_engine/collection.rb', line 164 def transform_values(&block) to_h.transform_values(&block) end |
#values ⇒ Array
Returns the values of the collection
73 74 75 |
# File 'lib/compliance_engine/collection.rb', line 73 def values to_h.values end |