Sha256: f6930a02b179a233807f1f772e8424fff0c0918ffe62aba28d2c8f826e412ec8

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require 'miu/sockets'
require 'miu/writable'
require 'miu/utility'

module Miu
  module Publisher
    class << self
      def new(*args, &block)
        options = Miu::Utility.extract_options! args
        host = args.shift || '127.0.0.1'
        port = args.shift || Miu.default_sub_port
        socket = options[:socket] || PubSocket

        klass = Class.new(socket, &block)
        klass.send :include, Writable

        klass.new.tap do |pub|
          address = Miu::Socket.build_address host, port
          pub.connect address
        end
      end

      def included(base)
        base.extend ClassMethods
      end
    end

    module ClassMethods
      def socket_type(socket = nil)
        if socket
          @socket_type = socket
        else
          @socket_type
        end
      end
    end

    attr_reader :publisher

    def initialize(host, port, tag)
      @publisher = Miu::Publisher.new host, port, :socket => self.class.socket_type
      @tag = tag
    end

    def close
      @publisher.close
    end

    def write(message)
      @publisher.write @tag, message
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
miu-0.2.3 lib/miu/publisher.rb
miu-0.2.2 lib/miu/publisher.rb