class ClockworkMocks::Scheduler

Attributes

tasks[R]

Public Class Methods

init_rspec(allow, receive, clock_file = nil, &block) click to toggle source
# File lib/clockwork_mocks/scheduler.rb, line 11
def self.init_rspec(allow, receive, clock_file = nil, &block)
  Scheduler.new.tap { |s| s.init_rspec(allow, receive, clock_file, &block) }
end
new() click to toggle source
# File lib/clockwork_mocks/scheduler.rb, line 7
def initialize
  @tasks = []
end

Public Instance Methods

every(interval, name, hash = {}, &block) click to toggle source
# File lib/clockwork_mocks/scheduler.rb, line 55
def every(interval, name, hash = {}, &block)
  @tasks.push ClockworkTask.new(interval, name, hash, block)
end
handler(&block) click to toggle source
# File lib/clockwork_mocks/scheduler.rb, line 50
def handler(&block)
  return @handler unless block_given?
  @handler = block
end
init_rspec(allow, receive, clock_file = nil) { || ... } click to toggle source
# File lib/clockwork_mocks/scheduler.rb, line 15
def init_rspec(allow, receive, clock_file = nil)
  allow.call(Clockwork).to receive.call(:handler) do |&block|
    handler(&block)
  end

  allow.call(Clockwork).to receive.call(:every) do |interval, name, hash, &block|
    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
end
reset!() click to toggle source
# File lib/clockwork_mocks/scheduler.rb, line 46
def reset!
  @tasks.each(&:reset!)
end
work() click to toggle source
# File lib/clockwork_mocks/scheduler.rb, line 36
def work
  loop do
    t = @tasks.min_by(&:due)

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

    t.perform(@handler)
  end
end