Giter Site home page Giter Site logo

abhionlyone / mongoid-sequence2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jhjguxin/mongoid-sequence2

0.0 2.0 0.0 369 KB

Specify fields to behave like a sequence number (exactly like the "id" column in conventional SQL flavors) while using Mongoid.

License: MIT License

Ruby 100.00%

mongoid-sequence2's Introduction

Mongoid Sequence

Mongoid Sequence allows you to specify fields to behave like a sequence number (exactly like the "id" column in conventional SQL flavors).

Credits

This gem was inspired by a couple of gists by masatomo and ShogunPanda.

and

https://github.com/agile42/mongoid_sequence2/ from francis

Usage

setup

since v0.2.3 you can specific custom default storage options for Mongoid::Sequences, eg:

#config/initializers/sequence2.rb
Mongoid::Sequence2.setup do |config|
 config.collection_name = :__sequences
 config.session = :default
end

Include Mongoid::Sequence in your class and call sequence(:field).

Like this:

class Sequenced
	include Mongoid::Document
	include Mongoid::Sequence

	field :my_sequence, :type => Integer
	sequence :my_sequence
end

s1 = Sequenced.create
s1.sequence #=> 1

s2 = Sequenced.create
s2.sequence #=> 2 # and so on

It's also possible to make the id field behave like this:

class Sequenced
	include Mongoid::Document
	include Mongoid::Sequence

	sequence :_id
end

s1 = Sequenced.create
s1.id #=> 1

s2 = Sequenced.create
s2.id #=> 2 # and so on

You can also have any per model sequence without need to create given model's objects:

class Sequenced
	include Mongoid::Document
	include Mongoid::Sequence
end

Sequenced.next_sequence('some_name_1') #=> 1
Sequenced.next_sequence('some_name_1') #=> 2
Sequenced.next_sequence('some_name_2') #=> 1
# can also give an special step
Sequenced.next_sequence('some_name_2', 2) #=> 3

notice:

for v0.2.5, step should only work with no auto_increment field, more info check test/reference_test.rb

Consistency

Mongoid::Sequence uses the atomic findAndModify command, so you shouldn't have to worry about the sequence's consistency.

Installation

Just add it to your projects' Gemfile:

gem "mongoid-sequence2"

Thanks

thanks masatomo, goncalossilva, agile42


Copyright © 2010 Gonçalo Silva, released under the MIT license

mongoid-sequence2's People

Contributors

goncalossilva avatar jhjguxin avatar abhionlyone avatar

Watchers

James Cloos avatar  avatar

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.