Sha256: 7f4b3ad77501db0a39406d7e8b247d7190d402fa956eef07aa4eef5592209fb4

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Actors
  class Hub
    class ActorsProxy
    	require "typed_map"

      def initialize(actors, channels)
        raise ArgumentError, "'actors' should be an instance of TypedMap"   unless actors.instance_of? TypedMap
        raise ArgumentError, "'channels' should be an instance of TypedMap" unless channels.instance_of? TypedMap

        @actors   = actors
        @channels = channels
      end

      def add(actor_name, executable, subscribed_on: [], publishes_to: [])
        actor = Actor.new(actor_name, executable)
        @actors.add actor.name, actor

        subscribed_on.each do |chname|
          channel = find_or_else_create_channel(chname)

          channel.subscribers.add actor.name, actor
        end

        publishes_to.each do |chname|
        	find_or_else_create_channel(chname)
        end
      end

      def keys
        @actors.keys
      end

      def [](key)
        @actors[key]
      end

      def has?(key)
        @actors.has? key
      end

      def count
        @actors.count
      end

      def length
        @actors.length
      end

      def to_a
        @actors.to_a
      end

      def to_h
        @actors.to_h
      end

      private

      def find_or_else_create_channel(chname)
        return @channels[chname] if @channels.has? chname
        
        channel = Channel.new(chname)
        @channels.add channel.name, channel

        channel
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
actors-0.1.0 lib/actors/hub/actors_proxy.rb