Class: Zerg::Support::EventMachine::ReceiveMock

Inherits:
Object
  • Object
show all
Defined in:
lib/zerg_support/event_machine/connection_mocks.rb

Overview

Mocks the receiving end of an EventMachine connection. The data to be received is passed as an array of strings to the constructor. Calling #replay mocks receiving the data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strings = ['']) ⇒ ReceiveMock

Returns a new instance of ReceiveMock.



25
26
27
28
# File 'lib/zerg_support/event_machine/connection_mocks.rb', line 25

def initialize(strings = [''])
  @strings = strings.kind_of?(String) ? [strings] : strings
  @objects = []
end

Instance Attribute Details

#objectsObject

Returns the value of attribute objects.



23
24
25
# File 'lib/zerg_support/event_machine/connection_mocks.rb', line 23

def objects
  @objects
end

#stringsObject

Returns the value of attribute strings.



22
23
24
# File 'lib/zerg_support/event_machine/connection_mocks.rb', line 22

def strings
  @strings
end

Class Method Details

.object_name(name) ⇒ Object

Declares the name of the object to be received. For instance, a frame protocol would use :frame for name. This generates a receive_frame method, and a frames accessor.



39
40
41
42
43
# File 'lib/zerg_support/event_machine/connection_mocks.rb', line 39

def self.object_name(name)
  define_method(:"receive_#{name}") { |object| @objects << object }
  return if name == :object
  alias_method :"#{name}s", :objects
end

Instance Method Details

#replayObject

Simulates receiving all the given strings as data from Event Machine.



31
32
33
34
# File 'lib/zerg_support/event_machine/connection_mocks.rb', line 31

def replay
  @strings.each { |str| receive_data str }
  self
end