Over the last months and weeks and days I contributed some tiny gems (I call them microgems) for the really awesome mongoid ORM.
With the release of the third microgem I also began my meta gem MongoidMidgets to easily include all my stuff with a single line in my Gemfile.
gem "mongoid_midgets", :require => ["mongoid_bitfield", "mongoid_touch", "mongoid_upk"]
mongoid_touch gem was already introduced in another blog post (Ruby Gem Contributions).
Side fact: Durran, the maintainer of mongoid, asked me, if I want to merge the mongoid_touch into the core. Unfortunatelly I didn't invest the time to do so, because this needs a lot of work to have a really competitable functionality like in ActiveRecord and mongoid is also a huge code base where you can get lost like Alice in Wonderland … ;o)
A short overview of the other microgems:
This tiny gem provides a bitfield or a collection of boolean bits/flags.
This bitfield will be stored as an integer value in the mongodb database.
In the project of my current employer I found this tiny bitfields but for ActiveRecord. I also want to have them in Mongoid, too.
Currently these bits in a bitfield aren't searchable, so this gem is just for fun now. Maybe later it will extended to have more usage of it.
Main benefit should be to save a little bit space in the database, because an integer value should always be smaller than an array or a hash of multiple booleans. For models with huge sets of boolean fields it could really helpful despite the fact of missing searchability.
class User include Mongoid::Document include Mongoid::Bitfield bitfield :flags, :is_admin, :adult, :drinker end ### no save user = User.new user.is_admin #=> false user.is_admin = true user.is_admin #=> true ### atomic save user = User.create! user.drinker_enable! user.reload user.drinker #=> true
- More infos on github: asaaki/mongoid_bitfield
- Current build status:
Mongoid::UPK generates more unique _id by using UUID.
UPK stands for "unique primary key" or "uuid primary key".
While writing very fast to MongoDB it is possible to take a BSON::ObjectId twice. This will lead to big troubles because you never know which document will be returned.
This gem utilizes the key method and overwrites the automatically given default id with the choosen generator on save. So the real id can be read first after a successful save (I don't want to hack deeper here, because saves are more important than getting a unique id on instance initialization).
UUID is in most cases more reliable, especially if the app instances are set up correctly (and/or running on different machines).
### base pk => c1b457b03214012f316d10e6baa29f2c class MyModel include Mongoid::Document include Mongoid::UPK uuid_pk # you can leave this out, because it's the default generator end ### pretty pk (with dashes) => c1b457b0-3214-012f-316d-10e6baa29f2c class MyModel include Mongoid::Document include Mongoid::UPK pretty_uuid_pk end ### self-defined pk gen class MyModel include Mongoid::Document include Mongoid::UPK unique_pk do # code goes here # result must be a string # example: Digest::MD5.hexdigest(Time.now.to_f.to_s) end end
- More infos on github: asaaki/mongoid_upk
- Current build status:
Feel free to contribute!
Or write your own microgems to mongoid.
You can also ask me to include them into MongoidMidgets. If I like them and they are very useful, I will put them into my collection.