Class: OkComputer::CheckCollection
- Inherits:
-
Object
- Object
- OkComputer::CheckCollection
- Defined in:
- lib/ok_computer/check_collection.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#display ⇒ Object
Returns the value of attribute display.
-
#registrant_name ⇒ Object
Returns the value of attribute registrant_name.
Instance Method Summary collapse
- #<=>(check) ⇒ Object
-
#[](key) ⇒ Object
Public: Returns a check or collection if it’s in the check collection.
- #check_names ⇒ Object (also: #keys)
-
#checks ⇒ Object
(also: #values)
Public: The list of checks in the collection.
-
#deregister(name) ⇒ Object
Public: Deregisters a check from the collection.
-
#fetch(key, default = nil) ⇒ Object
Public: Returns a check or collection if it’s in the check collection.
-
#initialize(display) ⇒ CheckCollection
constructor
Public: Initialize a new CheckCollection.
-
#register(name, check) ⇒ Object
Public: Registers a check into the collection.
-
#run ⇒ Object
Public: Run the collection’s checks.
- #self_and_sub_collections ⇒ Object
- #sub_collections ⇒ Object
-
#success? ⇒ Boolean
Public: Whether all the checks succeed.
-
#to_json(*args) ⇒ Object
Public: The JSON of each check in the collection.
-
#to_text ⇒ Object
Public: The text of each check in the collection.
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
#collection ⇒ Object
Returns the value of attribute collection.
3 4 5 |
# File 'lib/ok_computer/check_collection.rb', line 3 def collection @collection end |
#display ⇒ Object
Returns the value of attribute display.
3 4 5 |
# File 'lib/ok_computer/check_collection.rb', line 3 def display @display end |
#registrant_name ⇒ Object
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_names ⇒ Object Also known as: keys
53 54 55 |
# File 'lib/ok_computer/check_collection.rb', line 53 def check_names collection.keys end |
#checks ⇒ Object 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
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 |
#run ⇒ Object
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_collections ⇒ Object
63 64 65 |
# File 'lib/ok_computer/check_collection.rb', line 63 def self_and_sub_collections [collection] + sub_collections end |
#sub_collections ⇒ Object
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
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_text ⇒ Object
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 |