Class Net::SSH::Multi::DynamicServer

  1. lib/net/ssh/multi/dynamic_server.rb
Parent: Object

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.

Methods

public class

  1. new

public instance

  1. []
  2. []=
  3. each
  4. evaluate!
  5. to_ary

Attributes

callback [R] The Proc object to call to evaluate the server(s)
master [R] The Net::SSH::Multi::Session instance that owns this dynamic server record.
options [R] The hash of options that will be used to initialize the server records.

Public class methods

new (master, options, callback)

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.

[show source]
    # File lib/net/ssh/multi/dynamic_server.rb, line 32
32:     def initialize(master, options, callback)
33:       @master, @options, @callback = master, options, callback
34:       @servers = nil
35:     end

Public instance methods

[] (key)

Returns the value for the given key in the :properties hash of the options. If no :properties hash exists in options, this returns nil.

[show source]
    # File lib/net/ssh/multi/dynamic_server.rb, line 39
39:     def [](key)
40:       (options[:properties] ||= {})[key]
41:     end
[]= (key, value)

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.

[show source]
    # File lib/net/ssh/multi/dynamic_server.rb, line 45
45:     def []=(key, value)
46:       (options[:properties] ||= {})[key] = value
47:     end
each () {|server| ...}

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!).

[show source]
    # File lib/net/ssh/multi/dynamic_server.rb, line 52
52:     def each
53:       (@servers || []).each { |server| yield server }
54:     end
evaluate! ()

Evaluates the callback and instantiates the servers, memoizing the result. Subsequent calls to evaluate! will simply return the cached list of servers.

[show source]
    # File lib/net/ssh/multi/dynamic_server.rb, line 59
59:     def evaluate!
60:       @servers ||= Array(callback[options]).map do |server|
61:           case server
62:           when String then Net::SSH::Multi::Server.new(master, server, options)
63:           else server
64:           end
65:         end
66:     end
to_ary ()

Alias for evaluate!