Compressed fields in Mongoid
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
This comment has been minimized.
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