Class: Rack::Session::SmartCookie::MessagePack

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/session/smart_cookie.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|@factory| ... } ⇒ MessagePack

Returns a new instance of MessagePack.

Yields:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rack/session/smart_cookie.rb', line 45

def initialize
  # Create our own factory so we don't pollute the global namespace
  # with our custom type.
  @factory = ::MessagePack::Factory.new

  # MessagePack gets custom types 0x80..0xFF
  # we get 0x60...0x80
  @factory.register_type(0x60, Symbol)
  # user gets 0x00...0x60
  yield @factory if block_given?
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



43
44
45
# File 'lib/rack/session/smart_cookie.rb', line 43

def factory
  @factory
end

Instance Method Details

#decode(bin) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rack/session/smart_cookie.rb', line 62

def decode(bin)
  return unless bin

  # https://github.com/msgpack/msgpack-ruby/issues/141
  factory.unpacker.feed(bin).read
rescue
end

#encode(data) ⇒ Object



57
58
59
60
# File 'lib/rack/session/smart_cookie.rb', line 57

def encode(data)
  # https://github.com/msgpack/msgpack-ruby/issues/141
  factory.packer.write(data).to_str
end