Class: ElectricSlide::AgentStrategy::FixedPriority
- Inherits:
-
Object
- Object
- ElectricSlide::AgentStrategy::FixedPriority
- Defined in:
- lib/electric_slide/agent_strategy/fixed_priority.rb
Instance Method Summary collapse
- #<<(agent) ⇒ Object
- #agent_available? ⇒ Boolean
-
#available_agent_summary ⇒ Hash
Returns information about the number of available agents The data returned depends on the AgentStrategy in use.
- #checkout_agent ⇒ Object
- #delete(agent) ⇒ Object
-
#initialize ⇒ FixedPriority
constructor
A new instance of FixedPriority.
Constructor Details
#initialize ⇒ FixedPriority
Returns a new instance of FixedPriority.
6 7 8 |
# File 'lib/electric_slide/agent_strategy/fixed_priority.rb', line 6 def initialize @priorities = {} end |
Instance Method Details
#<<(agent) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/electric_slide/agent_strategy/fixed_priority.rb', line 39 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 |
#agent_available? ⇒ Boolean
10 11 12 13 14 |
# File 'lib/electric_slide/agent_strategy/fixed_priority.rb', line 10 def agent_available? !!@priorities.detect do |priority, agents| agents.present? end end |
#available_agent_summary ⇒ Hash
Returns information about the number of available agents The data returned depends on the AgentStrategy in use. :total: The total number of available agents :priorities: A Hash containing the number of available agents at each priority
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/electric_slide/agent_strategy/fixed_priority.rb', line 21 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 |
#checkout_agent ⇒ Object
32 33 34 35 36 37 |
# File 'lib/electric_slide/agent_strategy/fixed_priority.rb', line 32 def checkout_agent _, agents = @priorities.sort.detect do |priority, agents| agents.present? end agents.shift end |
#delete(agent) ⇒ Object
51 52 53 54 55 |
# File 'lib/electric_slide/agent_strategy/fixed_priority.rb', line 51 def delete(agent) @priorities.detect do |priority, agents| agents.delete(agent) end end |