Sha256: fd5a8e7afb025531f79a0f116805602325e49161df14df56e10167e54f87e882

Contents?: true

Size: 972 Bytes

Versions: 2

Compression:

Stored size: 972 Bytes

Contents

module EventSourcery
  class EventBodySerializer
    def self.serialize(event_body,
                       serializer: EventSourcery.config.event_body_serializer)
      serializer.serialize(event_body)
    end

    def initialize
      @serializers = Hash.new(IdentitySerializer)
    end

    def add(type, serializer = nil, &block_serializer)
      @serializers[type] = serializer || block_serializer
      self
    end

    def serialize(object)
      serializer = @serializers[object.class]
      serializer.call(object, &method(:serialize))
    end

    module HashSerializer
      def self.call(hash, &serialize)
        hash.each_with_object({}) do |(key, value), memo|
          memo[key.to_s] = serialize.call(value)
        end
      end
    end

    module ArraySerializer
      def self.call(array, &serialize)
        array.map(&serialize)
      end
    end

    module IdentitySerializer
      def self.call(object)
        object
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
event_sourcery-0.14.0 lib/event_sourcery/event_body_serializer.rb
event_sourcery-0.13.0 lib/event_sourcery/event_body_serializer.rb