Sha256: e84c769ace1444260a3fd8d38732554e5257ef095c8d4a228768f9ee5b168c03

Contents?: true

Size: 807 Bytes

Versions: 4

Compression:

Stored size: 807 Bytes

Contents

module Tennis::Worker::Generic::Serialize
  def serialize(object = nil, loader: nil,  dumper: nil)
    if object
      _serializers[:load] = object if object.respond_to?(:load)
      _serializers[:dump] = object if object.respond_to?(:dump)
    else
      _serializers[:load] = loader if loader
      _serializers[:dump] = dumper if dumper
    end
  end

  def _apply_serializer(serializer_kind, message)
    serializer = _serializers[serializer_kind]
    if serializer.kind_of?(Proc)
      serializer.call(message)
    elsif serializer.respond_to?(serializer_kind)
      serializer.__send__(serializer_kind, message)
    else
      fail "Unexpected (de)serializer, it must be a Proc or respond to ##{kind}"
    end
  end

  def _serializers
    @_serializers ||= Hash.new(->(message){ message })
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tennis-jobs-0.3.1 lib/tennis/worker/generic/serialize.rb
tennis-jobs-0.3.0 lib/tennis/worker/generic/serialize.rb
tennis-jobs-0.2.0 lib/tennis/worker/generic/serialize.rb
tennis-jobs-0.1.0 lib/tennis/worker/generic/serialize.rb