Class: Net::SSH::Multi::ServerList
- Inherits:
-
Object
- Object
- Net::SSH::Multi::ServerList
- Includes:
- Enumerable
- Defined in:
- lib/net/ssh/multi/server_list.rb
Overview
Encapsulates a list of server objects, both dynamic (Net::SSH::Multi::DynamicServer) and static (Net::SSH::Multi::Server). It attempts to make it transparent whether a dynamic server set has been evaluated or not. Note that a ServerList is NOT an Array, though it is Enumerable.
Instance Method Summary collapse
-
#add(server) ⇒ Object
Adds the given server to the list, and returns the argument.
-
#concat(servers) ⇒ Object
Adds an array (or otherwise Enumerable list) of servers to this list, by calling #add for each argument.
-
#each ⇒ Object
Iterates over each distinct server record in the collection.
-
#flatten ⇒ Object
(also: #to_ary)
Returns an array of all servers in the list, with dynamic server records expanded.
-
#initialize(list = []) ⇒ ServerList
constructor
Create a new ServerList that wraps the given server list.
-
#select ⇒ Object
Works exactly as Enumerable#select, but returns the result as a new ServerList instance.
Constructor Details
#initialize(list = []) ⇒ ServerList
Create a new ServerList that wraps the given server list. Duplicate entries will be discarded.
15 16 17 |
# File 'lib/net/ssh/multi/server_list.rb', line 15 def initialize(list=[]) @list = list.uniq end |
Instance Method Details
#add(server) ⇒ Object
Adds the given server to the list, and returns the argument. If an identical server definition already exists in the collection, the argument is not added, and the existing server record is returned instead.
23 24 25 26 27 28 29 30 31 |
# File 'lib/net/ssh/multi/server_list.rb', line 23 def add(server) index = @list.index(server) if index server = @list[index] else @list.push(server) end server end |
#concat(servers) ⇒ Object
Adds an array (or otherwise Enumerable list) of servers to this list, by calling #add for each argument. Returns self
.
35 36 37 38 |
# File 'lib/net/ssh/multi/server_list.rb', line 35 def concat(servers) servers.each { |server| add(server) } self end |
#each ⇒ Object
Iterates over each distinct server record in the collection. This will correctly iterate over server records instantiated by a DynamicServer as well, but only if the dynamic server has been “evaluated” (see Net::SSH::Multi::DynamicServer#evaluate!).
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/net/ssh/multi/server_list.rb', line 44 def each @list.each do |server| case server when Server then yield server when DynamicServer then server.each { |item| yield item } else raise ArgumentError, "server list contains non-server: #{server.class}" end end self end |
#flatten ⇒ Object Also known as: to_ary
Returns an array of all servers in the list, with dynamic server records expanded. The result is an array of distinct server records (duplicates are removed from the result).
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/net/ssh/multi/server_list.rb', line 65 def flatten result = @list.inject([]) do |aggregator, server| case server when Server then aggregator.push(server) when DynamicServer then aggregator.concat(server) else raise ArgumentError, "server list contains non-server: #{server.class}" end end result.uniq end |
#select ⇒ Object
Works exactly as Enumerable#select, but returns the result as a new ServerList instance.
57 58 59 60 |
# File 'lib/net/ssh/multi/server_list.rb', line 57 def select subset = @list.select { |i| yield i } ServerList.new(subset) end |