Skip to content

Instantly share code, notes, and snippets.

@mboeh
Created August 5, 2014 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mboeh/4e563e78678eb6f4d12f to your computer and use it in GitHub Desktop.
Save mboeh/4e563e78678eb6f4d12f to your computer and use it in GitHub Desktop.
nicer inspects for Bunny objects
# 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:guest@127.0.0.1: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