class SyncWrap::Host
Represents various host (server, machine instance) metadata and serves as a container for roles and components.
Attributes
Array of role Symbols or (direct) Component instances in the order added.
The space in which this host was constructed.
Public Class Methods
# File lib/syncwrap/host.rb, line 32 def initialize( space, props = {} ) @space = space @props = {} @contents = [ :all ] merge_props( props ) end
Public Instance Methods
Return the property by (Symbol) key
# File lib/syncwrap/host.rb, line 45 def []( key ) @props[ key ] end
Set property by (Symbol) key to value. Note that the :roles property key is supported here, but is effectively the same as add( val ). Roles are only added, never removed.
# File lib/syncwrap/host.rb, line 52 def []=( key, val ) key = key.to_sym if key == :roles add( *val ) else @props[ key.to_sym ] = val end val end
Add any number of roles (by Symbol) or (direct) Component instances.
# File lib/syncwrap/host.rb, line 76 def add( *args ) args.each do |arg| case( arg ) when Symbol @contents << arg unless @contents.include?( arg ) when Component @contents << arg else raise "Invalid host #{name} addition: #{arg.inspect}" end end end
Return the last component which is a kind of the specified Class or Module clz, or nil if not found.
# File lib/syncwrap/host.rb, line 134 def component( clz ) components.reverse.find { |c| c.kind_of?( clz ) } end
Return a flat Array of Component instances by traversing previously added roles and any direct components in order.
# File lib/syncwrap/host.rb, line 96 def components @contents.inject([]) do |m,c| if c.is_a?( Symbol ) m += space.role( c ) else m << c end m end end
Returns components instances under the specified roles for this host
# File lib/syncwrap/host.rb, line 109 def components_in_roles( qroles ) ( qroles & roles ).inject([]) { |m,r| m += space.role( r ) } end
Merge properties. Note that the :roles property key is supported here, but is affectively the same as add( val ).
# File lib/syncwrap/host.rb, line 64 def merge_props( opts ) opts.each do |key,val| self[ key ] = val end end
Return the :name property.
# File lib/syncwrap/host.rb, line 40 def name self[ :name ] end
Return the last component added to this Host prior to the given component (either directly or via a role), or nil if there is no such component.
# File lib/syncwrap/host.rb, line 116 def prior_component( component ) last = nil @contents.each do |c| if c.is_a?( Symbol ) space.role( c ).each do |rc| return last if rc.equal?( component ) #identity last = rc end else return last if c.equal?( component ) #identity last = c end end nil end
Return an Array of previously added role symbols.
# File lib/syncwrap/host.rb, line 90 def roles @contents.select { |c| c.is_a?( Symbol ) } end
# File lib/syncwrap/host.rb, line 70 def to_h @props.merge( roles: roles ) end