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) ⇒ Object
Iterates over the collection.
-
#each_key(&block) ⇒ Object
Iterates over keys in the collection.
-
#each_value(&block) ⇒ Object
Iterates over values in the collection.
-
#initialize(data) ⇒ Collection
constructor
A generic compliance engine data collection.
-
#invalidate_cache(data = nil) ⇒ NilClass
Invalidate the cache of computed data.
-
#keys ⇒ Array
Returns the keys of the collection.
-
#reject(&block) ⇒ Hash
Filter out values in the collection.
-
#select(&block) ⇒ Hash
Select values in the collection.
-
#to_h ⇒ Hash
Converts the object to a hash representation.
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
53 54 55 |
# File 'lib/compliance_engine/collection.rb', line 53 def [](key) collection[key] end |
#all?(&block) ⇒ TrueClass, FalseClass
Return true if all of the values in the collection match the block
90 91 92 |
# File 'lib/compliance_engine/collection.rb', line 90 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
82 83 84 |
# File 'lib/compliance_engine/collection.rb', line 82 def any?(&block) to_h.any?(&block) end |
#each(&block) ⇒ Object
Iterates over the collection
60 61 62 |
# File 'lib/compliance_engine/collection.rb', line 60 def each(&block) to_h.each(&block) end |
#each_key(&block) ⇒ Object
Iterates over keys in the collection
74 75 76 |
# File 'lib/compliance_engine/collection.rb', line 74 def each_key(&block) to_h.each_key(&block) end |
#each_value(&block) ⇒ Object
Iterates over values in the collection
67 68 69 |
# File 'lib/compliance_engine/collection.rb', line 67 def each_value(&block) to_h.each_value(&block) 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
45 46 47 |
# File 'lib/compliance_engine/collection.rb', line 45 def keys to_h.keys end |
#reject(&block) ⇒ Hash
Filter out values in the collection
106 107 108 |
# File 'lib/compliance_engine/collection.rb', line 106 def reject(&block) to_h.reject(&block) end |
#select(&block) ⇒ Hash
Select values in the collection
98 99 100 |
# File 'lib/compliance_engine/collection.rb', line 98 def select(&block) to_h.select(&block) end |
#to_h ⇒ Hash
Converts the object to a hash representation
38 39 40 |
# File 'lib/compliance_engine/collection.rb', line 38 def to_h collection.reject { |k, _| k.is_a?(Symbol) } end |