Sha256: 99831410b069de1477c57c7a5a3efde7bc586d5328805038e2bda8f6f61a1adf

Contents?: true

Size: 817 Bytes

Versions: 1

Compression:

Stored size: 817 Bytes

Contents

# frozen_string_literal: true

module ClockworkMocks
  class ClockworkTask
    def initialize(interval, name, hash, block)
      @interval = interval
      @name = name
      @hash = hash || {}
      @block = block

      @due = calc_due
    end

    def reset!
      @due = Time.now
      @due = calc_due
    end

    def perform(handler = nil)
      if @block
        @block.call
      elsif handler
        handler.call(@name, Time.now)
      end
      @due = calc_due
    end

    attr_reader :due

    private

    def calc_due
      ndue = Time.at((due || Time.now).to_f + @interval)

      return ndue unless @hash[:at]

      at_split = @hash[:at].split(':').map(&:to_i)
      new_due = ndue.change(hour: at_split[0], minute: at_split[1])

      new_due < ndue ? new_due : (new_due - 1.day)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clockwork-mocks-1.1.0 lib/clockwork_mocks/clockwork_task.rb