Sha256: 957b7047d64a89bc3ac4aa3fc467841240529a27774539c4bf3cd59ad6aab7aa

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

# encoding: utf-8
module FileWatch
  class WatchedFilesCollection

    def initialize(settings)
      @sort_by = settings.file_sort_by # "last_modified" | "path"
      @sort_direction = settings.file_sort_direction # "asc" | "desc"
      @sort_method = method("#{@sort_by}_#{@sort_direction}".to_sym)
      @files = []
      @pointers = {}
    end

    def add(watched_file)
      @files << watched_file
      @sort_method.call
    end

    def delete(paths)
      Array(paths).each do |f|
        index = @pointers.delete(f)
        @files.delete_at(index)
      end
      @sort_method.call
    end

    def close_all
      @files.each(&:file_close)
    end

    def empty?
      @files.empty?
    end

    def keys
      @pointers.keys
    end

    def values
      @files
    end

    def watched_file_by_path(path)
      index = @pointers[path]
      return nil unless index
      @files[index]
    end

    private

    def last_modified_asc
      @files.sort! do |left, right|
        left.modified_at <=> right.modified_at
      end
      refresh_pointers
    end

    def last_modified_desc
      @files.sort! do |left, right|
        right.modified_at <=> left.modified_at
      end
      refresh_pointers
    end

    def path_asc
      @files.sort! do |left, right|
        left.path <=> right.path
      end
      refresh_pointers
    end

    def path_desc
      @files.sort! do |left, right|
        right.path <=> left.path
      end
      refresh_pointers
    end

    def refresh_pointers
      @files.each_with_index do |watched_file, index|
        @pointers[watched_file.path] = index
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
logstash-input-file-4.1.3 lib/filewatch/watched_files_collection.rb
logstash-input-file-4.1.2 lib/filewatch/watched_files_collection.rb
logstash-input-file-4.1.1 lib/filewatch/watched_files_collection.rb
logstash-input-file-4.1.0 lib/filewatch/watched_files_collection.rb