Class: Expedite::Agents

Inherits:
Object
  • Object
show all
Defined in:
lib/expedite/agents.rb

Defined Under Namespace

Classes: Registration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgents

Returns a new instance of Agents.



80
81
82
# File 'lib/expedite/agents.rb', line 80

def initialize
  @registrations = []
end

Class Method Details

.currentObject



46
47
48
# File 'lib/expedite/agents.rb', line 46

def self.current
  @current ||= Agents.new
end

.lookup(agent) ⇒ Object

Retrieves the specified agent



52
53
54
# File 'lib/expedite/agents.rb', line 52

def self.lookup(agent)
  self.current.lookup(agent.to_s)
end

.register(matcher, **named_options) ⇒ Object

Registers a agent. Agents are matched in the order they are registered.

matcher

Wildcard to match a name against.

named_options

Agent options.

Example

Expedite::Agents.register('base' do |name|
  puts "Base #{name} started"
end
Expedite::Agents.register('development/abc', parent: 'base') do |name|
  puts "Agent #{name} started"
end


70
71
72
# File 'lib/expedite/agents.rb', line 70

def self.register(matcher, **named_options)
  self.current.register(matcher.to_s, **named_options)
end

.resetObject

Resets registrations to default



76
77
78
# File 'lib/expedite/agents.rb', line 76

def self.reset
  self.current.reset
end

Instance Method Details

#lookup(agent) ⇒ Object

Raises:



84
85
86
87
88
89
90
# File 'lib/expedite/agents.rb', line 84

def lookup(agent)
  ret = @registrations.find do |r|
    r.match?(agent)
  end
  raise AgentNotFoundError, "Agent #{agent.inspect} not found" if ret.nil?
  ret.agent
end

#register(matcher, **named_options) ⇒ Object



92
93
94
95
96
# File 'lib/expedite/agents.rb', line 92

def register(matcher, **named_options)
  agent = Agent.new(**named_options)
  @registrations << Registration.new(matcher, agent)
  agent
end

#resetObject



98
99
100
101
# File 'lib/expedite/agents.rb', line 98

def reset
  @registrations = []
  nil
end