Module: Spark

Defined in:
lib/spark.rb,
lib/spark/rake/speck_task.rb

Defined Under Namespace

Modules: Rake

Constant Summary collapse

VERSION =
0

Class Method Summary collapse

Class Method Details

.playback(target, indent = 0) ⇒ Object

“Plays” a ‘Speck`, or `Speck:Battery`, recursively. This consists of:

  • Printing data about the ‘Speck` or `Battery`

  • Executing the ‘Battery`

  • Executing the ‘Speck`(s)

  • Executing each ‘Check` belonging to the `Speck`(s)

  • Printing data about each ‘Check` and its result

  • Recursively repeating the above for each child ‘Speck` or `Battery`



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spark.rb', line 17

def self.playback target, indent = 0
  if target.respond_to? :specks and target.respond_to? :targets
    target.specks.each {|speck| Spark::playback speck }
    target.targets.each {|object, battery| Spark::playback battery }
  else
    # TODO: FUCK FUCK FUCK THIS IS UGLY CODE ARRRGH
    puts ("  " * indent) + target.target.inspect if target.target
    indent += 1
    
    target.execute
    
    checks = target.checks.group_by do |check|
      begin
        check.execute unless check.status
      rescue Speck::Exception::CheckFailed
      end
      puts ("  " * indent) + case check.status
      when :passed then (" # " + check.status.to_s).green
      when :failed then (" # " + check.status.to_s).red
      when :future then (" # " + check.status.to_s).yellow
      else              (" # " + check.status.to_s).cyan
      end
      check.status
    end
    
    child_checks = target.children
      .inject({:passed => [], :failed => []}) do |children_checks, speck|
        child_checks = Spark::playback speck, indent
        child_checks.each do |k,v|
          children_checks[k] ||= Array.new
          children_checks[k] += v || Array.new
        end
        children_checks
      end
    
    child_checks.each do |k,v|
      checks[k] ||= Array.new
      checks[k] += v || Array.new
    end
    
    
    indent -= 1
    
    # TODO: FUCK FUCK FUCK THIS IS EVEN UGLIER THAN THE ABOVE CODE!!!!1!1
    total = checks.inject(0) {|t, (k,v)| t + v.size }
    passed = checks[:passed].size
    failed = checks[:failed].size
    other = checks.inject(0) {|t, (k,v)| t += v.size unless [:passed, :failed].include? k; t }
    puts ("  " * indent) + "(#{
      checks[:failed].size > 0 ?
        checks[:passed].size.to_s.red : checks[:passed].size.to_s.green
      }#{other &&! other.zero? ? '/' + other.to_s.cyan : nil } of #{total})" unless total.zero?
    
    return checks
  end
end