Sha256: 68fcb64ac4f15294e5c96af6a3c4cbb7bdd22e04efe0bd7a3f107d04c1b2de90

Contents?: true

Size: 972 Bytes

Versions: 1

Compression:

Stored size: 972 Bytes

Contents

# frozen_string_literal: true

module ClockworkMocks
  class Scheduler
    attr_reader :tasks

    def initialize()
      @tasks = []
    end

    def self.init_rspec(allow, receive, clock_file = nil)
      scheduler = Scheduler.new

      allow.call(Clockwork).to receive.call(:every) do |interval, name, hash, &block|
        scheduler.every interval, name, hash, &block
      end

      if block_given?
        yield
      else
        unless clock_file
          rails = Object.const_get('Rails')
          clock_file = "#{rails.root}/clock.rb" if rails
        end

        load clock_file if clock_file
      end

      scheduler
    end

    def work
      loop do
        t = @tasks.min_by(&:due)

        break if t.nil? || t.due > Time.now

        t.perform
      end
    end

    def reset!
      @tasks.each(&:reset!)
    end

    def every(interval, name, hash = {}, &block)
      @tasks.push ClockworkTask.new(interval, name, hash, block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clockwork-mocks-1.0.0 lib/clockwork_mocks/scheduler.rb