Module: AceMQ::AMQP::Codec
- Defined in:
- lib/acemq/amqp/codec.rb
Overview
What turns a payload into bytes and back.
Not a base class to inherit from — anything answering these four methods is a codec, which is how a codec from another gem works here without depending on this one. The methods are documented here because a duck type still has to be written down somewhere.
Ruby's decode returns a value rather than filling in a destination the
caller passed. That is the one place this deliberately departs from the
Go shape, which has no choice: Ruby has no type to be told, so asking for
one would be ceremony with nothing behind it.
Class Method Summary collapse
- .check!(candidate) ⇒ Object private
-
.codec?(candidate) ⇒ Boolean
Whether an object can stand in as a codec.
Instance Method Summary collapse
-
#can_decode?(content_type) ⇒ Boolean
Whether this codec should handle it.
-
#content_type ⇒ String
What this codec writes onto a message.
-
#decode(body) ⇒ Object
The payload.
-
#encode(payload) ⇒ String
The bytes to publish.
Class Method Details
.check!(candidate) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
85 86 87 88 89 90 91 |
# File 'lib/acemq/amqp/codec.rb', line 85 def self.check!(candidate) return candidate if codec?(candidate) raise ArgumentError, "#{candidate.inspect} is not a codec: it needs content_type, encode, " \ "decode and can_decode?" end |
.codec?(candidate) ⇒ Boolean
Whether an object can stand in as a codec.
Checked when a connection is built rather than when the first message arrives, so a codec missing a method is a mistake at start-up instead of a mystery at three in the morning.
79 80 81 |
# File 'lib/acemq/amqp/codec.rb', line 79 def self.codec?(candidate) %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) } end |
Instance Method Details
#can_decode?(content_type) ⇒ Boolean
Returns whether this codec should handle it.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/acemq/amqp/codec.rb', line 70 module Codec # Whether an object can stand in as a codec. # # Checked when a connection is built rather than when the first message # arrives, so a codec missing a method is a mistake at start-up instead # of a mystery at three in the morning. # # @param candidate [Object] # @return [Boolean] def self.codec?(candidate) %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) } end # @raise [ArgumentError] when +candidate+ is not a codec # @api private def self.check!(candidate) return candidate if codec?(candidate) raise ArgumentError, "#{candidate.inspect} is not a codec: it needs content_type, encode, " \ "decode and can_decode?" end end |
#content_type ⇒ String
Returns what this codec writes onto a message.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/acemq/amqp/codec.rb', line 70 module Codec # Whether an object can stand in as a codec. # # Checked when a connection is built rather than when the first message # arrives, so a codec missing a method is a mistake at start-up instead # of a mystery at three in the morning. # # @param candidate [Object] # @return [Boolean] def self.codec?(candidate) %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) } end # @raise [ArgumentError] when +candidate+ is not a codec # @api private def self.check!(candidate) return candidate if codec?(candidate) raise ArgumentError, "#{candidate.inspect} is not a codec: it needs content_type, encode, " \ "decode and can_decode?" end end |
#decode(body) ⇒ Object
Returns the payload.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/acemq/amqp/codec.rb', line 70 module Codec # Whether an object can stand in as a codec. # # Checked when a connection is built rather than when the first message # arrives, so a codec missing a method is a mistake at start-up instead # of a mystery at three in the morning. # # @param candidate [Object] # @return [Boolean] def self.codec?(candidate) %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) } end # @raise [ArgumentError] when +candidate+ is not a codec # @api private def self.check!(candidate) return candidate if codec?(candidate) raise ArgumentError, "#{candidate.inspect} is not a codec: it needs content_type, encode, " \ "decode and can_decode?" end end |
#encode(payload) ⇒ String
Returns the bytes to publish.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/acemq/amqp/codec.rb', line 70 module Codec # Whether an object can stand in as a codec. # # Checked when a connection is built rather than when the first message # arrives, so a codec missing a method is a mistake at start-up instead # of a mystery at three in the morning. # # @param candidate [Object] # @return [Boolean] def self.codec?(candidate) %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) } end # @raise [ArgumentError] when +candidate+ is not a codec # @api private def self.check!(candidate) return candidate if codec?(candidate) raise ArgumentError, "#{candidate.inspect} is not a codec: it needs content_type, encode, " \ "decode and can_decode?" end end |