Class: Dojo

Inherits:
Object
  • Object
show all
Defined in:
lib/sensei/dojo.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, git) ⇒ Dojo

Returns a new instance of Dojo.



3
4
5
6
7
8
9
10
# File 'lib/sensei/dojo.rb', line 3

def initialize(name, git)
  @git = git
  @name = name
  @iteration = 1
  @git.do("checkout iteration-1")
  @git.do("branch -D #{name}")
  @git.do("checkout -b #{name}")
end

Instance Method Details

#commit(message) ⇒ Object



101
102
103
104
# File 'lib/sensei/dojo.rb', line 101

def commit message
  @git.do("add .")
  @git.do(%Q{commit -m "#{message}"})
end

#cukes_failedObject



19
20
21
22
# File 'lib/sensei/dojo.rb', line 19

def cukes_failed
  puts 
  puts "FAIL: Your cukes failed!"
end

#flogObject



106
107
108
# File 'lib/sensei/dojo.rb', line 106

def flog
  system("flog lib/*.rb")
end

#next_iterationObject



95
96
97
98
99
# File 'lib/sensei/dojo.rb', line 95

def next_iteration
  commit("iteration #{@iteration} refactored.")
  @iteration = @iteration+1
  @git.do("merge iteration-#{@iteration}")
end

#refactoring_loopObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sensei/dojo.rb', line 110

def refactoring_loop
  go_next = false
  until go_next
    message = "Press [ENTER] to run the tests again (with refactoring tips)"
    message << ", [SPACE ENTER] to move to the next iteration" if success?
    puts message

    go_next = (STDIN.gets == " \n")
    
    unless go_next && success?
      system("rake spec features")
      if success?
        flog
      end
      go_next = false
    end
  end
end

#runObject



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
73
74
75
76
77
78
79
# File 'lib/sensei/dojo.rb', line 37

def run
  @max_iterations = IterationList.new(Dir.pwd).iterations
  until @iteration > @max_iterations

    run_cukes
    unless @cukes_passed 
      cukes_failed
      suggest_write_specs
      run_specs
      until @specs_passed
        specs_failed
        run_specs
      end
      specs_passed
      run_cukes
    end

    if success?
      if @iteration < @max_iterations
        commit("iteration #{@iteration} tests passing.")

        puts
        puts
        puts "WIN!"
        puts "Perhaps you would care for a spot of refactoring at this point?"

        refactoring_loop
        next_iteration
      else
        puts "EPIC WIN! Now to play the refactoring game..."
        until false
          system("rake spec features")
          if success?
            flog
            commit "refactoring"
          end
          puts "Not bad. Can you do any better? (Hit a key to re-run the flog)"
          STDIN.gets
        end
      end
    end
  end
end

#run_cukesObject



85
86
87
88
# File 'lib/sensei/dojo.rb', line 85

def run_cukes
  system("cucumber features")
  @cukes_passed = success?
end

#run_specsObject



90
91
92
93
# File 'lib/sensei/dojo.rb', line 90

def run_specs
  system("rake spec")
  @specs_passed = success?
end

#specs_failedObject



24
25
26
27
28
29
# File 'lib/sensei/dojo.rb', line 24

def specs_failed
  puts
  puts "FAIL: Better fix those specs!"
  puts "FAIL! Hit [ENTER] to run them when you're ready..."
  STDIN.gets
end

#specs_passedObject



31
32
33
34
35
# File 'lib/sensei/dojo.rb', line 31

def specs_passed
  puts
  puts "Your specs passed :-). Hit [ENTER] to try the cukes again"
  STDIN.gets
end

#success?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/sensei/dojo.rb', line 81

def success?
  $?.exitstatus == 0
end

#suggest_write_specsObject



12
13
14
15
16
17
# File 'lib/sensei/dojo.rb', line 12

def suggest_write_specs
  puts
  puts "FAIL: how about writing some specs in ./spec ?"
  puts "FAIL! Hit [ENTER] to run them when you're ready..."
  STDIN.gets
end