Sha256: 2b01a9d423c21962b8582a33bfc643aac58fc0196f01db8732876ac3990fa8f7

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Stalking
  class Consumer
    class Handlers
      attr_accessor :job_handlers, :error_handlers, :before_handlers, :after_handlers

      def initialize
        self.job_handlers = {}

        self.error_handlers = []
        self.before_handlers = []
        self.after_handlers = []
      end

      def job(name, &block)
        self.job_handlers[name] = block
      end

      def error(&block)
        self.error_handlers.push block
      end

      def before(&block)
        self.before_handlers.push block
      end

      def after(&block)
        self.after_handlers.push block
      end
    end

    def self.instances=(instances)
      @instances = instances
    end

    def self.instances
      @instances
    end

    self.instances = []

    def initialize(options = {}, &block)
      @handlers = Handlers.new
      @handlers.instance_exec(&block)

      self.class.instances.push self
    end

    def run_job(name, args)
      @handlers.job_handlers[name].call(args)
    end

    def run_error(e, name, args)
      @handlers.error_handlers.each { |error| error.call e, name, args }
    end

    def run_before(name, args)
      @handlers.before_handlers.each { |before| before.call name, args }
    end

    def run_after(name, args)
      @handlers.after_handlers.each { |after| after.call name, args }
    end
  end

  class Producer
    attr_accessor :queue

    def initialize(options = {})
      self.queue = []
    end

    def enqueue(name, args = {}, options = {})
      queue.push [name, args, options]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bbqueue-0.0.2 test/bbqueue/stalking.rb
bbqueue-0.0.1 test/bbqueue/stalking.rb