Sha256: d59491d60a6025e7d3caf0dcff9c435d99c858b9fd0afac49282e68878c12c59
Contents?: true
Size: 1.65 KB
Versions: 1
Compression:
Stored size: 1.65 KB
Contents
module BSONMiniHash def self.bson_lib case when Kernel.const_defined?(:Moped) # Mongoid :moped when Kernel.const_defined?(:BSON) # Mongo driver (incliding MongoMapper) :bson else raise "In order to use BSONMiniHash, you need to have either the 'bson' or 'moped' gem loaded" end end # # Beware, if you're not using SHA1, MD5 or any other hash with a length divisible by two, # you'll get a zero padded string when doing a round trip as the packed string will have # an extra nibble of space at the end. # def self.pack(str, type = :hex) str = str.downcase.strip packed_str = \ case when Hex::TYPES.include?(type) Hex.pack(str, type) else raise ArgumentError, "type #{type} not supported" end bson_obj(packed_str) end def self.digest(data, type) packed_str = \ case when Hex::TYPES.include?(type) Hex.digest(data, type) else raise ArgumentError, "type #{type} not supported" end bson_obj(packed_str) end def self.unpack(packed_obj, type = :hex) # # Both BSON and Moped give the right thing for to_s # packed_str = packed_obj.to_s case when Hex::TYPES.include?(type) Hex.unpack(packed_str) else raise ArgumentError, "type #{type} not supported" end end private def self.bson_obj(packed_str) case self.bson_lib when :bson BSON::Binary.new(packed_str) when :moped Moped::BSON::Binary.new(:user, packed_str) end end end require "bson_minihash/version" require "bson_minihash/hex"
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
bson_minihash-0.0.1 | lib/bson_minihash.rb |