class OkComputer::CheckCollection

Attributes

collection[RW]
display[RW]
registrant_name[RW]

Public Class Methods

new(display) click to toggle source

Public: Initialize a new CheckCollection

display - the display name for the Check Collection

# File lib/ok_computer/check_collection.rb, line 8
def initialize(display)
  self.display = display
  self.collection = {}
end

Public Instance Methods

<=>(check) click to toggle source
# 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) click to toggle source

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

key - a check or collection name

# File lib/ok_computer/check_collection.rb, line 31
def [](key)
  fetch(key)
  rescue KeyError
end
check_names() click to toggle source
# File lib/ok_computer/check_collection.rb, line 53
def check_names
  collection.keys
end
Also aliased as: keys
checks() click to toggle source

Public: The list of checks in the collection

Returns an Array of the collection’s values

# File lib/ok_computer/check_collection.rb, line 39
def checks
  collection.values
end
Also aliased as: values
deregister(name) click to toggle source

Public: Deregisters a check from the collection

Returns the check

# File lib/ok_computer/check_collection.rb, line 78
def deregister(name)
  check = collection.delete(name)
end
fetch(key, default=nil) click to toggle source

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

# 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
keys()
Alias for: check_names
register(name, check) click to toggle source

Public: Registers a check into the collection

Returns the check

# File lib/ok_computer/check_collection.rb, line 70
def register(name, check)
  check.registrant_name = name
  collection[name] = check
end
run() click to toggle source

Public: Run the collection’s checks

# 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_collections() click to toggle source
# File lib/ok_computer/check_collection.rb, line 63
def self_and_sub_collections
  [collection] + sub_collections
end
sub_collections() click to toggle source
# File lib/ok_computer/check_collection.rb, line 59
def sub_collections
  checks.select{ |c| c.is_a?(CheckCollection)}
end
success?() click to toggle source

Public: Whether all the checks succeed

Returns a Boolean

# File lib/ok_computer/check_collection.rb, line 105
def success?
  checks.all?(&:success?)
end
to_json(*args) click to toggle source

Public: The JSON of each check in the collection

Returns a String containing a JSON array of hashes

# 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_text() click to toggle source

Public: The text of each check in the collection

Returns a String

# 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
values()
Alias for: checks

Private Instance Methods

check_in_parallel() click to toggle source
# File lib/ok_computer/check_collection.rb, line 115
def check_in_parallel
  threads = checks.map do |check|
    Thread.new { check.run }
  end
  threads.each(&:join)
end
check_in_sequence() click to toggle source
# File lib/ok_computer/check_collection.rb, line 111
def check_in_sequence
  checks.each(&:run)
end