Class: Kriterion::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/kriterion/object.rb

Direct Known Subclasses

Event, Item, Resource, Section, Standard

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Object

Returns a new instance of Object.



3
4
5
# File 'lib/kriterion/object.rb', line 3

def initialize(data)
  @compliance = data['compliance']
end

Class Method Details

.primary_keyObject



95
96
97
# File 'lib/kriterion/object.rb', line 95

def self.primary_key
  :name
end

Instance Method Details

#compliance(objects) ⇒ Object

Returns the cahced complicance value or calculates from scratch if required



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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?Boolean

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

Returns:

  • (Boolean)


39
40
41
# File 'lib/kriterion/object.rb', line 39

def expandable?
  false
end

#expandable_keysObject



43
44
45
# File 'lib/kriterion/object.rb', line 43

def expandable_keys
  []
end

#find_section(name) ⇒ Object



47
48
49
# File 'lib/kriterion/object.rb', line 47

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

#flush_compliance! {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



78
79
80
81
82
83
84
85
86
87
88
89
# 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_keysObject



28
29
30
31
32
33
34
35
# File 'lib/kriterion/object.rb', line 28

def full_keys
  %w[
    sections
    items
    resources
    events
  ]
end

#primary_keyObject



91
92
93
# File 'lib/kriterion/object.rb', line 91

def primary_key
  self.class.primary_key
end

#to_h(mode = :basic) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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