Module: AceMQ::AMQP::Codecs

Defined in:
lib/acemq/amqp/codec.rb

Overview

Codecs by name, so configuration can ask for a format without the calling code naming the class.

The names are shared with the other libraries — json, bytes, string, yaml, toml and xml mean the same six things in Go and Java — so a deployment that sets ACEMQ_CODEC does not have to be rewritten per language. text is the same codec as string: it is the name Java and Python give it, and one ACEMQ_CODEC value has to work in all of them.

ProtobufCodec and AvroCodec are deliberately not here. Both are built around a message type or a schema, a name in configuration cannot carry one, and the Java library leaves them out of its own registry for exactly the same reason.

Class Method Summary collapse

Class Method Details

.build(name) ⇒ Object

Builds the codec registered under a name.

Parameters:

  • name (String)

Returns:

  • (Object) —

    a codec

Raises:

  • (ArgumentError) —

    when nothing is registered under it



344
345
346
347
348
349
350
# File 'lib/acemq/amqp/codec.rb', line 344

def build(name)
  build = @lock.synchronize { @registry[name.to_s] }
  return build.call if build

  raise ArgumentError,
        "no codec named #{name.to_s.inspect} is registered; known: #{names.join(", ")}"
end

.names ⇒ Array<String>

The registered names, sorted.

Returns:

  • (Array<String>)


355
356
357
# File 'lib/acemq/amqp/codec.rb', line 355

def names
  @lock.synchronize { @registry.keys.sort }
end

.register(name, &build) ⇒ Object

Makes a codec available under a name.

Registering a name twice replaces the first, which is what lets a test override a default rather than having to work around it.

Parameters:

  • name (String) —

    what configuration will ask for

Yield Returns:

  • (Object) —

    a new codec



329
330
331
332
333
334
335
336
337
# File 'lib/acemq/amqp/codec.rb', line 329

def register(name, &build)
  unless build
    raise ArgumentError,
          "Codecs.register(#{name.inspect}) needs a block that builds one"
  end

  @lock.synchronize { @registry[name.to_s] = build }
  name.to_s
end