Sha256: 4a93551ebd8339d05679c347590eb1fad0dea8443c1f5b76a7699883397dd3f4

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

class Kriterion
  class Object
    def to_h(mode = :basic)
      raise 'Mode must be :basic or :full' unless %i[basic full].include? mode
      hash = {}

      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|
          %w[
            sections
            items
            resources
            events
          ].include? k
        end
      elsif mode == :full
        hash
      end
    end

    def find_section(name)
      sections ? sections.select { |s| s.name == name }[0] : nil
    end

    def compliance(objects)
      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kriterion-0.0.1 lib/kriterion/object.rb