module MmUsesUuid::ClassMethods

Public Instance Methods

convert_ids_to_BSON(ids) click to toggle source
# File lib/mm_uses_uuid.rb, line 89
def convert_ids_to_BSON(ids)
  ids.map {|id| BsonUuid.to_mongo(id)}
end
deserialize_id(id) click to toggle source
# File lib/mm_uses_uuid.rb, line 53
def deserialize_id(id)
  BSON::Binary.new(id, BSON::Binary::SUBTYPE_UUID)
end
find(*ids) click to toggle source
Calls superclass method
# File lib/mm_uses_uuid.rb, line 73
def find(*ids)
  batch_mode = ids.first.is_a?(Array) || ids.length > 1
  ids.flatten!
  ids = convert_ids_to_BSON(ids)
  results = super(ids)
  batch_mode ? results : results.first
end
find!(*ids) click to toggle source
Calls superclass method
# File lib/mm_uses_uuid.rb, line 81
def find!(*ids)
  batch_mode = ids.first.is_a?(Array) || ids.length > 1
  ids.flatten!
  ids = convert_ids_to_BSON(ids)
  results = super(ids)
  batch_mode ? results : results.first
end
make_uuid() click to toggle source
# File lib/mm_uses_uuid.rb, line 57
def make_uuid
  uuid = SecureRandom.uuid.gsub!('-', '')
  if single_collection_inherited? and not embeddable?
    lookup_class_name = collection_name.singularize.camelize
  else
    lookup_class_name = name
  end
  replacement_lsn = UuidModel.class_lsn_lookup[lookup_class_name] || 0x00
  uuid[-2..-1] = replacement_lsn.to_s(16).rjust(2,'0')
  BSON::Binary.new(uuid, BSON::Binary::SUBTYPE_UUID)
  
rescue => e
  binding.pry
  raise e
end
serialize_id(object) click to toggle source
# File lib/mm_uses_uuid.rb, line 44
def serialize_id(object)
  case object
  when MongoMapper::Document, MongoMapper::EmbeddedDocument
    object.id.to_s
  else
    object.to_s
  end       
end
uuid_lsn(lsn_integer) click to toggle source

def new(params = {}) passed_id = params.delete(:id) || params.delete(:_id) || params.delete('id') || params.delete('_id') new_object = super(params) if passed_id if passed_id.is_a?(BSON::Binary) and passed_id.subtype == BSON::Binary::SUBTYPE_UUID new_object.id = passed_id else raise ArgumentError, “if you pass an explicit :id parameter it must be a valid BSON::Binary::SUBTYPE_UUID” end else new_object.find_new_uuid end new_object end

# File lib/mm_uses_uuid.rb, line 108
def uuid_lsn(lsn_integer)
  raise "lsn_integer must be from 0-255" unless (0..255).cover?(lsn_integer)
  UuidModel.add_lsn_mapping(lsn_integer, self.name)
end