Sha256: f4b4b8e6dd77cb6bbda23538b279068066d8b09598dc04625e4e3d9ed4cbae68

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'rubygems'
require 'active_support'
require 'pathname'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path  = root_path.join('lib')
$:.unshift(lib_path)

require 'adapter/memory'

# Adapter.define also takes any combination of module and block.
#
# If module present, it is included. If block present, it is turned
# into a module and included. This means that including a module and
# a block allows overriding the module by defining methods in the block.
#
# In our case below, we simply override the memory adapter to create
# a new adapter that encodes/decodes using JSON instead of the default
# Marshal.load/dump. Also, important to note that this does not affect
# the memory adapter which still uses Marshal.
Adapter.define(:memory_json, Adapter::Memory) do
  def encode(attributes)
    ActiveSupport::JSON.encode(attributes)
  end

  def decode(attributes)
    ActiveSupport::JSON.decode(attributes)
  end
end

adapter = Adapter[:memory_json].new({})
adapter.clear

adapter.write('foo', 'bar' => 'baz')
# Encoded in adapter as json instead of being marshal'd
puts adapter.client['foo'].inspect # "{\"bar\":\"baz\"}"


adapter.client['foo'] = ActiveSupport::JSON.encode('chunky' => 'bacon')
# Decoded from adapter using json instead of being un-marshal'd
puts adapter.read('foo').inspect # {"chunky"=>"bacon"}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adapter-0.6.3 examples/overriding_serialization.rb
adapter-0.6.2 examples/overriding_serialization.rb