Sha256: 1b782ada00ad337b6640b120171358b1105469c0ca9fc9fd8fba1ff6ae7ad26e

Contents?: true

Size: 978 Bytes

Versions: 2

Compression:

Stored size: 978 Bytes

Contents

# frozen_string_literal: true

module Dynflow
  module Flows
    class Registry
      class IdentifierTaken < ArgumentError; end
      class UnknownIdentifier < ArgumentError; end

      class << self
        def register!(klass, identifier)
          if (found = serialization_map[identifier])
            raise IdentifierTaken, "Error setting up mapping #{identifier} to #{klass}, it already maps to #{found}"
          else
            serialization_map.update(identifier => klass)
          end
        end

        def encode(klass)
          klass = klass.class unless klass.is_a?(Class)
          serialization_map.invert[klass] || raise(UnknownIdentifier, "Could not find mapping for #{klass}")
        end

        def decode(identifier)
          serialization_map[identifier] || raise(UnknownIdentifier, "Could not find mapping for #{identifier}")
        end

        def serialization_map
          @serialization_map ||= {}
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynflow-1.9.0 lib/dynflow/flows/registry.rb
dynflow-1.8.3 lib/dynflow/flows/registry.rb