Sha256: c2cc45fdffc15ce5d1dc6e2e1f25f586d06fa9f2f19c781447c0295f9fb024aa

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module DownloadTV
  ##
  # Builds and applies filters to the results
  class Filterer
    attr_reader :filters

    def initialize(filters_config)
      @filters = []
      build_filters(filters_config)
    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

    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

    ##
    # 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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
download_tv-2.6.6 lib/download_tv/filterer.rb
download_tv-2.6.5 lib/download_tv/filterer.rb
download_tv-2.6.4 lib/download_tv/filterer.rb
download_tv-2.6.3 lib/download_tv/filterer.rb
download_tv-2.6.2 lib/download_tv/filterer.rb
download_tv-2.6.1 lib/download_tv/filterer.rb
download_tv-2.6.0 lib/download_tv/filterer.rb
download_tv-2.5.5 lib/download_tv/filterer.rb