Sha256: a8813737a1529e739981dc21fc104914c916347e663482f2a066adef0995de75

Contents?: true

Size: 748 Bytes

Versions: 8

Compression:

Stored size: 748 Bytes

Contents

require 'prj/dir_with_score'
require 'strscan'

module Prj

  class Filter
    def initialize(letters, case_sensitive = true)
      @letters = letters.to_a
      @case_sensitive = case_sensitive
    end

    def filter(directories)
      wrapped_with_score(directories).sort.map(&:to_s)
    end

    def distance(dir)
      scanner = StringScanner.new(dir)
      @letters.each do |letter|
        regexp = Regexp.new(".*?[#{letter}]", !@case_sensitive)
        scanner.scan(regexp) or return :no_score
      end
      scanner.pos - @letters.length
    end

    def wrapped_with_score(directories)
      directories.map { |d| DirWithScore.new(d, distance(d)) }.reject { |d| d.score == :no_score }
    end
    private :wrapped_with_score
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
prj-2.0.1 lib/prj/filter.rb
prj-2.0.1.pre.2 lib/prj/filter.rb
prj-2.0.1.pre.1 lib/prj/filter.rb
prj-2.0.0 lib/prj/filter.rb
prj-1.0.2 lib/prj/filter.rb
prj-1.0.1 lib/prj/filter.rb
prj-1.0.0 lib/prj/filter.rb
prj-0.1.0 lib/prj/filter.rb