Sha256: ac1ebacaaf907d338bf0417ca2e4a7b6e0d28fb7a054cf5cf35770f9ba5b5b87

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

# frozen_string_literal: true

module ActiveJob
  module Scheduler
    # Generates a time duration from a given parsed interval value.
    class Interval
      attr_reader :type, :value

      # All interval types in options.
      TYPES = %w[cron in at every nat].freeze

      delegate :==, to: :to_duration

      def initialize(params = {})
        params.each do |type, value|
          @type = type
          @value = value
        end
      end

      # Parse this interval with `Fugit`
      def to_duration
        return value if value.is_a? Integer

        duration = Fugit.public_send("do_parse_#{parser}", value)

        return duration.next_time - Time.current if duration.is_a? Fugit::Cron

        duration.to_sec
      end

      # Discover which `Rufus::Scheduler` parser to use by checking the
      # type.
      #
      # @return [String]
      def parser
        case type.to_s
        when 'every'
          'duration'
        else
          type
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activejob-scheduler-1.0.0.pre lib/active_job/scheduler/interval.rb