Sha256: 6b70d9ae7dfb0073ad068491cd27894c9ca62e1f97f8cec292df7b21f64976dc

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require "active_support/core_ext/date"
require "active_support/core_ext/time"
require "active_support/core_ext/numeric/time"

module Chrono
  class NextTime
    attr_reader :now, :source

    attr_writer :time

    def initialize(options)
      @now = options[:now]
      @source = options[:source]
    end

    def to_time
      loop do
        case
        when !scheduled_in_this_month?
          carry_month
        when !scheduled_in_this_day?
          carry_day
        when !scheduled_in_this_wday?
          carry_day
        when !scheduled_in_this_hour?
          carry_hour
        when !scheduled_in_this_minute?
          carry_minute
        else
          break time
        end
      end
    end

    private

    def time
      @time ||= (now + 1.minute).at_beginning_of_minute
    end

    def schedule
      @schedule ||= Schedule.new(source)
    end

    def scheduled_in_this_month?
      schedule.months.include?(time.month)
    end

    def scheduled_in_this_day?
      schedule.days.include?(time.day)
    end

    def scheduled_in_this_wday?
      schedule.wdays.include?(time.wday)
    end

    def scheduled_in_this_hour?
      schedule.hours.include?(time.hour)
    end

    def scheduled_in_this_minute?
      schedule.minutes.include?(time.min)
    end

    def carry_month
      self.time = time.next_month.at_beginning_of_month
    end

    def carry_day
      self.time = time.tomorrow.at_beginning_of_day
    end

    def carry_hour
      self.time = (time + 1.hour).at_beginning_of_hour
    end

    def carry_minute
      self.time = (time + 1.minute).at_beginning_of_minute
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chrono-0.0.5 lib/chrono/next_time.rb
chrono-0.0.4 lib/chrono/next_time.rb
chrono-0.0.3 lib/chrono/next_time.rb