Sha256: 02bfff68066702bfba9dc6d6768ddc8155bc99e27113461a0df6f578ff669b5b

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

module Watchr
  module EventHandler
    class Portable
      include Base

      attr_accessor :monitored_paths
      attr_accessor :reference_mtime

      def initialize
        @reference_mtime = Time.now
      end

      # Enters listening loop.
      #
      # Will block control flow until application is explicitly stopped/killed.
      #
      def listen(monitored_paths)
        @monitored_paths = monitored_paths
        loop { trigger; sleep(1) }
      end

      # See if an event occured, and if so notify observers.
      def trigger #:nodoc:
        path, type = detect_event
        notify(path, type) unless path.nil?
      end

      # Update list of monitored paths.
      def refresh(monitored_paths)
        @monitored_paths = monitored_paths
      end

      private

      # Verify mtimes of monitored files.
      #
      # If the latest mtime is more recent than the reference mtime, return
      # that file's path.
      #
      # ===== Returns
      # path and type of event if event occured, nil otherwise
      #
      def detect_event
        path = @monitored_paths.max {|a,b| a.mtime <=> b.mtime }

        if path.mtime > @reference_mtime
          @reference_mtime = path.mtime
          [path, :modified]
        else
          nil
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
nestor-0.2.4 vendor/watchr-0.5.7/lib/watchr/event_handlers/portable.rb
nestor-0.2.3 vendor/watchr-0.5.7/lib/watchr/event_handlers/portable.rb
nestor-0.2.2 vendor/watchr-0.5.7/lib/watchr/event_handlers/portable.rb
nestor-0.2.1 vendor/watchr-0.5.7/lib/watchr/event_handlers/portable.rb
nestor-0.2.0 vendor/watchr-0.5.7/lib/watchr/event_handlers/portable.rb
nestor-0.1.1 vendor/watchr-0.5.7/lib/watchr/event_handlers/portable.rb
nestor-0.1.0 vendor/watchr-0.5.7/lib/watchr/event_handlers/portable.rb
watchr-0.5.7 lib/watchr/event_handlers/portable.rb