Class: Beholder

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

Constant Summary collapse

DEFAULT_RUNNER =
'ruby'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBeholder

Returns a new instance of Beholder.



55
56
57
58
59
60
# File 'lib/beholder.rb', line 55

def initialize
  @paths_to_watch = []
  @mappings, @treasure_maps = {}, {}
  @sent_an_int = false
  @be_verbose = ARGV.include?("-v") || ARGV.include?("--verbose")
end

Class Attribute Details

.possible_treasure_map_locationsObject



14
15
16
# File 'lib/beholder.rb', line 14

def possible_treasure_map_locations
  @possible_treasure_map_locations ||= ["#{Dir.pwd}/.treasure_map.rb", "#{Dir.pwd}/treasure_map.rb", "#{Dir.pwd}/config/treasure_map.rb"]
end

.runnerObject



10
11
12
# File 'lib/beholder.rb', line 10

def runner
  @runner ||= ::Beholder::DEFAULT_RUNNER
end

.test_typesObject



18
19
20
# File 'lib/beholder.rb', line 18

def test_types
  @test_types ||= %w{spec examples test}
end

Instance Attribute Details

#be_verboseObject (readonly)

Returns the value of attribute be_verbose.



52
53
54
# File 'lib/beholder.rb', line 52

def be_verbose
  @be_verbose
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



52
53
54
# File 'lib/beholder.rb', line 52

def mappings
  @mappings
end

#paths_to_watchObject (readonly)

Returns the value of attribute paths_to_watch.



52
53
54
# File 'lib/beholder.rb', line 52

def paths_to_watch
  @paths_to_watch
end

#sent_an_intObject (readonly)

Returns the value of attribute sent_an_int.



52
53
54
# File 'lib/beholder.rb', line 52

def sent_an_int
  @sent_an_int
end

#treasure_mapsObject (readonly)

Returns the value of attribute treasure_maps.



53
54
55
# File 'lib/beholder.rb', line 53

def treasure_maps
  @treasure_maps
end

#watcherObject (readonly)

Returns the value of attribute watcher.



53
54
55
# File 'lib/beholder.rb', line 53

def watcher
  @watcher
end

Class Method Details

.all_testsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/beholder.rb', line 35

def all_tests
  lambda { 
    dirs = []
    test_directories.each do |dir|
      test_extensions.each do |test_ext|
        files = Dir["#{dir}/**/*_#{test_ext}.rb"]
        # Ignore tarantula tests for now until we add a cleaner way
        files.reject! { |file| file.include?('tarantula/') }
        next if files.empty?
        dirs << files
      end
    end
    dirs.flatten!
  }.call
end

.runObject



68
69
70
71
72
# File 'lib/beholder.rb', line 68

def self.run
  beholder = new
  beholder.run
  self
end

.test_directoriesObject



26
27
28
29
30
31
32
33
# File 'lib/beholder.rb', line 26

def test_directories
  return @test_directories if @test_directories
  @test_directories = []
  test_types.each do |test_type|
    @test_directories << test_type if File.exist?(test_type) 
  end
  @test_directories
end

.test_extensionsObject



22
23
24
# File 'lib/beholder.rb', line 22

def test_extensions
  @test_extensions ||= %w{spec example test}
end

Instance Method Details

#build_cmd(runner, paths) ⇒ Object



129
130
131
132
133
134
# File 'lib/beholder.rb', line 129

def build_cmd(runner, paths)
  puts "\nRunning #{paths.join(', ').inspect} with #{runner}" 
  cmd = "#{runner} #{paths.join(' ')}"
  say cmd
  cmd
end

#default_optionsObject



82
83
84
# File 'lib/beholder.rb', line 82

def default_options
  { :command => ::Beholder.runner }
end

#keep_a_watchful_eye_for(*paths) ⇒ Object Also known as: watch



91
92
93
94
95
# File 'lib/beholder.rb', line 91

def keep_a_watchful_eye_for(*paths)
  @paths_to_watch.concat(paths)
  @paths_to_watch.uniq!
  @paths_to_watch.sort!
end

#map_for(map_name) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/beholder.rb', line 74

def map_for(map_name)
  @treasure_maps[map_name] ||= []
  @current_map = @treasure_maps[map_name]
  yield self if block_given?
ensure
  @current_map = nil
end

#on_change(paths) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/beholder.rb', line 105

def on_change(paths)
  say "#{paths} changed" unless paths.nil? || paths.empty?

  treasure_maps_changed = paths.select { |p| ::Beholder.possible_treasure_map_locations.include?(p) }
  treasure_maps_changed.each {|map_path| read_map_at(map_path) }

  runners_with_paths = {}
  paths.each do |path| 
    find_and_populate_matches(path, runners_with_paths) 
  end  

  runners_with_paths.each do |runner, paths| 
    paths.uniq!
    paths.compact!
  end

  run_tests runners_with_paths
end

#prepare_spell_for(pattern, options = {}, &blk) ⇒ Object Also known as: add_mapping



86
87
88
89
# File 'lib/beholder.rb', line 86

def prepare_spell_for(pattern, options = {}, &blk)
  options = default_options.merge(options)
  @current_map << [pattern, options, blk]
end

#read_all_mapsObject



136
137
138
139
# File 'lib/beholder.rb', line 136

def read_all_maps
  read_default_map
  ::Beholder::possible_treasure_map_locations.each { |path| read_map_at(path) }
end

#read_map_at(path) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/beholder.rb', line 141

def read_map_at(path)
  return unless File.exist?(path)
  say "Found a map at #{path}"
  begin
    instance_eval(File.readlines(path).join("\n"))
  rescue Object => e
    puts "Exception caught trying to load map at #{path}"
    puts e
  end
end

#runObject



62
63
64
65
66
# File 'lib/beholder.rb', line 62

def run
  read_all_maps
  prepare    
  start
end

#shutdownObject



100
101
102
103
# File 'lib/beholder.rb', line 100

def shutdown
  watcher.shutdown
  exit
end

#tests_matching(name) ⇒ Object



124
125
126
127
# File 'lib/beholder.rb', line 124

def tests_matching(name)
  regex = %r%.*#{name}.*\.rb$%
  ::Beholder.all_tests.find_all { |ex| ex =~ regex }
end