Class: ComplianceEngine::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/compliance_engine/collection.rb

Overview

A generic compliance engine data collection

Direct Known Subclasses

Ces, Checks, Controls, Profiles

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Collection

A generic compliance engine data collection

Parameters:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/compliance_engine/collection.rb', line 17

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|
    file_data = data.get(file)
    unless file_data.is_a?(Hash)
      ComplianceEngine.log.debug "Skipping #{file}: expected Hash content, got #{file_data.class}"
      next
    end

    entries = file_data[hash_key]
    next if entries.nil?

    unless entries.is_a?(Hash)
      ComplianceEngine.log.error "Expected '#{hash_key}' in #{file} to be a Hash, got #{entries.class}"
      next
    end

    entries.each do |k, v|
      @collection[k] ||= collected.new(k, data: self)
      @collection[k].add(file, v)
    end
  end
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



43
44
45
# File 'lib/compliance_engine/collection.rb', line 43

def collection
  @collection
end

#enforcement_toleranceObject

Returns the value of attribute enforcement_tolerance.



43
44
45
# File 'lib/compliance_engine/collection.rb', line 43

def enforcement_tolerance
  @enforcement_tolerance
end

#environment_dataObject

Returns the value of attribute environment_data.



43
44
45
# File 'lib/compliance_engine/collection.rb', line 43

def environment_data
  @environment_data
end

#factsObject

Returns the value of attribute facts.



43
44
45
# File 'lib/compliance_engine/collection.rb', line 43

def facts
  @facts
end

Instance Method Details

#[](key) ⇒ Object

Return a single value from the collection

Parameters:

  • key (String)

    the key of the value to return

Returns:

  • (Object)

    the value of the key



102
103
104
# File 'lib/compliance_engine/collection.rb', line 102

def [](key)
  collection[key]
end

#all?(&block) ⇒ TrueClass, FalseClass

Return true if all of the values in the collection match the block

Parameters:

  • block (Proc)

    the block to execute

Returns:

  • (TrueClass, FalseClass)

    true if all of the values in the collection match the block



151
152
153
# File 'lib/compliance_engine/collection.rb', line 151

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

Parameters:

  • block (Proc)

    the block to execute

Returns:

  • (TrueClass, FalseClass)

    true if any of the values in the collection match the block



143
144
145
# File 'lib/compliance_engine/collection.rb', line 143

def any?(&block)
  to_h.any?(&block)
end

#each(&block) ⇒ self, Enumerator

Iterates over the collection

Parameters:

  • block (Proc)

    the block to execute

Returns:

  • (self, Enumerator)


110
111
112
113
114
115
# File 'lib/compliance_engine/collection.rb', line 110

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

Parameters:

  • block (Proc)

    the block to execute

Returns:

  • (self, Enumerator)


132
133
134
135
136
137
# File 'lib/compliance_engine/collection.rb', line 132

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

Parameters:

  • block (Proc)

    the block to execute

Returns:

  • (self, Enumerator)


121
122
123
124
125
126
# File 'lib/compliance_engine/collection.rb', line 121

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.

Returns:

  • (NilClass)


70
71
72
73
74
75
# File 'lib/compliance_engine/collection.rb', line 70

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

Parameters:

Returns:

  • (NilClass)


49
50
51
52
53
54
# File 'lib/compliance_engine/collection.rb', line 49

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

#keysArray

Returns the keys of the collection

Returns:

  • (Array)

    the keys of the collection



87
88
89
# File 'lib/compliance_engine/collection.rb', line 87

def keys
  to_h.keys
end

#reject(&block) ⇒ ComplianceEngine::Collection, Enumerator

Filter out values in the collection

Parameters:

  • block (Proc)

    the block to execute

Returns:



173
174
175
176
177
178
179
# File 'lib/compliance_engine/collection.rb', line 173

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

Parameters:

  • block (Proc)

    the block to execute

Returns:



159
160
161
162
163
164
165
# File 'lib/compliance_engine/collection.rb', line 159

def select(&block)
  return to_enum(:select) unless block_given?

  result = dup
  result.collection = result.to_h.select(&block)
  result
end

#to_hHash

Converts the object to a hash representation

Returns:

  • (Hash)

    the hash representation of the object



80
81
82
# File 'lib/compliance_engine/collection.rb', line 80

def to_h
  collection.reject { |k, _| k.is_a?(Symbol) }
end

#transform_values(&block) ⇒ Hash, Enumerator

Transform values in the collection

Parameters:

  • block (Proc)

    the block to execute

Returns:

  • (Hash, Enumerator)

    a hash with transformed values, or an Enumerator when no block is given



185
186
187
# File 'lib/compliance_engine/collection.rb', line 185

def transform_values(&block)
  to_h.transform_values(&block)
end

#valuesArray

Returns the values of the collection

Returns:

  • (Array)

    the values of the collection



94
95
96
# File 'lib/compliance_engine/collection.rb', line 94

def values
  to_h.values
end