Class: OkComputer::CheckCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/ok_computer/check_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(display) ⇒ CheckCollection

Public: Initialize a new CheckCollection

display - the display name for the Check Collection



8
9
10
11
# File 'lib/ok_computer/check_collection.rb', line 8

def initialize(display)
  self.display = display
  self.collection = {}
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



3
4
5
# File 'lib/ok_computer/check_collection.rb', line 3

def collection
  @collection
end

#displayObject

Returns the value of attribute display.



3
4
5
# File 'lib/ok_computer/check_collection.rb', line 3

def display
  @display
end

#registrant_nameObject

Returns the value of attribute registrant_name.



3
4
5
# File 'lib/ok_computer/check_collection.rb', line 3

def registrant_name
  @registrant_name
end

Instance Method Details

#<=>(check) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ok_computer/check_collection.rb', line 43

def <=>(check)
  if check.is_a?(CheckCollection)
    registrant_name <=> check.registrant_name
  else
    1
  end
end

#[](key) ⇒ Object

Public: Returns a check or collection if it’s in the check collection

key - a check or collection name



31
32
33
34
# File 'lib/ok_computer/check_collection.rb', line 31

def [](key)
  fetch(key)
  rescue KeyError
end

#check_namesObject Also known as: keys



53
54
55
# File 'lib/ok_computer/check_collection.rb', line 53

def check_names
  collection.keys
end

#checksObject Also known as: values

Public: The list of checks in the collection

Returns an Array of the collection’s values



39
40
41
# File 'lib/ok_computer/check_collection.rb', line 39

def checks
  collection.values
end

#deregister(name) ⇒ Object

Public: Deregisters a check from the collection

Returns the check



78
79
80
# File 'lib/ok_computer/check_collection.rb', line 78

def deregister(name)
  check = collection.delete(name)
end

#fetch(key, default = nil) ⇒ Object

Public: Returns a check or collection if it’s in the check collection

key - a check or collection name throws a KeyError when the key is not found

Raises:

  • (KeyError)


22
23
24
25
26
# File 'lib/ok_computer/check_collection.rb', line 22

def fetch(key, default=nil)
  found_in = self_and_sub_collections.detect{ |c| c[key] }
  raise KeyError unless found_in
  found_in[key]
end

#register(name, check) ⇒ Object

Public: Registers a check into the collection

Returns the check



70
71
72
73
# File 'lib/ok_computer/check_collection.rb', line 70

def register(name, check)
  check.registrant_name = name
  collection[name] = check
end

#runObject

Public: Run the collection’s checks



14
15
16
# File 'lib/ok_computer/check_collection.rb', line 14

def run
  OkComputer.check_in_parallel ? check_in_parallel : check_in_sequence
end

#self_and_sub_collectionsObject



63
64
65
# File 'lib/ok_computer/check_collection.rb', line 63

def self_and_sub_collections
  [collection] + sub_collections
end

#sub_collectionsObject



59
60
61
# File 'lib/ok_computer/check_collection.rb', line 59

def sub_collections
  checks.select{ |c| c.is_a?(CheckCollection)}
end

#success?Boolean

Public: Whether all the checks succeed

Returns a Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ok_computer/check_collection.rb', line 105

def success?
  checks.all?(&:success?)
end

#to_json(*args) ⇒ Object

Public: The JSON of each check in the collection

Returns a String containing a JSON array of hashes



92
93
94
95
96
97
98
99
100
# File 'lib/ok_computer/check_collection.rb', line 92

def to_json(*args)
  # smooshing their #to_json objects into one JSON hash
  combined = {}
  checks.each do |check|
    combined.merge!(JSON.parse(check.to_json))
  end

  combined.to_json
end

#to_textObject

Public: The text of each check in the collection

Returns a String



85
86
87
# File 'lib/ok_computer/check_collection.rb', line 85

def to_text
  "#{display}\n#{checks.sort.map{ |c| "#{"\s\s" unless c.is_a?(CheckCollection)}#{c.to_text}"}.join("\n")}"
end