Sha256: a2b1f69ed7e9b7a669762bd5ca192063ad5c6be51175a2cd7453b3a6bad5b92e

Contents?: true

Size: 1.59 KB

Versions: 12

Compression:

Stored size: 1.59 KB

Contents

module Listen
  class Directory
    attr_accessor :path, :options

    def initialize(path, options = {})
      @path    = path
      @options = options
    end

    def scan
      _update_record
      _all_entries.each do |entry_path, data|
        case data[:type]
        when 'File' then _async_change(entry_path, options.merge(type: 'File'))
        when 'Dir'
          _async_change(entry_path, options.merge(type: 'Dir')) if _recursive_scan?(entry_path)
        end
      end
    end

    private

    def _update_record
      if ::Dir.exists?(path)
        _record.async.set_path(path, { type: 'Dir'})
      else
        _record.async.unset_path(path)
      end
    end

    def _all_entries
      _record_entries.merge(_entries)
    end

    def _entries
      return {} unless ::Dir.exists?(path)
      entries = ::Dir.entries(path) - %w[. ..]
      entries = entries.map { |entry| [entry, type: _entry_type(entry)] }
      Hash[*entries.flatten]
    end

    def _entry_type(entry_path)
      entry_path = path.join(entry_path)
      if entry_path.file?
        'File'
      elsif entry_path.directory?
        'Dir'
      end
    end

    def _record_entries
      future = _record.future.dir_entries(path)
      future.value
    end

    def _record
      Celluloid::Actor[:listen_record]
    end

    def _change_pool
      Celluloid::Actor[:listen_change_pool]
    end

    def _recursive_scan?(path)
      !::Dir.exists?(path) || options[:recursive]
    end

    def _async_change(entry_path, options)
      entry_path = path.join(entry_path)
      _change_pool.async.change(entry_path, options)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
listen-2.2.0 lib/listen/directory.rb
listen-2.1.2 lib/listen/directory.rb
listen-2.1.1 lib/listen/directory.rb
listen-2.1.0 lib/listen/directory.rb
listen-2.0.4 lib/listen/directory.rb
listen-2.0.3 lib/listen/directory.rb
listen-2.0.2 lib/listen/directory.rb
listen-2.0.1 lib/listen/directory.rb
listen-2.0.0 lib/listen/directory.rb
listen-2.0.0.pre.1 lib/listen/directory.rb
listen-2.0.0.beta.2 lib/listen/directory.rb
listen-2.0.0.beta.1 lib/listen/directory.rb