Skip to content

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharywelch committed Jul 9, 2016
1 parent 015464e commit 0b11eba
Show file tree
Hide file tree
Showing 15 changed files with 481 additions and 2 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log
.idea
/bin

.idea
bin

spec/config.yml
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.2.3
before_install: gem install bundler -v 1.12.5
49 changes: 49 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Contributor Code of Conduct

As contributors and maintainers of this project, and in the interest of
fostering an open and welcoming community, we pledge to respect all people who
contribute through reporting issues, posting feature requests, updating
documentation, submitting pull requests or patches, and other activities.

We are committed to making participation in this project a harassment-free
experience for everyone, regardless of level of experience, gender, gender
identity and expression, sexual orientation, disability, personal appearance,
body size, race, ethnicity, age, religion, or nationality.

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information, such as physical or electronic
addresses, without explicit permission
* Other unethical or unprofessional conduct

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

By adopting this Code of Conduct, project maintainers commit themselves to
fairly and consistently applying these principles to every aspect of managing
this project. Project maintainers who do not follow or enforce the Code of
Conduct may be permanently removed from the project team.

This code of conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting a project maintainer at [email protected]. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. Maintainers are
obligated to maintain confidentiality with regard to the reporter of an
incident.

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 1.3.0, available at
[http://contributor-covenant.org/version/1/3/0/][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/3/0/
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in acts_as_sequence.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Ram and Zac

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
113 changes: 111 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,111 @@
# sequencer
Sequence Generator
# Sqlserver::Sequence

[![Gem Version](https://badge.fury.io/rb/sqlserver-sequence.svg)](https://badge.fury.io/rb/sqlserver-sequence)

A Rails plugin for SQL Server sequences.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'sqlserver-sequence'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install sqlserver-sequence

## Usage

Specify the attribute you'd like to sequence

```ruby
class Supplier < ActiveRecord::Base
sequence :number
end
```

Now when you create a record `number` will be assigned the next value for that sequence.

$ supplier = Supplier.create
$ supplier.number
> 10 # generated by sequence named 'number'

$ other_supplier = Supplier.create
$ other_supplier.number
> 11

You can also define multiple sequences

```ruby
class Supplier < ActiveRecord::Base
sequence :number
sequence :purchase_order
end
```

### Options

###### name

By default `sqlserver-sequence` will look for a row in `sys.sequences` with the same `name` as the attribute. You can use the `name` option if the sequence is named differently.

```ruby
class Supplier < ActiveRecord::Base
sequence :number, name: 'next_supplier_number'
end
```
$ supplier = Supplier.create
$ supplier.number
> 10 # generated by sequence named 'next_supplier_number'

###### prefix

You can use the `prefix` option to prepend every sequence value with a string.

```ruby
class Supplier < ActiveRecord::Base
sequence :number, prefix: 'S-'
end
```
$ supplier = Supplier.create
$ supplier.number
> S-10

###### format

Pass a lambda to the `format` option if you need more control over the assigned sequence value.

```ruby
class Supplier < ActiveRecord::Base
sequence :number, format: lambda { |num| num.rjust(10, 0) }
end
```
$ supplier = Supplier.create
$ supplier.number
> 0000000010

## Testing with SQLite

If your test suite uses SQLite you'll need to turn off sequence generation or provide a stub for `next_sequence_value`.

```ruby
before do
allow_any_instance_of(Supplier).to receive(:next_sequence_value).and_return('10')
end
```

## Contributing

1. Fork it ( https://github.com/zacharywelch/sqlserver-sequence/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

See the [Running Tests](RUNNING_TESTS.md) guide for details on how to run the test suite on SQL Server.
13 changes: 13 additions & 0 deletions RUNNING_TESTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Running Tests

To successfully run the test suite you'll need SQL Server credentials and the ability to create tables and sequences.

When the tests are run a `suppliers` table and sequence named `number` are created for support.

## Getting Started

Copy config.yml.sample as config.yml

$ cp spec/config.yml.sample spec/config.yml

Then update `config.yml` with the server, database and your credentials.
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
49 changes: 49 additions & 0 deletions lib/sqlserver/sequence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'active_support/concern'
require 'sqlserver/sequence/version'

module Sqlserver
module Sequence
extend ActiveSupport::Concern

class_methods do
def sequence(field, options = {})
unless defined?(sequences)
include Sqlserver::Sequence::InstanceMethods

class_attribute :sequences
self.sequences = {}

before_create :set_sequences
end

default_options = { name: field.to_s, format: nil, prefix: nil }
self.sequences[field] = default_options.merge(options)
end
end

module InstanceMethods

def next_sequence_value(sequence_name)
self.class.connection.select_value(
"select next value for #{sequence_name}"
)
end

private

def set_sequences
sequences.each do |field, options|
name = options[:name]
prefix = options[:prefix]
format = options[:format]

value = next_sequence_value(name)
value = format.call(value) if format.respond_to?(:call)
send "#{field}=", [prefix, value].join
end
end
end
end
end

ActiveRecord::Base.send :include, Sqlserver::Sequence
5 changes: 5 additions & 0 deletions lib/sqlserver/sequence/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Sqlserver
module Sequence
VERSION = "0.1.1"
end
end
6 changes: 6 additions & 0 deletions spec/config.yml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
connection:
adapter: sqlserver
dataserver: *********
database: *********
username: '*********'
password: '*********'
Loading

0 comments on commit 0b11eba

Please sign in to comment.