Coder Social home page Coder Social logo

mm_uses_uuid's Introduction

mm_uses_uuid plugin

Models that use this plugin use a BSON::Binary::SUBTYPE_UUID for the model's id field rather than the default BSON::ObjectId.

Requirements

  • Ruby 1.9
  • MongoMapper 0.10.1 or greater

Installation

Add this to your Gemfile if using Bundler: gem 'mm_uses_uuid'

Or install the gem from the command line: gem install mm_uses_uuid

Usage

Use the MongoMapper plugin method to add MmUsesUuid to your model, for example:

class Group
  include MongoMapper::Document
  plugin  MmUsesUuid
  
  many :people, :class_name => 'Person'
end

The newly instantiated model will have a randomly generated UUID.

Ensuring truly unique UUIDs

If you want to make sure that the UUID hasn't already been used you can generate a new one like this:

g = Group.new
g.find_new_uuid(:ensure_unique_in => Group)

This will generate random UUIDs until it finds one that isn't in the passed collection (Group in the example). Obviously, the whole idea of random (type 4) UUIDs is that there is a tiny probability of generating duplicates. For this reason, you should only consider using :ensure_unique_in if a duplicate UUID would be a disaster for you.

Encoding class in the LSN

It is possible to encode the class of an object in its UUID by forcing the least significant nibble (the rightmost hex character) of its UUIDs to be a particular value. To do this, add the uuid_lsn method to you model and pass it a single hex character like this:

class Group
  include MongoMapper::Document
  plugin  MmUsesUuid
  
  many :people, :class_name => 'Person'
  
  uuid_lsn 0x0
end

Once this value is set you can use UuidModel.find(...) to find by id (or a list of ids) and it will automatically detect the class by inspecting the last character of the UUIDs you pass. So for the example above, all UUIDs generated for new Group objects will end in '0' and, if you pass a UUID ending in '0' to UuidModel.find, it will pass that request on to Group.find(). Clearly, this method only allows you to target 16 collections at most as the last nibble can only have values of 0 to 15.

This method can be useful if you need to implement a polymorphic many-to-many association but you don't want to use Single Collection Inheritance because the polymorphic values have significantly different behaviours and attributes. The following example shows how to do this:

class Person
  include MongoMapper::Document
  plugin  MmUsesUuid
  
  key :name
  key :age
  
  key  :interest_ids, Array
  many :interests, :in => :interest_ids, :class_name => 'UuidModel' #values can be a Group or a Person
  
  belongs_to :group
  
  uuid_lsn 0xf
end

Copyright (c) 2011 PeepAll Ltd, released under the MIT license

mm_uses_uuid's People

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

marshallmick007

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.