Class: Bind::Listener
- Inherits:
-
Object
- Object
- Bind::Listener
- Defined in:
- lib/bind/listener.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#event ⇒ Object
Returns the value of attribute event.
-
#finish_time ⇒ Object
readonly
Returns the value of attribute finish_time.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#paths ⇒ Object
Returns the value of attribute paths.
-
#run_time ⇒ Object
readonly
Returns the value of attribute run_time.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#expand_dirs(paths) ⇒ Object
Expand directories into file paths, returns array.
-
#initialize(options = {}) ⇒ Listener
constructor
Event listener.
-
#run! ⇒ Object
Start the listener.
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 = {} @run_time, @mtimes = 0, {} @paths = .fetch :paths do raise ArgumentError, 'specify one or more :paths (or directories) to bind the listener to' end @actions = .fetch :actions do raise ArgumentError, ':actions must be an array of objects responding to #call' end @log = .fetch :debug, false @timeout = .fetch :timeout, 0 @interval = .fetch :interval, 2 @event = .fetch :event, :change end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
5 6 7 |
# File 'lib/bind/listener.rb', line 5 def actions @actions end |
#event ⇒ Object
Returns the value of attribute event.
5 6 7 |
# File 'lib/bind/listener.rb', line 5 def event @event end |
#finish_time ⇒ Object (readonly)
Returns the value of attribute finish_time.
6 7 8 |
# File 'lib/bind/listener.rb', line 6 def finish_time @finish_time end |
#interval ⇒ Object
Returns the value of attribute interval.
5 6 7 |
# File 'lib/bind/listener.rb', line 5 def interval @interval end |
#paths ⇒ Object
Returns the value of attribute paths.
5 6 7 |
# File 'lib/bind/listener.rb', line 5 def paths @paths end |
#run_time ⇒ Object (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_time ⇒ Object (readonly)
Returns the value of attribute start_time.
6 7 8 |
# File 'lib/bind/listener.rb', line 6 def start_time @start_time end |
#timeout ⇒ Object
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 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 = @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 |