Class: MindfulWatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ MindfulWatcher

Returns a new instance of MindfulWatcher.



3
4
5
6
7
8
9
10
11
12
# File 'lib/mindful_watcher.rb', line 3

def initialize(args=[])
puts "initializing sass_watcher"
args.delete_if { |a| a == '--watch' }
  @args     = args
  path_args = args.select { |a| a =~ /.+\:.+/ }.first || ""
  sources   = path_args.gsub(/\:.+/, ' ')
  @dir2observe = File.realdirpath(File.dirname(sources)) if sources

  raise NotObservableException, :msg => 'cannot find folder in arguments' if @dir2observe.nil?
end

Instance Method Details

#observe_folderObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mindful_watcher.rb', line 14

def observe_folder
  fm = DirectoryRunner.new(@dir2observe)
  while true do
    fm.process do |filename|
      if @last_change.nil? || File.mtime(filename).to_s > @last_change.to_s
        @last_change = Time.now
        update_files(filename)
      end
    end
    sleep 2
  end
end

#update_files(filename) ⇒ Object



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

def update_files(filename)
  files_converted = system("sass #{@args.join(' ')}")
  if files_converted
    puts "#{@last_change} file changed: #{filename}" 
  else
    raise "SassNotFoundException", :msg => "please run 'gem install sass'"
  end
end