Created
October 15, 2012 13:12
-
-
Save romanbsd/3892391 to your computer and use it in GitHub Desktop.
Compressed fields in Mongoid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zlib' | |
module Mongoid | |
module Fields | |
module Compressed | |
extend ActiveSupport::Concern | |
class Field | |
include Mongoid::Fields::Serializable | |
def deserialize(object) | |
Zlib::Inflate.inflate(object.to_s) | |
end | |
def serialize(object) | |
BSON::Binary.new(Zlib::Deflate.deflate(object.to_s)) | |
end | |
end | |
module ClassMethods | |
def compressed_field(name, options = {}) | |
field(name, options.merge(type: Mongoid::Fields::Compressed::Field)) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, thanks for this! I just made a more recent version compatible with Mongoid 6.4 in case anyone's looking for this: https://gist.github.com/jarthod/6c7ddbea1c47b9ca5f159b6c28f81374