Class: Sasquatch::Listener

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/quatch/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger, #print_line

Constructor Details

#initialize(file, running = true, &block) ⇒ Listener

Returns a new instance of Listener.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/quatch/listener.rb', line 8

def initialize file, running = true, &block
  @file = file

  @base_path = File.dirname(@file)

  @running = running

  validate_file @file

  find_imports @file

  @file_changed = block

  # init the logger
  logger "Watching '#{@file}' for updates"

  # listen to the main file's directory
  @listener = Listen.to(File.dirname(@file), only: /\.js$/, &hear)
  # start listening
  start
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



6
7
8
# File 'lib/quatch/listener.rb', line 6

def base_path
  @base_path
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/quatch/listener.rb', line 6

def file
  @file
end

#file_changedObject (readonly)

Returns the value of attribute file_changed.



6
7
8
# File 'lib/quatch/listener.rb', line 6

def file_changed
  @file_changed
end

#filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/quatch/listener.rb', line 6

def files
  @files
end

#listenerObject (readonly)

Returns the value of attribute listener.



6
7
8
# File 'lib/quatch/listener.rb', line 6

def listener
  @listener
end

#runningObject (readonly)

Returns the value of attribute running.



6
7
8
# File 'lib/quatch/listener.rb', line 6

def running
  @running
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/quatch/listener.rb', line 6

def status
  @status
end

Instance Method Details

#_get_file_name(filename) ⇒ Object



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

def _get_file_name filename
  filename.sub(/^.*?#{@base_path}\//, '')
end

#find_imports(file) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/quatch/listener.rb', line 30

def find_imports file
  @files = {}
  File.read(file).scan(/\/\* @import .+?(.*.js)/) do |filename|
    @import = "#{File.dirname(@file)}/#{filename.first}"
    validate_file @import
    @files[filename.first] = File.new(File.absolute_path(@import))
  end
end

#hearObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/quatch/listener.rb', line 39

def hear
  Proc.new do |modified, added, removed|
    if(modified)
      modified_file = modified.first
      if(imported? modified_file)
        if(@file_changed)
          @file_changed.call(_get_file_name(modified_file), @file, @files)
        end
      end
    end
  end
end

#imported?(file) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/quatch/listener.rb', line 70

def imported? file

  filename = _get_file_name file

  if(filename == _get_file_name(@file) or @files.keys.include? filename)
    return true
  end

  false

end

#startObject



52
53
54
55
56
57
# File 'lib/quatch/listener.rb', line 52

def start
  @listener.start # not blocking
  while( @running ) do
    sleep(1)
  end
end

#stopObject



59
60
61
62
# File 'lib/quatch/listener.rb', line 59

def stop
  @listener.pause
  @running = false
end

#validate_file(file) ⇒ Object



64
65
66
67
68
# File 'lib/quatch/listener.rb', line 64

def validate_file file
  unless(File.file?(file))
    raise "Specified file doesn't exist"
  end
end