Sha256: c3671dbd16ce7a934ca254c2b9d9db6d21d7adab89c39d5076c51d2d83075f95

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

# encoding: utf-8

class ElectricSlide
  class AgentStrategy
    class FixedPriority
      def initialize
        @priorities = {}
      end

      def agent_available?
        !!@priorities.detect do |priority, agents|
          agents.present?
        end
      end

      # Returns information about the number of available agents
      # The data returned depends on the AgentStrategy in use.
      # @return [Hash] Summary information about agents available, depending on strategy
      # :total: The total number of available agents
      # :priorities: A Hash containing the number of available agents at each priority
      def available_agent_summary
        @priorities.inject({}) do |summary, data|
          priority, agents = *data
          summary[:total] ||= 0
          summary[:total] += agents.count
          summary[:priorities] ||= {}
          summary[:priorities][priority] = agents.count
          summary
        end
      end

      def checkout_agent
        _, agents = @priorities.detect do |priority, agents|
          agents.present?
        end
        agents.shift
      end

      def <<(agent)
        # TODO: How aggressively do we check for agents duplicated in multiple priorities?
        raise ArgumentError, "Agents must have a specified priority" unless agent.respond_to?(:priority)
        priority = agent.priority || 999999
        @priorities[priority] ||= []
        @priorities[priority] << agent unless @priorities[priority].include? agent
      end

      def delete(agent)
        @priorities.detect do |priority, agents|
          agents.delete(agent)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
electric_slide-0.4.1 lib/electric_slide/agent_strategy/fixed_priority.rb
electric_slide-0.4.0 lib/electric_slide/agent_strategy/fixed_priority.rb
electric_slide-0.3.0 lib/electric_slide/agent_strategy/fixed_priority.rb
electric_slide-0.2.0 lib/electric_slide/agent_strategy/fixed_priority.rb