Class: ElectricSlide::AgentStrategy::FixedPriority

Inherits:
Object
  • Object
show all
Defined in:
lib/electric_slide/agent_strategy/fixed_priority.rb

Instance Method Summary collapse

Constructor Details

#initializeFixedPriority

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

Raises:

  • (ArgumentError)


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

Returns:

  • (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_summaryHash

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

Returns:

  • (Hash)

    Summary information about agents available, depending on strategy



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_agentObject



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