Sha256: eb6ee58949d955ff3cec289b04bb96910e43e086368fec5fcbaede176c904d1b

Contents?: true

Size: 966 Bytes

Versions: 1

Compression:

Stored size: 966 Bytes

Contents

require "representable/deserializer"

module Representable
  class Serializer < Deserializer
    def call(object)
      return object if object.nil? # DISCUSS: move to Object#serialize ?

      serialize(object, @binding.user_options)
    end

  private
    # Serialize one object by calling to_json etc. on it.
    def serialize(object, user_options)
      object = prepare(object)

      return object unless @binding.representable?

      @binding.evaluate_option(:serialize, object) do
        object.send(@binding.serialize_method, user_options.merge!({:wrap => false}))
      end
    end


    class Collection < self
      def serialize(array, *args)
        array.collect { |item| super(item, *args) } # TODO: i don't want Array but Forms here - what now?
      end
    end


    class Hash < self
      def serialize(hash, *args)
        {}.tap do |hsh|
          hash.each { |key, obj| hsh[key] = super(obj, *args) }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
representable-2.1.0 lib/representable/serializer.rb