Class: Beholder
- Inherits:
-
Object
- Object
- Beholder
- Defined in:
- lib/beholder.rb
Instance Attribute Summary collapse
-
#all_examples ⇒ Object
readonly
Returns the value of attribute all_examples.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#paths_to_watch ⇒ Object
readonly
Returns the value of attribute paths_to_watch.
-
#possible_map_locations ⇒ Object
readonly
Returns the value of attribute possible_map_locations.
-
#sent_an_int ⇒ Object
readonly
Returns the value of attribute sent_an_int.
-
#treasure_maps ⇒ Object
readonly
Returns the value of attribute treasure_maps.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
-
#watcher ⇒ Object
readonly
Returns the value of attribute watcher.
-
#working_directory ⇒ Object
readonly
Returns the value of attribute working_directory.
Class Method Summary collapse
Instance Method Summary collapse
- #add_mapping(pattern, &blk) ⇒ Object (also: #prepare_spell_for)
- #build_cmd(paths) ⇒ Object
- #examples_matching(name, suffix = "example") ⇒ Object
-
#initialize ⇒ Beholder
constructor
A new instance of Beholder.
- #map_for(map_name) ⇒ Object
- #on_change(paths) ⇒ Object
- #read_all_maps ⇒ Object
- #read_map_at(path) ⇒ Object
- #run ⇒ Object
- #shutdown ⇒ Object
- #watch(*paths) ⇒ Object (also: #keep_a_watchful_eye_for)
Constructor Details
#initialize ⇒ Beholder
Returns a new instance of Beholder.
10 11 12 13 14 15 16 17 |
# File 'lib/beholder.rb', line 10 def initialize @working_directory = Dir.pwd @paths_to_watch, @all_examples = [], [] @mappings, @treasure_maps = {}, {} @sent_an_int = false @verbose = ARGV.include?("-v") || ARGV.include?("--verbose") @possible_map_locations = ["#{@working_directory}/.treasure_map.rb", "#{@working_directory}/treasure_map.rb", "#{@working_directory}/config/treasure_map.rb"] end |
Instance Attribute Details
#all_examples ⇒ Object (readonly)
Returns the value of attribute all_examples.
8 9 10 |
# File 'lib/beholder.rb', line 8 def all_examples @all_examples end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
7 8 9 |
# File 'lib/beholder.rb', line 7 def mappings @mappings end |
#paths_to_watch ⇒ Object (readonly)
Returns the value of attribute paths_to_watch.
7 8 9 |
# File 'lib/beholder.rb', line 7 def paths_to_watch @paths_to_watch end |
#possible_map_locations ⇒ Object (readonly)
Returns the value of attribute possible_map_locations.
8 9 10 |
# File 'lib/beholder.rb', line 8 def possible_map_locations @possible_map_locations end |
#sent_an_int ⇒ Object (readonly)
Returns the value of attribute sent_an_int.
7 8 9 |
# File 'lib/beholder.rb', line 7 def sent_an_int @sent_an_int end |
#treasure_maps ⇒ Object (readonly)
Returns the value of attribute treasure_maps.
8 9 10 |
# File 'lib/beholder.rb', line 8 def treasure_maps @treasure_maps end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
7 8 9 |
# File 'lib/beholder.rb', line 7 def verbose @verbose end |
#watcher ⇒ Object (readonly)
Returns the value of attribute watcher.
8 9 10 |
# File 'lib/beholder.rb', line 8 def watcher @watcher end |
#working_directory ⇒ Object (readonly)
Returns the value of attribute working_directory.
7 8 9 |
# File 'lib/beholder.rb', line 7 def working_directory @working_directory end |
Class Method Details
.run ⇒ Object
26 27 28 29 30 |
# File 'lib/beholder.rb', line 26 def self.run beholder = new beholder.run self end |
Instance Method Details
#add_mapping(pattern, &blk) ⇒ Object Also known as: prepare_spell_for
40 41 42 |
# File 'lib/beholder.rb', line 40 def add_mapping(pattern, &blk) @current_map << [pattern, blk] end |
#build_cmd(paths) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/beholder.rb', line 71 def build_cmd(paths) classes = paths.map { |p| p.gsub(".rb", "") }.join(" ") puts "\nRunning #{paths.join(', ').inspect}" execute = %[-e "%w[#{classes}].each { |f| require f }"] # Pickup command from treasure map here, probably cmd = "ruby #{execute}" say cmd cmd end |
#examples_matching(name, suffix = "example") ⇒ Object
66 67 68 69 |
# File 'lib/beholder.rb', line 66 def examples_matching(name, suffix = "example") regex = %r%.*#{name}_#{suffix}\.rb$% all_examples.find_all { |ex| ex =~ regex } end |
#map_for(map_name) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/beholder.rb', line 32 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
58 59 60 61 62 63 64 |
# File 'lib/beholder.rb', line 58 def on_change(paths) say "#{paths} changed" unless paths.nil? || paths.empty? treasure_maps_changed = paths.select { |p| possible_map_locations.include?(p) } treasure_maps_changed.each {|map_path| read_map_at(map_path) } matches = paths.map { |path| find_matches(path) }.uniq.compact run_tests matches end |
#read_all_maps ⇒ Object
82 83 84 85 |
# File 'lib/beholder.rb', line 82 def read_all_maps read_default_map possible_map_locations.each { |path| read_map_at(path) } end |
#read_map_at(path) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/beholder.rb', line 87 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 |
#run ⇒ Object
19 20 21 22 23 24 |
# File 'lib/beholder.rb', line 19 def run read_all_maps set_all_examples if all_examples.empty? prepare start end |
#shutdown ⇒ Object
53 54 55 56 |
# File 'lib/beholder.rb', line 53 def shutdown watcher.shutdown exit end |
#watch(*paths) ⇒ Object Also known as: keep_a_watchful_eye_for
44 45 46 47 48 |
# File 'lib/beholder.rb', line 44 def watch(*paths) self.paths_to_watch.concat(paths) self.paths_to_watch.uniq! self.paths_to_watch.sort! end |