Sha256: 1d64d1feb4919db57ac05d81c6e3875d29978234923bc8f6bf70114857981d20

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'ostruct'
require 'active_support'

module Marvin
  # Marvin::TestClient is a simple client used for testing
  # Marvin::Base derivatives in a non-network-reliant setting.
  class TestClient < AbstractClient
    attr_accessor :incoming_commands, :outgoing_commands, :last_sent, :dispatched_events, :connection_open
    
    cattr_accessor :instances
    self.instances = []
    
    DispatchedEvents = Struct.new(:name, :options)
    
    def initialize
      super
      self.incoming_commands = []
      self.outgoing_commands = []
      self.dispatched_events = []
      self.connection_open   = false
      self.instances << self
    end
    
    def connection_open?
      self.connection_open
    end
    
    def send_line(*args)
      self.outgoing_commands += args
      self.last_sent = args.last
    end
    
    def test_command(name, *args)
      options = args.extract_options!
      host_mask = options.delete(:host_mask) || ":WiZ!jto@tolsun.oulu.fi"
      name = name.to_s.upcase
      args = args.flatten.compact
      irc_command = "#{host_mask} #{name} #{args.join(" ").strip}"
      self.receive_line irc_command
    end
    
    def dispatch_event(name, opts = {})
      self.dispatched_events << [name, opts]
      super(name, opts = {})
    end
    
    def self.run
      self.instances.each do |i|
        i.connection_open = true
      end
    end
    
    def self.stop
      self.instances.each do |i|
        i.connection_open = false
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
Sutto-marvin-0.1.0.20081014 lib/marvin/test_client.rb
Sutto-marvin-0.1.0.20081016 lib/marvin/test_client.rb