Sha256: f72f8f9bc742f8157822b743573e8b3a7631342b7bd59560964750d6bd41bf24

Contents?: true

Size: 1.91 KB

Versions: 6

Compression:

Stored size: 1.91 KB

Contents

require 'server'
Thread.abort_on_exception = true

class Role
  INDEX = {}

  class << self
    def get_role(msg)
      role = INDEX[msg['role']].new(msg)
      role.start
      role
    end

    def role(name)
      INDEX[name] = self
      self.my_role = name
    end

    attr_accessor :my_role
  end

  def state
    { 'role' => self.class.my_role,
      'active' => @active_links,
      'disabled' => @disabled_links,
      'broken' => @broken_links }
  end

  def initialize(manifest)
    @manifest = manifest
  end

  def start
    raise NotImplementedError
  end

  def stop
    raise NotImplementedError
  end

  def disable
    raise NotImplementedError
  end

  def enable
    raise NotImplementedError
  end

  def disable_link(type, addr)
    raise NotImplementedError
  end

  def enable_link(type, addr)
    raise NotImplementedError
  end

  def init_links(type, links, &block)
    @active_links ||= {}
    @active_links[type] ||= (links || [])

    @broken_links ||= {}
    @broken_links[type] ||= []

    @disabled_links ||= {}
    @disabled_links[type] ||= []

    @event_callbacks ||= {}
    @event_callbacks[type] = block
  end

  def disable_link(type, addr)
    types = @active_links.keys

    types.each do |type|
      if @active_links[type].include?(addr) || @broken_links[type].include?(addr)
        @active_links[type].delete addr
        @broken_links[type].delete addr
    
        @disabled_links[type] << addr unless @disabled_links[type].include?(addr)

        @event_callbacks[type].call('disable', addr) if @event_callbacks[type]
      end
    end
  end

  def enable_link(type, addr)
    types = @active_links.keys

    types.each do |type|
      if @disabled_links[type].include?(addr)
        @disabled_links[type].delete addr

        @active_links[type] << addr unless @active_links[type].include?(addr)

        @event_callbacks[type].call('enable', addr) if @event_callbacks[type]
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bum-0.0.17 lib/role.rb
bum-0.0.16 lib/role.rb
bum-0.0.15 lib/role.rb
bum-0.0.14 lib/role.rb
bum-0.0.13 lib/role.rb
bum-0.0.12 lib/role.rb