class Observr::EventHandler::Portable

Public Class Methods

new() click to toggle source
# File lib/observr/event_handlers/portable.rb, line 6
def initialize
  @reference_mtime = @reference_atime = @reference_ctime = Time.now
end

Public Instance Methods

listen(monitored_paths) click to toggle source

Enters listening loop.

Will block control flow until application is explicitly stopped/killed.

@param [Array<Pathname>] monitored_paths

list of paths the application is currently monitoring.

@return [undefined]

# File lib/observr/event_handlers/portable.rb, line 19
def listen(monitored_paths)
  @monitored_paths = monitored_paths
  loop { trigger; sleep(1) }
end
refresh(monitored_paths) click to toggle source

Update list of monitored paths.

@param [Array<Pathname>] monitored_paths

list of paths the application is currently monitoring.

@return [undefined]

# File lib/observr/event_handlers/portable.rb, line 41
def refresh(monitored_paths)
  @monitored_paths = monitored_paths
end
trigger() click to toggle source

See if an event occured, and if so notify observers.

@return [undefined]

@private

# File lib/observr/event_handlers/portable.rb, line 29
def trigger
  path, type = detect_event
  notify(path, type) unless path.nil?
end

Private Instance Methods

detect_event() click to toggle source

Verify mtimes of monitored files.

If the latest mtime is more recent than the reference mtime, return that file’s path.

@return [[Pathname, Symbol]]

path and type of event if event occured, nil otherwise

@todo improve ENOENT error handling

# File lib/observr/event_handlers/portable.rb, line 57
def detect_event # OPTIMIZE, REFACTOR
  @monitored_paths.each do |path|
    return [path, :deleted] unless path.exist?
  end

  mtime_path = @monitored_paths.max {|a,b| a.mtime <=> b.mtime }
  atime_path = @monitored_paths.max {|a,b| a.atime <=> b.atime }
  ctime_path = @monitored_paths.max {|a,b| a.ctime <=> b.ctime }

  if    mtime_path.mtime > @reference_mtime then @reference_mtime = mtime_path.mtime; [mtime_path, :modified]
  elsif atime_path.atime > @reference_atime then @reference_atime = atime_path.atime; [atime_path, :accessed]
  elsif ctime_path.ctime > @reference_ctime then @reference_ctime = ctime_path.ctime; [ctime_path, :changed ]
  else; nil; end
rescue Errno::ENOENT
  retry
end