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, exclude_skipped_checks = false) ⇒ CheckCollection

Public: Initialize a new CheckCollection

display - the display name for the Check Collection exclude_skipped_checks - whether checks marked skip_all should be omitted



9
10
11
12
13
14
# File 'lib/ok_computer/check_collection.rb', line 9

def initialize(display, exclude_skipped_checks=false)
  self.display = display
  self.collection = {}
  self.skip_all = false
  @exclude_skipped_checks = exclude_skipped_checks
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

#skip_allObject

Returns the value of attribute skip_all.



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

def skip_all
  @skip_all
end

Instance Method Details

#<=>(check) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/ok_computer/check_collection.rb', line 46

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



34
35
36
37
# File 'lib/ok_computer/check_collection.rb', line 34

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

#check_namesObject Also known as: keys



56
57
58
# File 'lib/ok_computer/check_collection.rb', line 56

def check_names
  included_collection.keys
end

#checksObject Also known as: values

Public: The list of checks in the collection

Returns an Array of the collection's values



42
43
44
# File 'lib/ok_computer/check_collection.rb', line 42

def checks
  included_collection.values
end

#deregister(name) ⇒ Object

Public: Deregisters a check from the collection

Returns the check



81
82
83
# File 'lib/ok_computer/check_collection.rb', line 81

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)


25
26
27
28
29
# File 'lib/ok_computer/check_collection.rb', line 25

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



73
74
75
76
# File 'lib/ok_computer/check_collection.rb', line 73

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

#runObject

Public: Run the collection's checks



17
18
19
# File 'lib/ok_computer/check_collection.rb', line 17

def run
  OkComputer.check_in_parallel ? check_in_parallel : check_in_sequence
end

#self_and_sub_collectionsObject



66
67
68
# File 'lib/ok_computer/check_collection.rb', line 66

def self_and_sub_collections
  [collection] + sub_collections
end

#sub_collectionsObject



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

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

#success?Boolean

Public: Whether all the checks succeed

Returns a Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/ok_computer/check_collection.rb', line 108

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



95
96
97
98
99
100
101
102
103
# File 'lib/ok_computer/check_collection.rb', line 95

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



88
89
90
# File 'lib/ok_computer/check_collection.rb', line 88

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