Class: ActiveMessaging::Adapters::Test::Connection

Inherits:
BaseConnection show all
Defined in:
lib/activemessaging/adapters/test.rb

Instance Attribute Summary collapse

Attributes inherited from BaseConnection

#reliable

Instance Method Summary collapse

Methods included from ActiveMessaging::Adapter

included, #logger

Constructor Details

#initialize(cfg) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
18
19
# File 'lib/activemessaging/adapters/test.rb', line 12

def initialize cfg
  @config = cfg
  @subscriptions = []
  @destinations = []
  @received_messages = []
  @unreceived_messages = []
  @connected = true
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/activemessaging/adapters/test.rb', line 10

def config
  @config
end

#connectedObject

Returns the value of attribute connected.



10
11
12
# File 'lib/activemessaging/adapters/test.rb', line 10

def connected
  @connected
end

#destinationsObject

Returns the value of attribute destinations.



10
11
12
# File 'lib/activemessaging/adapters/test.rb', line 10

def destinations
  @destinations
end

#received_messagesObject

Returns the value of attribute received_messages.



10
11
12
# File 'lib/activemessaging/adapters/test.rb', line 10

def received_messages
  @received_messages
end

#subscriptionsObject

Returns the value of attribute subscriptions.



10
11
12
# File 'lib/activemessaging/adapters/test.rb', line 10

def subscriptions
  @subscriptions
end

#unreceived_messagesObject

Returns the value of attribute unreceived_messages.



10
11
12
# File 'lib/activemessaging/adapters/test.rb', line 10

def unreceived_messages
  @unreceived_messages
end

Instance Method Details

#all_messagesObject



88
89
90
# File 'lib/activemessaging/adapters/test.rb', line 88

def all_messages
  @destinations.map {|q| q.messages }.flatten
end

#disconnectObject



21
22
23
24
25
26
27
# File 'lib/activemessaging/adapters/test.rb', line 21

def disconnect
  @subscriptions = []
  @destinations = []
  @received_messages = []
  @unreceived_messages = []
  @connected = false
end

#find_destination(destination_name) ⇒ Object



80
81
82
# File 'lib/activemessaging/adapters/test.rb', line 80

def find_destination destination_name
  @destinations.find{|q| q.name == destination_name }
end

#find_message(destination_name, body) ⇒ Object

test helper methods



63
64
65
66
67
68
69
70
71
72
# File 'lib/activemessaging/adapters/test.rb', line 63

def find_message destination_name, body
  
  all_messages.find do |m|
    m.destination == destination_name && if body.is_a?(Regexp)
      m.body =~ body
    else
      m.body == body.to_s
    end
  end
end

#find_subscription(destination_name) ⇒ Object



84
85
86
# File 'lib/activemessaging/adapters/test.rb', line 84

def find_subscription destination_name
  @subscriptions.find{|s| s.name == destination_name}
end

#open_destination(destination_name) ⇒ Object



74
75
76
77
78
# File 'lib/activemessaging/adapters/test.rb', line 74

def open_destination destination_name
  unless find_destination destination_name
    @destinations << Destination.new(destination_name)
  end
end

#receive(options = {}) ⇒ Object



47
48
49
50
51
52
# File 'lib/activemessaging/adapters/test.rb', line 47

def receive(options={})
  destination = @destinations.find do |q|
    find_subscription(q.name) && !q.empty?
  end
  destination.receive unless destination.nil?
end

#received(message, headers = {}) ⇒ Object



54
55
56
# File 'lib/activemessaging/adapters/test.rb', line 54

def received message, headers={}
  @received_messages << message
end

#send(destination_name, message_body, message_headers = {}) ⇒ Object



41
42
43
44
45
# File 'lib/activemessaging/adapters/test.rb', line 41

def send destination_name, message_body, message_headers={}
  open_destination destination_name
  destination = find_destination destination_name
  destination.send Message.new(message_body, nil, message_headers, destination_name)
end

#subscribe(destination_name, subscribe_headers = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/activemessaging/adapters/test.rb', line 29

def subscribe destination_name, subscribe_headers={}
  open_destination destination_name
  unless @subscriptions.find {|s| s.name == destination_name} 
    @subscriptions << Subscription.new(destination_name, subscribe_headers)
  end
  @subscriptions.last
end

#unreceive(message, headers = {}) ⇒ Object



58
59
60
# File 'lib/activemessaging/adapters/test.rb', line 58

def unreceive message, headers={}
  @unreceived_messages << message
end

#unsubscribe(destination_name, unsubscribe_headers = {}) ⇒ Object



37
38
39
# File 'lib/activemessaging/adapters/test.rb', line 37

def unsubscribe destination_name, unsubscribe_headers={}
  @subscriptions.delete_if {|s| s.name == destination_name}
end