Class: MarchHareMock::Queue

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs = {}) ⇒ Queue

Returns a new instance of Queue.



46
47
48
49
50
51
# File 'lib/march_hare_mock.rb', line 46

def initialize(name, attrs = {})
  self.name           = name
  self.attrs          = attrs.dup
  self.messages       = []
  self.delivery_count = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/march_hare_mock.rb', line 79

def method_missing(method, *args)
  method_name  = method.to_s
  is_predicate = false
  if method_name =~ /^(.*)\?$/
    key           = $1.to_sym
    is_predicate = true
  else
    key = method.to_sym
  end

  if attrs.has_key? key
    value = attrs[key]
    is_predicate ? !!value : value
  else
    super
  end
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



44
45
46
# File 'lib/march_hare_mock.rb', line 44

def attrs
  @attrs
end

#delivery_countObject

Returns the value of attribute delivery_count.



44
45
46
# File 'lib/march_hare_mock.rb', line 44

def delivery_count
  @delivery_count
end

#messagesObject

Returns the value of attribute messages.



44
45
46
# File 'lib/march_hare_mock.rb', line 44

def messages
  @messages
end

#nameObject

Returns the value of attribute name.



44
45
46
# File 'lib/march_hare_mock.rb', line 44

def name
  @name
end

Instance Method Details

#bind(exchange, *args) ⇒ Object



53
54
55
# File 'lib/march_hare_mock.rb', line 53

def bind(exchange, *args)
  exchange.queues << self
end

#default_consumerObject



65
66
67
# File 'lib/march_hare_mock.rb', line 65

def default_consumer
  BunnyMock::Consumer.new(self.delivery_count)
end

#snapshot_messagesObject

NOTE: This is NOT a method that is supported on real Bunny queues.

This is a custom method to get us a deep copy of
all the messages currently in the queue. This is provided
to aid in testing a system where it is not practical for the
test to subscribe to the queue and read the messages, but we
need to verify that certain messages have been published.


75
76
77
# File 'lib/march_hare_mock.rb', line 75

def snapshot_messages
  Marshal.load(Marshal.dump(messages))
end

#subscribe(*args, &block) ⇒ Object

Note that this doesn’t block waiting for messages like the real world.



58
59
60
61
62
63
# File 'lib/march_hare_mock.rb', line 58

def subscribe(*args, &block)
  while message = messages.shift
    self.delivery_count += 1
    yield({:payload => message})
  end
end