Created
August 5, 2014 18:35
-
-
Save mboeh/4e563e78678eb6f4d12f to your computer and use it in GitHub Desktop.
nicer inspects for Bunny objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bunny uses the default Ruby inspect, which means it includes nested objects. | |
# that means that inspecting a Bunny::Queue ends up dumping a screenful of irrelevant | |
# gunk nested somewhere deep in the protocol implementation. | |
# These monkeypatches make #inspect terse and relevant for Exchange, Queue, Channel, | |
# and Session. | |
# Caveat emptor: there are some configuration details the inspect format doesn't cover, | |
# and there are very likely bugs -- this was thrown together through some experimentation | |
# on the REPL. | |
# Example: | |
# <Bunny::Exchange "activity" topic durable !auto_delete channel=2> | |
class Bunny::Exchange | |
def inspect | |
"<#{self.class}".tap do |out| | |
out << " #{@name.inspect} #{@type} " | |
out << (@durable ? "durable" : "!durable") | |
out << " " | |
out << (@auto_delete ? "auto_delete" : "!auto_delete") | |
out << " channel=#{@channel.id}" | |
out << ">" | |
end | |
end | |
end | |
# Example: | |
# <Bunny::Queue "amq.gen-uVZ-RRuoL0dzFj9nwr3kGw" !durable !auto_delete | |
# !exclusive channel=2 bindings=[{activity "foo.*"}{activity "*.bar"}]> | |
class Bunny::Queue | |
def inspect | |
"<#{self.class}".tap do |out| | |
out << " #{@name.inspect} " | |
out << (@durable ? "durable" : "!durable") | |
out << " " | |
out << (@auto_delete ? "auto_delete" : "!auto_delete") | |
out << " " | |
out << (@exclusive ? "exclusive" : "!exclusive") | |
out << " channel=#{@channel.id}" | |
out << " bindings=[" | |
@bindings.each do |bind| | |
out << "{#{bind[:exchange]}" | |
out << " #{bind[:routing_key].inspect}" if bind[:routing_key] | |
out << "}" | |
end | |
out << "]>" | |
end | |
end | |
end | |
# Example: | |
# <Bunny::Channel id=2 open> | |
class Bunny::Channel | |
def inspect | |
"<#{self.class}".tap do |out| | |
out << " id=#{id} #{status}" | |
out << ">" | |
end | |
end | |
end | |
# Example: | |
# <Bunny::Session amq://guest:[email protected]:5672// open> | |
class Bunny::Session | |
def inspect | |
"<#{self.class}".tap do |out| | |
out << " amq://#{@user}:#{@pass}@#{@host}:#{@port}/#{@vhost} #{status}" | |
out << ">" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment