Class: Moqueue::MockExchange

Inherits:
Object
  • Object
show all
Defined in:
lib/moqueue/mock_exchange.rb

Defined Under Namespace

Modules: BaseKey Classes: DirectBindingKey, TopicBindingKey

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MockExchange

Returns a new instance of MockExchange.



26
27
28
29
30
31
32
33
34
# File 'lib/moqueue/mock_exchange.rb', line 26

def initialize(opts={})
  if @topic = opts[:topic]
    MockBroker.instance.register_topic_exchange(self)
  elsif @fanout = opts[:fanout]
    MockBroker.instance.register_fanout_exchange(self)
  elsif @direct = opts[:direct]
    MockBroker.instance.register_direct_exchange(self)
  end
end

Instance Attribute Details

#directObject (readonly)

Returns the value of attribute direct.



4
5
6
# File 'lib/moqueue/mock_exchange.rb', line 4

def direct
  @direct
end

#fanoutObject (readonly)

Returns the value of attribute fanout.



4
5
6
# File 'lib/moqueue/mock_exchange.rb', line 4

def fanout
  @fanout
end

#topicObject (readonly)

Returns the value of attribute topic.



4
5
6
# File 'lib/moqueue/mock_exchange.rb', line 4

def topic
  @topic
end

Class Method Details

.new(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/moqueue/mock_exchange.rb', line 8

def new(opts={})
  if opts[:topic] && topic_exchange = MockBroker.instance.find_topic_exchange(opts[:topic])
    return topic_exchange
  end
  
  if opts[:fanout] && fanout = MockBroker.instance.find_fanout_exchange(opts[:fanout])
    return fanout
  end
  
  if opts[:direct] && direct = MockBroker.instance.find_direct_exchange(opts[:direct])
    return direct
  end
  
  super
end

Instance Method Details

#acked_messagesObject



40
41
42
43
44
45
# File 'lib/moqueue/mock_exchange.rb', line 40

def acked_messages
  attached_queues.map do |q|
    q = q.first if q.kind_of?(Array)
    q.acked_messages
  end.flatten
end

#attach_queue(queue, opts = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/moqueue/mock_exchange.rb', line 47

def attach_queue(queue, opts={})
  if topic
    attached_queues << [queue, TopicBindingKey.new(opts[:key])]
  elsif direct
    attached_queues << [queue, DirectBindingKey.new(opts[:key])]
  else
    attached_queues << queue
  end
end

#attached_queuesObject



36
37
38
# File 'lib/moqueue/mock_exchange.rb', line 36

def attached_queues
  @attached_queues ||= []
end

#publish(message, opts = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/moqueue/mock_exchange.rb', line 57

def publish(message, opts={})
  require_routing_key(opts) if topic
  matching_queues(opts).each do |q| 
    q.receive(message, prepare_header_opts(opts))
  end
end

#received_ack_for_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/moqueue/mock_exchange.rb', line 64

def received_ack_for_message?(message)
  acked_messages.include?(message)
end