Class: Guard::Reloader

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/reloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Reloader

Returns a new instance of Reloader.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/guard/reloader.rb', line 11

def initialize(watchers=[], options={})
  super
  @options = {
    :all_on_start   => true,
    :all_after_pass => true,
    :keep_failed    => true
  }.update(options)
  ::Guard::UI.info("Guard::Reloader #{ReloaderVersion::VERSION} setting up test environment... this may take some time", :reset => true)
  require 'test/test_helper'
  require 'test/unit/ui/console/testrunner'

#      @runner = Runner.new(options)
end

Instance Method Details

#camelize(lower_case_and_underscored_word) ⇒ Object



31
32
33
# File 'lib/guard/reloader.rb', line 31

def camelize(lower_case_and_underscored_word)
  lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

#class_symbol(class_name) ⇒ Object



121
122
123
# File 'lib/guard/reloader.rb', line 121

def class_symbol(class_name)
  class_name.split('::')[-1].to_sym
end

#controller?(p) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/guard/reloader.rb', line 62

def controller?(p)
  !!%r{^app/controllers/(.+)\.rb$}.match(p)
end

#execute_test(klass) ⇒ Object

passed = @runner.run(paths)



107
108
109
110
111
112
113
# File 'lib/guard/reloader.rb', line 107

def execute_test(klass)
  begin
    ::Guard::UI.info("run test for #{klass.name}")
    Test::Unit::UI::Console::TestRunner.run(klass) if klass
#        rescue Exception => ex find what kind of exception to handle first
  end
end

#functional_test?(p) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/guard/reloader.rb', line 59

def functional_test?(p)
  !!%r{^test/functional/(.+)\.rb$}.match(p)
end

#get_controller_name(p) ⇒ Object



38
39
40
41
# File 'lib/guard/reloader.rb', line 38

def get_controller_name(p)
  m = %r{^app/controllers/(.+)\.rb$}.match(p)
  camelize(m[1]) if m[1]
end

#get_functional_test_path(p) ⇒ Object



46
47
48
49
# File 'lib/guard/reloader.rb', line 46

def get_functional_test_path(p)
  m = %r{^app/controllers/(.+)\.rb$}.match(p)
  "test/functional/#{m[1]}_test.rb"
end

#get_model_name(p) ⇒ Object



34
35
36
37
# File 'lib/guard/reloader.rb', line 34

def get_model_name(p)
  m = %r{^app/models/(.+)\.rb$}.match(p)
  camelize(m[1]) if m[1]
end

#get_test_name(p) ⇒ Object



50
51
52
53
54
55
# File 'lib/guard/reloader.rb', line 50

def get_test_name(p)
  m = %r{^test/(unit|functional)/(.+)\.rb$}.match(p)
  if m[2]
    camelize(m[2])
  end
end

#get_unit_test_path(p) ⇒ Object



42
43
44
45
# File 'lib/guard/reloader.rb', line 42

def get_unit_test_path(p)
  m = %r{^app/models/(.+)\.rb$}.match(p)
  "test/unit/#{m[1]}_test.rb"
end

#json?(p) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# File 'lib/guard/reloader.rb', line 68

def json?(p)
  begin
    map = JSON.parse(p)
    !!map["path"]
  rescue
    false
  end
end

#model?(p) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/guard/reloader.rb', line 65

def model?(p)
  !!%r{^app/models/(.+)\.rb$}.match(p)
end

#reload(klass_name, path) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/guard/reloader.rb', line 124

def reload(klass_name, path)
  begin
    klass = to_class(klass_name)
    if klass
      ::Guard::UI.info("Killing #{klass_name} class", :reset => true)
      klass.parent.send(:remove_const, class_symbol(klass_name))
    end
    load(path)
    ::Guard::UI.info("#{klass_name} on #{path} reloader process is done!", :reset => true)
    to_class(klass_name)
  rescue Exception => ex
    puts ex
  end
end

#run_on_change(paths) ⇒ Object



102
103
104
105
106
# File 'lib/guard/reloader.rb', line 102

def run_on_change(paths)
 paths.each {|p| smart_test(p) }
#      paths  = Inspector.clean(paths)
#      passed = @runner.run(paths)
end

#smart_test(path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/guard/reloader.rb', line 76

def smart_test(path)
  if model?(path)
    ::Guard::UI.info("Detected #{path} as model!", :reset => true)
    model_class = reload(get_model_name(path), path)
    test_path = get_unit_test_path(path)
    test_class = reload(get_test_name(test_path),test_path)
    execute_test(test_class) if test_class
  elsif controller?(path)
    ::Guard::UI.info("Detected #{path} as controller!", :reset => true)
    controller_class = reload(get_controller_name(path), path)
    test_path = get_functional_test_path(path)
    test_class = reload(get_test_name(test_path),test_path)
    execute_test(test_class) if test_class
  elsif unit_test?(path)
    ::Guard::UI.info("Detected #{path} as unit test!", :reset => true)
    test_class = reload(get_test_name(path),path)
    execute_test(test_class) if test_class
  elsif functional_test?(path)
    ::Guard::UI.info("Detected #{path} as functional test!", :reset => true)
    test_class = reload(get_test_name(path),path)
    execute_test(test_class) if test_class
  else
    ::Guard::UI.info("Give up on #{path}", :reset => true)
  end
  ::Guard::UI.info("Process for #{path} is done!\n\n", :reset => true)
end

#startObject



25
26
27
28
# File 'lib/guard/reloader.rb', line 25

def start
  ::Guard::UI.info("Guard::Reloader #{ReloaderVersion::VERSION} is running, with Test::Unit #{::Test::Unit::VERSION}!", :reset => true)
  run_all if @options[:all_on_start]
end

#to_class(class_name) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/guard/reloader.rb', line 114

def to_class(class_name)
  begin
    k = class_name.split('::').inject(Object) {|p, x| p.const_get(x.to_sym) }
  rescue NameError => ex
    puts "#{class_name} class doesn't exists"
  end
end

#unit_test?(p) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/guard/reloader.rb', line 56

def unit_test?(p)
  !!%r{^test/unit/(.+)\.rb$}.match(p)
end