Sha256: 4aab30809a220be1b147803aa1e9820b61fd2e9608468aef884a2118c3eee5e1

Contents?: true

Size: 1.36 KB

Versions: 71

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EhbrsRubyUtils
  module Videos
    class Quality
      enable_simple_cache
      include ::Comparable

      POSITIVE_MARGIN = 0.75
      LIST = [240, 480, 720, 1080, 2160].freeze

      class << self
        enable_simple_cache

        def by_height(height)
          list.find { |v| v.height == height }
        end

        private

        def list_uncached
          preceding = nil
          LIST.map { |height| preceding = new(height, preceding) }
        end
      end

      attr_reader :following

      common_constructor :height, :preceding do
        preceding&.send('following=', self)
      end

      def <=>(other)
        height <=> other.height
      end

      def to_s
        height.to_s
      end

      def to_xs
        "#{height} (Min: #{min_height}, Max: #{max_height})"
      end

      def resolution_match?(resolution)
        (min_height.blank? || resolution.lower >= min_height) &&
          (max_height.blank? || resolution.lower <= max_height)
      end

      private

      attr_writer :following

      def max_height_uncached
        following.if_present do |v|
          delta = v.height - height
          height + (delta * POSITIVE_MARGIN).to_i
        end
      end

      def min_height_uncached
        preceding.if_present { |v| v.max_height + 1 }
      end
    end
  end
end

Version data entries

71 entries across 71 versions & 2 rubygems

Version Path
ehbrs_ruby_utils-0.33.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.32.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.31.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.30.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.29.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.28.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.27.1 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.27.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.26.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.25.1 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.25.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.24.1 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.24.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.23.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.22.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.21.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.20.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.19.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.18.0 lib/ehbrs_ruby_utils/videos/quality.rb
ehbrs_ruby_utils-0.17.3 lib/ehbrs_ruby_utils/videos/quality.rb