Class: Beholder

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBeholder

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', 'test', 'spec']
  @sent_an_int = false
  @mappings = {}
  @working_directory = Dir.pwd
  @be_verbose = ARGV.include?("-v") || ARGV.include?("--verbose")
end

Instance Attribute Details

#be_verboseObject (readonly)

Returns the value of attribute be_verbose.



7
8
9
# File 'lib/beholder.rb', line 7

def be_verbose
  @be_verbose
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



7
8
9
# File 'lib/beholder.rb', line 7

def mappings
  @mappings
end

#paths_to_watchObject (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_intObject (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_eyeObject (readonly)

Returns the value of attribute the_eye.



7
8
9
# File 'lib/beholder.rb', line 7

def the_eye
  @the_eye
end

#working_directoryObject (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_gazeObject



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



33
34
35
# File 'lib/beholder.rb', line 33

def blink
  @sent_an_int = false
end

#close_your_eyeObject



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
    puts "Unknown file: #{treasure}"
    ''
  end
end

#notice_thief_taking(treasure) ⇒ Object



66
67
68
69
70
# File 'lib/beholder.rb', line 66

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_eyeObject



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_interlopersObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/beholder.rb', line 72

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
64
# File 'lib/beholder.rb', line 59

def reclaim_stolen_treasure_at(coordinates)
  real_coordinates = coordinates.map { |f| Dir.glob(f) }
  return if real_coordinates.empty?
  puts "\nRunning #{real_coordinates.join(', ')}" 
  system "ruby #{real_coordinates.join(' ')}"
end