Class: MonkeyForms::Serializers::MessagePackJson

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_forms/serializers/message_pack_json.rb

Overview

Saves form as cookie as json+gzip

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MessagePackJson

Returns a new instance of MessagePackJson.



5
6
7
8
9
10
# File 'lib/monkey_forms/serializers/message_pack_json.rb', line 5

def initialize options={}
  @cookie_name = options[:name]
  @domain      = options[:domain]
  @secure      = options[:secure]
  @httponly    = options[:httponly]
end

Instance Method Details

#load(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/monkey_forms/serializers/message_pack_json.rb', line 12

def load options={}
  request = options[:request]
  result = ActiveSupport::HashWithIndifferentAccess.new
  return result if request.blank?
  cookie = request.cookies[@cookie_name]
  return result if cookie.blank?
  result.merge!(ActiveSupport::JSON.decode(MessagePack.unpack(cookie)).stringify_keys)
end

#save(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/monkey_forms/serializers/message_pack_json.rb', line 21

def save options = {}
  attributes = options[:attributes]
  response   = options[:response]

  cookie_json = ActiveSupport::JSON.encode(attributes).to_msgpack
  cookie_hash = { :value    => cookie_json,
                  :httponly => @httponly,
                  :secure   => @secure,
                  :domain   => @domain }
  response.set_cookie(@cookie_name, cookie_hash)
end