Class: Net::SSH::Multi::DynamicServer
- Inherits:
-
Object
- Object
- Net::SSH::Multi::DynamicServer
- Defined in:
- lib/net/ssh/multi/dynamic_server.rb
Overview
Represents a lazily evaluated collection of servers. This will usually be created via Net::SSH::Multi::Session#use(&block), and is useful for creating server definitions where the name or address of the servers are not known until run-time.
session.use { lookup_ip_address_of_server }
This prevents lookup_ip_address_of_server
from being invoked unless the server is actually needed, at which point it is invoked and the result cached.
The callback should return either nil
(in which case no new servers are instantiated), a String (representing a connection specification), an array of Strings, or an array of Net::SSH::Multi::Server instances.
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
The Proc object to call to evaluate the server(s).
-
#master ⇒ Object
readonly
The Net::SSH::Multi::Session instance that owns this dynamic server record.
-
#options ⇒ Object
readonly
The hash of options that will be used to initialize the server records.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the value for the given
key
in the :properties hash of theoptions
. -
#[]=(key, value) ⇒ Object
Sets the given key/value pair in the
:properties
key in the options hash. -
#each ⇒ Object
Iterates over every instantiated server record in this dynamic server.
-
#evaluate! ⇒ Object
(also: #to_ary)
Evaluates the callback and instantiates the servers, memoizing the result.
-
#initialize(master, options, callback) ⇒ DynamicServer
constructor
Create a new DynamicServer record, owned by the given Net::SSH::Multi::Session
master
, with the given hash ofoptions
, and using the given Proccallback
to lazily evaluate the actual server instances.
Constructor Details
#initialize(master, options, callback) ⇒ DynamicServer
Create a new DynamicServer record, owned by the given Net::SSH::Multi::Session master
, with the given hash of options
, and using the given Proc callback
to lazily evaluate the actual server instances.
32 33 34 35 |
# File 'lib/net/ssh/multi/dynamic_server.rb', line 32 def initialize(master, , callback) @master, @options, @callback = master, , callback @servers = nil end |
Instance Attribute Details
#callback ⇒ Object (readonly)
The Proc object to call to evaluate the server(s)
24 25 26 |
# File 'lib/net/ssh/multi/dynamic_server.rb', line 24 def callback @callback end |
#master ⇒ Object (readonly)
The Net::SSH::Multi::Session instance that owns this dynamic server record.
21 22 23 |
# File 'lib/net/ssh/multi/dynamic_server.rb', line 21 def master @master end |
#options ⇒ Object (readonly)
The hash of options that will be used to initialize the server records.
27 28 29 |
# File 'lib/net/ssh/multi/dynamic_server.rb', line 27 def @options end |
Instance Method Details
#[](key) ⇒ Object
Returns the value for the given key
in the :properties hash of the options
. If no :properties hash exists in options
, this returns nil
.
39 40 41 |
# File 'lib/net/ssh/multi/dynamic_server.rb', line 39 def [](key) ([:properties] ||= {})[key] end |
#[]=(key, value) ⇒ Object
Sets the given key/value pair in the :properties
key in the options hash. If the options hash has no :properties key, it will be created.
45 46 47 |
# File 'lib/net/ssh/multi/dynamic_server.rb', line 45 def []=(key, value) ([:properties] ||= {})[key] = value end |
#each ⇒ Object
Iterates over every instantiated server record in this dynamic server. If the servers have not yet been instantiated, this does nothing (e.g., it does not automatically invoke #evaluate!).
52 53 54 |
# File 'lib/net/ssh/multi/dynamic_server.rb', line 52 def each (@servers || []).each { |server| yield server } end |
#evaluate! ⇒ Object Also known as: to_ary
Evaluates the callback and instantiates the servers, memoizing the result. Subsequent calls to #evaluate! will simply return the cached list of servers.
59 60 61 62 63 64 65 66 |
# File 'lib/net/ssh/multi/dynamic_server.rb', line 59 def evaluate! @servers ||= Array(callback[]).map do |server| case server when String then Net::SSH::Multi::Server.new(master, server, ) else server end end end |