Module: ActiveModel::Serializers::MessagePack

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Serialization
Defined in:
lib/msgpack_rails/activemodel/serializers/message_pack.rb

Instance Method Summary collapse

Instance Method Details

#as_msgpack(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/msgpack_rails/activemodel/serializers/message_pack.rb', line 17

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

#from_msgpack(msgpack, include_root = include_root_in_msgpack) ⇒ Object



32
33
34
35
36
37
# File 'lib/msgpack_rails/activemodel/serializers/message_pack.rb', line 32

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