0.0
No commit activity in last 3 years
No release in over 3 years
Add simple adjust! method to Mongoid.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.0.1

Runtime

>= 1.2
>= 2.0.1
 Project Readme

Mongoid adjust

Adds adjust method to Mongoid::Document so you can adjust multiple items in a collection with the same mutation.

Install

gem install mongoid_adjust

Usage

  require 'mongoid_adjust'
  
  # adjust all items where pos > 3, adding 1 to their position
  list.items.where(:pos.gt => 3).adjust!(:pos => 1)  

  # Make name of each item uppercase 
  list.items.adjust!(:name => :upcase)  

  # Multiply all item positions by 2
  list.items.adjust!(:pos => lambda{|i| i*2})

+Note:+

This functionality was taken from Mongo Mapper where it builds on the native multi-update functionality exposed by Mongo DB. Currently this project doesn't use the native Mongo DB functionality but that is a future goal!

TODO

The adjust! should allow for an operator symbol (:add, :subtract, :multiply, :divide), defaulting to :add

  list.items.where(:pos.gt => 3).adjust!(:subtract, :pos => 1)  

The above can currently be achieved using adjust!(:pos => -1), adding -1 to each.

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.