class OkComputer::CheckCollection
Attributes
Public Class Methods
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
# 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
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
# File lib/ok_computer/check_collection.rb, line 53 def check_names collection.keys end
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
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
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
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
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
# File lib/ok_computer/check_collection.rb, line 63 def self_and_sub_collections [collection] + sub_collections end
# File lib/ok_computer/check_collection.rb, line 59 def sub_collections checks.select{ |c| c.is_a?(CheckCollection)} end
Public: Whether all the checks succeed
Returns a Boolean
# File lib/ok_computer/check_collection.rb, line 105 def success? checks.all?(&:success?) end
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
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
Private Instance Methods
# 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
# File lib/ok_computer/check_collection.rb, line 111 def check_in_sequence checks.each(&:run) end