Sha256: d40956caa99fb3eaadf551fd7656e25294e0d746d5f39f9fce9c3fc75738fef2

Contents?: true

Size: 1022 Bytes

Versions: 5

Compression:

Stored size: 1022 Bytes

Contents

require "active_model"
require "msgpack_rails/activesupport/message_pack"

module ActiveModel
  module Serializers
    module MessagePack
      extend ActiveSupport::Concern
      include ActiveModel::Serialization

      included do
        extend ActiveModel::Naming

        class_attribute :include_root_in_msgpack
        self.include_root_in_msgpack = false
      end

      def as_msgpack(options = {})
        root = if options && options.key?(:root)
                 options[:root]
               else
                 include_root_in_msgpack
               end

        if root
          root = self.class.model_name.element if root == true
          { root => serializable_hash(options) }
        else
          serializable_hash(options)
        end
      end

      def from_msgpack(msgpack, include_root=include_root_in_msgpack)
        hash = ActiveSupport::MessagePack.decode(msgpack)
        hash = hash.values.first if include_root
        self.attributes = hash
        self
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
msgpack_rails-0.4.3 lib/msgpack_rails/activemodel/serializers/message_pack.rb
msgpack_rails-0.4.2 lib/msgpack_rails/activemodel/serializers/message_pack.rb
msgpack_rails-0.4.1 lib/msgpack_rails/activemodel/serializers/message_pack.rb
msgpack_rails-0.4.0 lib/msgpack_rails/activemodel/serializers/message_pack.rb
msgpack_rails-0.3.0 lib/msgpack_rails/activemodel/serializers/message_pack.rb