Sha256: fb7da37f76075e1604d1f2dbdec017610b71e48c7b4113a3c26d35c48b7a6bbc

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module DownloadTV
  ##
  # Builds and applies filters to the results
  class Filterer
    def initialize(filters_config)
      @filters = []
      build_filters(filters_config)
    end

    ##
    # Iteratively applies filters until they've all been applied
    # or applying the next filter would result in no results
    def filter(shows)
      # shows is tuple (show name, link)
      @filters.each do |f|
        new_shows = shows.reject { |name, _link| f.call(name) }
        # Go to next filter if the filter removes every release
        next if new_shows.empty?

        shows = new_shows
      end

      shows
    end

    private

    def build_filters(filters_config)
      return unless filters_config

      filters_config[:includes].map { |i| build_include_filter(i) }
      filters_config[:excludes].map { |i| build_exclude_filter(i) }
    end

    def build_include_filter(str)
      @filters << ->(n) { !n.upcase.include?(str) }
    end

    def build_exclude_filter(str)
      @filters << ->(n) { n.upcase.include?(str) }
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
download_tv-2.9.1 lib/download_tv/filterer.rb
download_tv-2.9.0 lib/download_tv/filterer.rb
download_tv-2.8.5 lib/download_tv/filterer.rb
download_tv-2.8.4 lib/download_tv/filterer.rb
download_tv-2.8.3 lib/download_tv/filterer.rb
download_tv-2.8.2 lib/download_tv/filterer.rb
download_tv-2.8.1 lib/download_tv/filterer.rb
download_tv-2.8.0 lib/download_tv/filterer.rb
download_tv-2.7.0 lib/download_tv/filterer.rb
download_tv-2.6.10 lib/download_tv/filterer.rb
download_tv-2.6.9 lib/download_tv/filterer.rb
download_tv-2.6.8 lib/download_tv/filterer.rb
download_tv-2.6.7 lib/download_tv/filterer.rb