Class: Beholder
- Inherits:
-
Object
- Object
- Beholder
- Defined in:
- lib/beholder.rb
Instance Attribute Summary collapse
-
#be_verbose ⇒ Object
readonly
Returns the value of attribute be_verbose.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#paths_to_watch ⇒ Object
readonly
Returns the value of attribute paths_to_watch.
-
#sent_an_int ⇒ Object
readonly
Returns the value of attribute sent_an_int.
-
#the_eye ⇒ Object
readonly
Returns the value of attribute the_eye.
-
#working_directory ⇒ Object
readonly
Returns the value of attribute working_directory.
Class Method Summary collapse
Instance Method Summary collapse
- #blink ⇒ Object
- #close_your_eye ⇒ Object
- #identify_stolen_treasure(treasure) ⇒ Object
-
#initialize ⇒ Beholder
constructor
A new instance of Beholder.
- #notice_thief_taking(treasure) ⇒ Object
- #open_your_eye ⇒ Object
- #prepare_for_interlopers ⇒ Object
- #reclaim_stolen_treasure_at(coordinates) ⇒ Object
Constructor Details
#initialize ⇒ Beholder
Returns a new instance of Beholder.
9 10 11 12 13 14 15 |
# File 'lib/beholder.rb', line 9 def initialize @paths_to_watch = ['app', 'config', 'lib', 'examples'] @sent_an_int = false @mappings = {} @working_directory = Dir.pwd @be_verbose = ARGV.include?("-v") || ARGV.include?("--verbose") end |
Instance Attribute Details
#be_verbose ⇒ Object (readonly)
Returns the value of attribute be_verbose.
7 8 9 |
# File 'lib/beholder.rb', line 7 def be_verbose @be_verbose 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 |
#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 |
#the_eye ⇒ Object (readonly)
Returns the value of attribute the_eye.
7 8 9 |
# File 'lib/beholder.rb', line 7 def the_eye @the_eye 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
.cast_thy_gaze ⇒ Object
17 18 19 20 21 |
# File 'lib/beholder.rb', line 17 def self.cast_thy_gaze @beholder = new @beholder.prepare_for_interlopers @beholder.open_your_eye end |
Instance Method Details
#blink ⇒ Object
33 34 35 |
# File 'lib/beholder.rb', line 33 def blink @sent_an_int = false end |
#close_your_eye ⇒ Object
37 38 39 40 |
# File 'lib/beholder.rb', line 37 def close_your_eye the_eye.shutdown exit end |
#identify_stolen_treasure(treasure) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/beholder.rb', line 42 def identify_stolen_treasure(treasure) case treasure when /#{working_directory}\/app\/(.*)\.rb/ "examples/#{$1}_example.rb" when /#{working_directory}\/lib\/(.*)\.rb/ "examples/lib/#{$1}_example.rb" when /#{working_directory}\/examples\/(.*)_example\.rb/ "examples/#{$1}_example.rb" when /#{working_directory}\/examples\/example_helper\.rb/, /#{working_directory}\/config/ "examples/**/*_example.rb" else say "Unknown file: #{treasure}" '' end end |
#notice_thief_taking(treasure) ⇒ Object
65 66 67 68 69 |
# File 'lib/beholder.rb', line 65 def notice_thief_taking(treasure) say "#{treasure} changed" coordinates = treasure.map { |t| identify_stolen_treasure(t) }.uniq.compact reclaim_stolen_treasure_at coordinates end |
#open_your_eye ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/beholder.rb', line 23 def open_your_eye say("Watching the following locations:\n #{paths_to_watch.join(", ")}") @the_eye = FSEvents::Stream.watch(paths_to_watch) do |treasure_chest| notice_thief_taking(treasure_chest.modified_files) blink puts "\n\nWaiting to hear from the disk since #{Time.now}" end @the_eye.run end |
#prepare_for_interlopers ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/beholder.rb', line 71 def prepare_for_interlopers trap 'INT' do if @sent_an_int then puts " A second INT? Ok, I get the message. Shutting down now." close_your_eye else puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again." @sent_an_int = true Kernel.sleep 1.5 end end end |
#reclaim_stolen_treasure_at(coordinates) ⇒ Object
59 60 61 62 63 |
# File 'lib/beholder.rb', line 59 def reclaim_stolen_treasure_at(coordinates) return if coordinates.nil? || coordinates.empty? puts "\nRunning #{coordinates.join(', ')}" system "ruby #{coordinates.map { |f| Dir.glob(f) }.join(' ')}" end |