Class: Bind::Listener

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Listener

Event listener. Must specify the :paths, and :action options.

Options:

:paths           array of file or directory paths
:actions         objects responding to #call, which is used as the callbacks for the event handler
:timeout         time in seconds, after which the listener should stop. Defaults to 0, meaning infinity
:event           event to bind to, may be one of (:change). Defaults to :change
:debug           log verbose debugging information to this stream
:interval        sleep interval in seconds. Defaults to 2


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bind/listener.rb', line 27

def initialize options = {}
  @run_time, @mtimes = 0, {}
  @paths = options.fetch :paths do
   raise ArgumentError, 'specify one or more :paths (or directories) to bind the listener to'
  end
  @actions = options.fetch :actions do
    raise ArgumentError, ':actions must be an array of objects responding to #call'
  end
  @log = options.fetch :debug, false
  @timeout = options.fetch :timeout, 0
  @interval = options.fetch :interval, 2
  @event = options.fetch :event, :change
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



5
6
7
# File 'lib/bind/listener.rb', line 5

def actions
  @actions
end

#eventObject

Returns the value of attribute event.



5
6
7
# File 'lib/bind/listener.rb', line 5

def event
  @event
end

#finish_timeObject (readonly)

Returns the value of attribute finish_time.



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

def finish_time
  @finish_time
end

#intervalObject

Returns the value of attribute interval.



5
6
7
# File 'lib/bind/listener.rb', line 5

def interval
  @interval
end

#pathsObject

Returns the value of attribute paths.



5
6
7
# File 'lib/bind/listener.rb', line 5

def paths
  @paths
end

#run_timeObject (readonly)

Returns the value of attribute run_time.



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

def run_time
  @run_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#timeoutObject

Returns the value of attribute timeout.



5
6
7
# File 'lib/bind/listener.rb', line 5

def timeout
  @timeout
end

Instance Method Details

#expand_dirs(paths) ⇒ Object

Expand directories into file paths, returns array.



44
45
46
47
48
49
50
51
52
# File 'lib/bind/listener.rb', line 44

def expand_dirs paths
  paths.inject [] do |files, path|
    case
    when File.directory?(path) ; files += Dir["#{path}/**/*.*"]
    when File.file?(path)      ; files << path
    else                         files += Dir[path]
    end
  end    
end

#run!Object

Start the listener.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bind/listener.rb', line 57

def run!
  files = expand_dirs @paths
  start_time = Time.now
  log "binding to #{files.join(', ')}, watching #{event} every #{interval} second(s)." + 
      (timeout > 0 ? " Terminating in #{timeout} seconds" : '')
  catch :halt do
    loop do
      @run_time = Time.now - start_time
      throw :halt if timeout > 0 and @run_time >= timeout
      log '.', true
      files.each { |file| send event, File.new(file) } 
      sleep interval
    end
  end
  finish_time = Time.now
  log "binding terminated"
end