Sha256: b45b3c4b072b72f3f9b31c7f8fbf661c13056c698985ddf562751f33f6e41fb3

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 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.sort.detect do |priority, agents|
          agents.present?
        end
        agents.shift
      end

      def <<(agent)
        raise ArgumentError, "Agents must have a specified priority" unless agent.respond_to?(:priority)

        priority = agent.priority || 999999
        @priorities[priority] ||= []

        unless @priorities[priority].include?(agent)
          delete(agent)
          @priorities[priority] << agent
        end
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
electric_slide-0.5.1 lib/electric_slide/agent_strategy/fixed_priority.rb
electric_slide-0.5.0 lib/electric_slide/agent_strategy/fixed_priority.rb