Sha256: 4f304ab277657b6f93d610b9939ebffe901717eadf0bdccd0b7ca981e748eee6

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Transmutation
  module Serialization
    # Serialize a given object with the looked up serializer.
    #
    #
    # @param object [Object] The object to serialize.
    # @param namespace [String, Symbol, Module] The namespace to lookup the serializer in.
    # @param serializer [String, Symbol, Class] The serializer to use.
    #
    # @return [Transmutation::Serializer] The serialized object. This will respond to `#as_json` and `#to_json`.
    def serialize(object, namespace: nil, serializer: nil)
      lookup_serializer(object, namespace: namespace, serializer: serializer).new(object)
    end

    # Lookup the serializer for the given object.
    #
    # This calls {Transmutation::Serialization::Lookup#serializer_for} to find the serializer for the given object.
    #
    # @param object [Object] The object to lookup the serializer for.
    # @param namespace [String, Symbol, Module] The namespace to lookup the serializer in.
    # @param serializer [String, Symbol, Class] The serializer to use.
    #
    # @return [Class<Transmutation::Serializer>] The serializer for the given object.
    #
    def lookup_serializer(object, namespace: nil, serializer: nil)
      Lookup.new(self, namespace: namespace).serializer_for(object, serializer: serializer)
    end

    private_class_method def self.included(base)
      base.include(Rendering) if base.respond_to?(:render)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
transmutation-0.2.3 lib/transmutation/serialization.rb
transmutation-0.2.2 lib/transmutation/serialization.rb
transmutation-0.2.1 lib/transmutation/serialization.rb
transmutation-0.2.0 lib/transmutation/serialization.rb