class Kriterion::Object

Public Class Methods

new(data) click to toggle source
# File lib/kriterion/object.rb, line 3
def initialize(data)
  @compliance = data['compliance']
end
primary_key() click to toggle source
# File lib/kriterion/object.rb, line 95
def self.primary_key
  :name
end

Public Instance Methods

compliance(objects) click to toggle source

Returns the cahced complicance value or calculates from scratch if required

# File lib/kriterion/object.rb, line 53
def compliance(objects)
  # Returns cached value if it exists
  return @compliance if @compliance

  # Calculate compliance
  total         = objects.count
  compliant     = objects.count { |o| o.compliance['compliant'] }
  non_compliant = total - compliant
  percentage    = if total.zero?
                    0
                  else
                    compliant / total
                  end

  {
    'compliant' => percentage == 1,
    'events'    => {
      'percentage'    => percentage,
      'compliant'     => compliant,
      'non_compliant' => non_compliant,
      'total'         => total
    }
  }
end
expandable?() click to toggle source

Objects should deflault to not being expandable unless someone has specifided it

# File lib/kriterion/object.rb, line 39
def expandable?
  false
end
expandable_keys() click to toggle source
# File lib/kriterion/object.rb, line 43
def expandable_keys
  []
end
find_section(name) click to toggle source
# File lib/kriterion/object.rb, line 47
def find_section(name)
  sections ? sections.select { |s| s.name == name }[0] : nil
end
flush_compliance!() { |thing| ... } click to toggle source
# File lib/kriterion/object.rb, line 78
def flush_compliance!
  @compliance = nil
  # Flush the compliance of all children also
  expandable_keys.each do |key|
    send(key).each do |thing|
      thing.flush_compliance!
      yield(thing) if block_given?
    end
  end
  yield(self) if block_given?
  compliance
end
full_keys() click to toggle source
# File lib/kriterion/object.rb, line 28
def full_keys
  %w[
    sections
    items
    resources
    events
  ]
end
primary_key() click to toggle source
# File lib/kriterion/object.rb, line 91
def primary_key
  self.class.primary_key
end
to_h(mode = :basic) click to toggle source
# File lib/kriterion/object.rb, line 7
def to_h(mode = :basic)
  raise 'Mode must be :basic or :full' unless %i[basic full].include? mode
  hash = {}

  # Add all instance variables to the hash without the @ sign
  instance_variables.each do |v|
    hash[v.to_s.gsub(/^@/, '')] = instance_variable_get(v.to_s)
  end

  if mode == :basic
    hash.reject do |k, _v|
      full_keys.include? k
    end
  elsif mode == :full
    expandable_keys.each do |key|
      hash[key.to_s] = send(key).map { |x| x.to_h(:full) }
    end
    hash
  end
end