Sha256: ee3455a0635c239d7aef54308076a372eb0c168d52538a1f02e66db2a0ed00f5

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require 'bbq/core'

module Bbq
  module Core
    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::Core.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::Core::Session.create(driver).tap do |session|
            taken.push(session)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bbq-core-0.4.0 lib/bbq/core/session.rb
bbq-core-0.3.0 lib/bbq/core/session.rb