I'm new to Ruby, using Bunny to consume messages from RabbitMQ.
So my class currently looks roughly like this:
class Consumer
include Validator
def initialize
#Start a RabbitMQ session
@rdSession = Session.new
@queueMain = rdSession.joinQueue('QueueMain')
@queueLittle = rdSession.joinQueue('QueueLittle')
...
@queueTen = rdSession.joinQueue('QueueTen')
goWork
end
def goWork
@queueMain.subscribe(:manual_ack => true) do |delivery_info, properties, payload|
goDoSomethingElse(payload)
end
....
@queueTen.subscribe(:manual_ack => true) do |delivery_info, properties, payload|
goDoAnotherPiece(payload)
end
end
My question is the file is becoming quite long so I want to reduce it somehow. So one thing I thought of is as those moving that long list of joining queues in initialize into another file as they are constant.
However what is the correct way to do this, should I create a module, copy across all those joinQueue lines, then refer to them in goWork as constants like: QUEUEMAIN?
Any ideas/suggestions would be appreciated.
Trying to understand good design for this?
Thanks.
There's more you can refactor here but basically yes you move the lifting to a module and thanks to @Amadan, you can
Also see ruby style guide it is recommended to user
snake_casefor all method and variable names and to useCamelCasefor class and module definitions, but I didn't do that as that was not your question. It is also recommend to use Rubocop to help keep proper style in mind.