Sha256: 9a0ef627b1c369454dd7ef8a953ac15cbaf20c709917052f49d06589afde7557

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module Bbq
  module Session
    extend self

    def next(options = {})
      driver = options.delete(:driver)
      pool   = options.delete(:pool)

      if pool
        pool.next(driver)
      else
        create(driver)
      end
    end

    def create(driver)
      Capybara::Session.new(driver, Bbq.app)
    end

    def pool
      @pool ||= Pool.new
    end

    class Pool
      attr_accessor :idle, :taken

      def initialize
        @idle   = []
        @taken  = []
      end

      def next(driver)
        take_idle(driver) || create(driver)
      end

      def release
        taken.each(&:reset!)
        idle.concat(taken)
        taken.clear
      end

      private

      def take_idle(driver)
        idle.find { |s| s.mode == driver }.tap do |session|
          if session
            idle.delete(session)
            taken.push(session)
          end
        end
      end

      def create(driver)
        Bbq::Session.create(driver).tap do |session|
          taken.push(session)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bbq-0.2.1 lib/bbq/session.rb
bbq-0.2.0 lib/bbq/session.rb
bbq-0.1.0 lib/bbq/session.rb