Class: DuckTest::Platforms::Generic::Listener

Inherits:
Object
  • Object
show all
Includes:
LoggerHelper, Listener, OSHelpers
Defined in:
lib/duck_test/platforms/generic/listener.rb

Overview

A generic listener to watch for changed, deleted, and new files on a file system.

Instance Attribute Summary (collapse)

Attributes included from Listener

#block

Instance Method Summary (collapse)

Methods included from OSHelpers

#available?, #current_os, #is_linux?, #is_mac?, #is_windows?

Methods included from Listener

#call_listener_event, #changed?, #changed_files, #dir_list, #file_list, #listener_event, #refresh, #speed, #speed=, #stop, #stop=, #update_all, #update_file_spec, #watch, #watch_file_spec, #watched?

Methods included from LoggerHelper

#ducklog

Constructor Details

- (Listener) initialize

A new instance of Listener



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/duck_test/platforms/generic/listener.rb', line 15

def initialize
  super

  ducklog.console "Platform listener: Generic"

  # i plan to implement feature to allow developer to specify which
  # listener to use, so, simply notify developer how to enable native listener
  # if it is not available.
  if self.is_linux? && !self.available?
    ducklog.console "########################################################################"
    ducklog.console "NOTE: Native file listener is NOT enabled."
    ducklog.console "To enable native file listener:"
    ducklog.console "Edit your Gemfile and add the following to your test group"
    ducklog.console "gem 'rb-inotify'"
    ducklog.console "########################################################################"

  elsif self.is_mac? && !self.available?
    ducklog.console "########################################################################"
    ducklog.console "NOTE: Native file listener is NOT enabled."
    ducklog.console "To enable native file listener:"
    ducklog.console "Edit your Gemfile and add the following to your test group"
    ducklog.console "gem 'rb-fsevent'"
    ducklog.console "########################################################################"


  elsif self.is_windows? && !self.available?
    ducklog.console "########################################################################"
    ducklog.console "NOTE: Native file listener is NOT enabled."
    ducklog.console "To enable native file listener:"
    ducklog.console "Edit your Gemfile and add the following to your test group"
    ducklog.console "gem 'rb-fchange'"
    ducklog.console "########################################################################"


  end

end

Instance Attribute Details

- (Object) thread

Returns the value of attribute thread



12
13
14
# File 'lib/duck_test/platforms/generic/listener.rb', line 12

def thread
  @thread
end

Instance Method Details

- (NilClass) start

Starts a thread and listens for changes to all of the directories / files added to the listener via Listener#watch

Returns:

  • (NilClass)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/duck_test/platforms/generic/listener.rb', line 57

def start

  # call super to trap control-C
  super

  # call refresh once prior to starting the loop so that all of the attributes
  # for all directories / files get updated.
  self.refresh

  # now, start the thread
  self.thread = Thread.new do

    until self.stop do

      sleep(self.speed)

      # grab a list of all changed and new files
      changed_files = self.refresh

      # now, update all of the attributes for all directories / files so the
      # next interation of the loop will not return invalid results.
      update_all

      # call the event listener block for all of the changed / new files
      changed_files.each do |item|
        self.call_listener_event(WatchEvent.new(self, item, :update, nil))
      end

    end

  end

  return nil
end