Sha256: 27095a51360c8aa7f25b9c53a85339887e8d4150136c882df5591c51c6271025

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require 'active_support/time'

module TimeDistance
  class Calculator
    MULTIPLES = {
      seconds: 1,
      minutes: 1.minute,
      hours: 1.hour,
      days: 1.day,
      weeks: 1.week,
      months: 1.month,
      years: 1.year
    }.freeze

    DEFAULTS = {
      format: [:months, :days, :hours, :minutes],
      translation_scope: 'datetime.time_distance'
    }.freeze

    attr_reader :options

    def initialize(time_from, time_to, options = {})
      @time_from = time_from
      @time_to = time_to
      @options = DEFAULTS.merge(options)

      @seconds = @time_from - @time_to
    end

    def to_s
      array = to_a.compact
      array << I18n.t(:ago, scope: options[:translation_scope]) if negative?

      array.join(' ')
    end

    def units
      @units ||= calculate
    end

    private

    def to_a
      options[:format].map do |unit|
        value = units[unit].to_i
        next unless value > 0

        I18n.t(unit, scope: [options[:translation_scope], :units], count: value)
      end
    end

    def negative?
      @seconds.to_i < 0
    end

    def desc_sorted_unit_format
      options[:format].sort { |p, n| MULTIPLES[n] <=> MULTIPLES[p] }
    end

    def calculate
      values = {}

      desc_sorted_unit_format.inject(@seconds.abs.to_f.round) do |remainder, unit|
        multiple = MULTIPLES[unit]

        values[unit] = remainder / multiple
        remainder % multiple
      end

      values
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
time_distance-0.1.2 lib/time_distance/calculator.rb