Sha256: 66f2ced242b7cd9d26dcd5180506598ce5d79a18c14f0fac60ffbb74a5290f99

Contents?: true

Size: 832 Bytes

Versions: 1

Compression:

Stored size: 832 Bytes

Contents

# encoding: utf-8
require 'base64'

# Stolen fr... I mean 'borrowed' from DragonFly
# https://github.com/markevans/dragonfly/blob/master/lib/dragonfly/serializer.rb
#
module Anisoptera
  module Serializer
    
    # Exceptions
    class BadString < RuntimeError; end
    
    extend self # So we can do Serializer.b64_encode, etc.
    
    def b64_encode(string)
      Base64.encode64(string).tr("\n=",'')
    end
    
    def b64_decode(string)
      padding_length = string.length % 4
      Base64.decode64(string + '=' * padding_length)
    end
    
    def marshal_encode(object)
      b64_encode(Marshal.dump(object))
    end
    
    def marshal_decode(string)
      Marshal.load(b64_decode(string))
    rescue TypeError, ArgumentError => e
      raise BadString, "couldn't decode #{string} - got #{e}"
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
anisoptera-0.0.2 lib/anisoptera/serializer.rb