class SyncWrap::Host

Represents various host (server, machine instance) metadata and serves as a container for roles and components.

Attributes

contents[R]

Array of role Symbols or (direct) Component instances in the order added.

props[R]
space[R]

The space in which this host was constructed.

Public Class Methods

new( space, props = {} ) click to toggle source
# File lib/syncwrap/host.rb, line 32
def initialize( space, props = {} )
  @space = space
  @props = {}
  @contents = [ :all ]
  merge_props( props )
end

Public Instance Methods

[]( key ) click to toggle source

Return the property by (Symbol) key

# File lib/syncwrap/host.rb, line 45
def []( key )
  @props[ key ]
end
[]=( key, val ) click to toggle source

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( *args ) click to toggle source

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
component( clz ) click to toggle source

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
components() click to toggle source

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
components_in_roles( qroles ) click to toggle source

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_props( opts ) click to toggle source

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
name() click to toggle source

Return the :name property.

# File lib/syncwrap/host.rb, line 40
def name
  self[ :name ]
end
prior_component( component ) click to toggle source

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
roles() click to toggle source

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
to_h() click to toggle source
# File lib/syncwrap/host.rb, line 70
def to_h
  @props.merge( roles: roles )
end