No release in over 3 years
Low commit activity in last 3 years
Provide access to PostgreSQL's sequences
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 2.1.0
= 11.3.0
= 3.5.0
= 0.64.0

Runtime

>= 4.2.11, <= 6.0.0.beta1
>= 0
 Project Readme

Build Status Gem Version

ActiveRecord::Sequence

Access to PostgreSQL's Sequences

Installation

Add this line to your application's Gemfile:

gem 'active_record-sequence'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_record-sequence

Usage

By default new sequence starts from 1:

sequence = ActiveRecord::Sequence.create('numbers')

#next returns next value in the sequence:

sequence.next #=> 1
sequence.next #=> 2

#peek returns current value:

sequence.peek #=> 2

You can start a sequence with specific value:

sequence = ActiveRecord::Sequence.create('numbers', start: 42)
sequence.next #=> 42
sequence.next #=> 43

Specify custom increment value:

sequence = ActiveRecord::Sequence.create('numbers', increment: 3)
sequence.next #=> 1
sequence.next #=> 4

If you pass negative increment, a sequence will be decreasing:

sequence = ActiveRecord::Sequence.create('numbers', increment: -3)
sequence.next #=> -1
sequence.next #=> -4

To limit number of elements in a sequence specify max value:

sequence = ActiveRecord::Sequence.create('numbers', max: 2)
sequence.next #=> 1
sequence.next #=> 2
sequence.next #=> fail with StopIteration

Decreasing sequence may be limited as well:

sequence = ActiveRecord::Sequence.create('numbers', min: -2, increment: -1)
sequence.next #=> -1
sequence.next #=> -2
sequence.next #=> fail with StopIteration

To define infinite sequence, use cycle option:

sequence = ActiveRecord::Sequence.create('numbers', max: 2, cycle: true)
sequence.next #=> 1
sequence.next #=> 2
sequence.next #=> 1
sequence.next #=> 2
# etc.

You can use previously created sequence by instantiating Sequence class:

ActiveRecord::Sequence.create('numbers', max: 2, cycle: true)
sequence = ActiveRecord::Sequence.new('numbers')
sequence.next #=> 1
sequence.next #=> 2
sequence.next #=> 1

To destroy a sequence:

ActiveRecord::Sequence.drop('numbers')

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

We test this gem against different versions of ActiveRecord using appraisal gem. To regenerate gemfiles run:

$ appraisal install

To run specs against all versions:

$ appraisal rake spec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bolshakov/active_record-sequence.