sqlserver-sequence 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c7bb870d95834cc1ba7d4f97dc0dfd29123426d4
4
+ data.tar.gz: 6afba73826fe2def398c39dfa30341a11a0ab49b
5
+ SHA512:
6
+ metadata.gz: 47606310d703f3cf4e14e66fbc1f611e4e221e2bfefabee6fb449c477c5e104676b0503254317189b4e3aa5933083c7f439a3d3589c54c4c40416421e56628cd
7
+ data.tar.gz: 31bfa269f1769ba4d24ef2e9d1b96f0a98bce5c4ef86f04d585a3ea46e7632384cbb327c530244ed57f525daf1a16b0492d14181a1aa46c59efbfdc3aaedbda5
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea
16
+ /bin
17
+
18
+ .idea
19
+ bin
20
+
21
+ spec/config.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at ramanathan.tirunellaisubramanian@careerbuilder.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_sequence.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ram and Zac
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # Sqlserver::Sequence [![Gem Version](https://badge.fury.io/rb/sqlserver-sequence.svg)](https://badge.fury.io/rb/sqlserver-sequence)
2
+
3
+ A Rails plugin for SQL Server sequences.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sqlserver-sequence', git: 'git@cagit.careerbuilder.com:CorpAppsCB/sqlserver-sequence.git'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ Specify the attribute you'd like to sequence
20
+
21
+ ```ruby
22
+ class Supplier < ActiveRecord::Base
23
+ sequence :number
24
+ end
25
+ ```
26
+
27
+ Now when you create a record `number` will be assigned the next value for that sequence.
28
+
29
+ $ supplier = Supplier.create
30
+ $ supplier.number
31
+ > 10 # generated by sequence named 'number'
32
+
33
+ $ other_supplier = Supplier.create
34
+ $ other_supplier.number
35
+ > 11
36
+
37
+ You can also define multiple sequences
38
+
39
+ ```ruby
40
+ class Supplier < ActiveRecord::Base
41
+ sequence :number
42
+ sequence :purchase_order
43
+ end
44
+ ```
45
+
46
+ ### Options
47
+
48
+ ###### name
49
+
50
+ 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.
51
+
52
+ ```ruby
53
+ class Supplier < ActiveRecord::Base
54
+ sequence :number, name: 'next_supplier_number'
55
+ end
56
+ ```
57
+ $ supplier = Supplier.create
58
+ $ supplier.number
59
+ > 10 # generated by sequence named 'next_supplier_number'
60
+
61
+ ###### prefix
62
+
63
+ You can use the `prefix` option to prepend every sequence value with a string.
64
+
65
+ ```ruby
66
+ class Supplier < ActiveRecord::Base
67
+ sequence :number, prefix: 'S-'
68
+ end
69
+ ```
70
+ $ supplier = Supplier.create
71
+ $ supplier.number
72
+ > S-10
73
+
74
+ ###### format
75
+
76
+ Pass a lambda to the `format` option if you need more control over the assigned sequence value.
77
+
78
+ ```ruby
79
+ class Supplier < ActiveRecord::Base
80
+ sequence :number, format: lambda { |num| num.rjust(10, 0) }
81
+ end
82
+ ```
83
+ $ supplier = Supplier.create
84
+ $ supplier.number
85
+ > 0000000010
86
+
87
+ ## Testing with SQLite
88
+
89
+ If your test suite uses SQLite you'll need to turn off sequence generation or provide a stub for `next_sequence_value`.
90
+
91
+ ```ruby
92
+ before do
93
+ allow_any_instance_of(Supplier).to receive(:next_sequence_value).and_return('10')
94
+ end
95
+ ```
96
+
97
+ ## Contributing
98
+
99
+ 1. Fork it ( https://github.com/CorpAppsCB/sqlserver-sequence/fork )
100
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
101
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
102
+ 4. Push to the branch (`git push origin my-new-feature`)
103
+ 5. Create a new Pull Request
104
+
105
+ See the [Running Tests](RUNNING_TESTS.md) guide for details on how to run the test suite on SQL Server.
data/RUNNING_TESTS.md ADDED
@@ -0,0 +1,13 @@
1
+ # Running Tests
2
+
3
+ To successfully run the test suite you'll need SQL Server credentials and the ability to create tables and sequences.
4
+
5
+ When the tests are run a `suppliers` table and sequence named `number` are created for support.
6
+
7
+ ## Getting Started
8
+
9
+ Copy config.yml.sample as config.yml
10
+
11
+ $ cp spec/config.yml.sample spec/config.yml
12
+
13
+ Then update `config.yml` with the server, database and your credentials.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,49 @@
1
+ require 'active_support/concern'
2
+ require 'sqlserver/sequence/version'
3
+
4
+ module Sqlserver
5
+ module Sequence
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ def sequence(field, options = {})
10
+ unless defined?(sequences)
11
+ include Sqlserver::Sequence::InstanceMethods
12
+
13
+ class_attribute :sequences
14
+ self.sequences = {}
15
+
16
+ before_create :set_sequences
17
+ end
18
+
19
+ default_options = { name: field.to_s, format: nil, prefix: nil }
20
+ self.sequences[field] = default_options.merge(options)
21
+ end
22
+ end
23
+
24
+ module InstanceMethods
25
+
26
+ def next_sequence_value(sequence_name)
27
+ self.class.connection.select_value(
28
+ "select next value for #{sequence_name}"
29
+ )
30
+ end
31
+
32
+ private
33
+
34
+ def set_sequences
35
+ sequences.each do |field, options|
36
+ name = options[:name]
37
+ prefix = options[:prefix]
38
+ format = options[:format]
39
+
40
+ value = next_sequence_value(name)
41
+ value = format.call(value) if format.respond_to?(:call)
42
+ send "#{field}=", [prefix, value].join
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ ActiveRecord::Base.send :include, Sqlserver::Sequence
@@ -0,0 +1,5 @@
1
+ module Sqlserver
2
+ module Sequence
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ connection:
2
+ adapter: sqlserver
3
+ dataserver: *********
4
+ database: *********
5
+ username: '*********'
6
+ password: '*********'
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ class Supplier < ActiveRecord::Base; end
4
+
5
+ describe Sqlserver::Sequence do
6
+
7
+ after(:each) { Supplier.sequences = {} }
8
+
9
+ describe '.sequences' do
10
+
11
+ context 'single sequence' do
12
+
13
+ before do
14
+ Supplier.sequence :number, name: 'sequence', prefix: 'N'
15
+ end
16
+
17
+ let(:expected) do
18
+ {
19
+ number: {
20
+ name: 'sequence',
21
+ prefix: 'N',
22
+ format: nil
23
+ }
24
+ }
25
+ end
26
+
27
+ specify { expect(Supplier.sequences).to eq(expected) }
28
+ end
29
+
30
+ context 'multiple sequences' do
31
+
32
+ before do
33
+ Supplier.sequence :number1, name: 'sequence1', prefix: 'N'
34
+ Supplier.sequence :number2, name: 'sequence2', prefix: 'N'
35
+ end
36
+
37
+ let(:expected) do
38
+ {
39
+ number1: {
40
+ name: 'sequence1',
41
+ prefix: 'N',
42
+ format: nil
43
+ },
44
+ number2: {
45
+ name: 'sequence2',
46
+ prefix: 'N',
47
+ format: nil
48
+ }
49
+ }
50
+ end
51
+
52
+ specify { expect(Supplier.sequences).to eq(expected) }
53
+ end
54
+
55
+ context 'with defaults' do
56
+
57
+ before do
58
+ Supplier.sequence :number
59
+ end
60
+
61
+ let(:expected) do
62
+ {
63
+ number: {
64
+ name: 'number',
65
+ prefix: nil,
66
+ format: nil
67
+ }
68
+ }
69
+ end
70
+
71
+ specify { expect(Supplier.sequences).to eq(expected) }
72
+ end
73
+ end
74
+
75
+ describe '#next_sequence_number' do
76
+
77
+ before do
78
+ Supplier.sequence :number
79
+ end
80
+
81
+ let!(:supplier) { Supplier.create }
82
+ let!(:other_supplier) { Supplier.create }
83
+
84
+ it 'returns the next value' do
85
+ expect(other_supplier.number).to be > supplier.number
86
+ end
87
+ end
88
+
89
+ describe 'saving' do
90
+
91
+ before do
92
+ allow_any_instance_of(Supplier).to receive(:next_sequence_value).
93
+ with('sequence').
94
+ and_return('1234567')
95
+ end
96
+
97
+ subject(:supplier) { Supplier.create }
98
+
99
+ context 'without options' do
100
+ before do
101
+ Supplier.sequence :number, name: 'sequence'
102
+ end
103
+ specify { expect(supplier.number).to eq '1234567' }
104
+ end
105
+
106
+ context 'with prefix' do
107
+ before do
108
+ Supplier.sequence :number, name: 'sequence', prefix: 'N'
109
+ end
110
+ specify { expect(supplier.number).to eq 'N1234567' }
111
+ end
112
+
113
+ context 'with format' do
114
+ before do
115
+ Supplier.sequence :number, name: 'sequence',
116
+ format: lambda { |num| num.rjust(10, '0') }
117
+ end
118
+ specify { expect(supplier.number).to eq '0001234567' }
119
+ end
120
+
121
+ context 'with prefix and format' do
122
+ before do
123
+ Supplier.sequence :number, name: 'sequence',
124
+ prefix: 'N',
125
+ format: lambda { |num| num.rjust(10, '0') }
126
+ end
127
+ specify { expect(supplier.number).to eq 'N0001234567' }
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,29 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'rspec'
4
+ require 'active_record'
5
+ require 'sqlserver/sequence'
6
+ require 'activerecord-sqlserver-adapter'
7
+ require 'tiny_tds'
8
+
9
+ # Requires everything in 'spec/support'
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ ActiveRecord::Base.establish_connection(
13
+ YAML.load_file("#{File.dirname(__FILE__)}/config.yml")['connection']
14
+ )
15
+
16
+ ActiveRecord::Schema.define do
17
+ create_table :suppliers, force: true do |t|
18
+ t.string :number
19
+ end
20
+
21
+ unless ActiveRecord::Base.connection.adapter_name == 'SQLite'
22
+ execute <<-INSERTSEQUENCESQL
23
+ IF NOT EXISTS(SELECT * FROM sys.sequences WHERE name = 'number')
24
+ CREATE SEQUENCE number
25
+ START WITH 1
26
+ INCREMENT BY 1
27
+ INSERTSEQUENCESQL
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sqlserver/sequence/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sqlserver-sequence"
8
+ spec.version = Sqlserver::Sequence::VERSION
9
+ spec.authors = ["Ram","Zac"]
10
+ spec.email = ["ramanathan.tirunellaisubramanian@careerbuilder.com",
11
+ "zachary.welch@careerbuilder.com"]
12
+
13
+ spec.summary = %q{Sequence number generation in Rails}
14
+ spec.description = %q{Sequence number generation in Rails}
15
+ spec.homepage = "https://github.com/zacharywelch/sqlserver-sequence"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "sqlite3"
27
+ spec.add_dependency "activerecord", "~> 4.2.0"
28
+ spec.add_dependency 'activerecord-sqlserver-adapter', '~> 4.2.0'
29
+ spec.add_dependency 'tiny_tds'
30
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sqlserver-sequence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ram
8
+ - Zac
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-07-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: sqlite3
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: activerecord
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 4.2.0
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 4.2.0
84
+ - !ruby/object:Gem::Dependency
85
+ name: activerecord-sqlserver-adapter
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 4.2.0
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 4.2.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: tiny_tds
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ description: Sequence number generation in Rails
113
+ email:
114
+ - ramanathan.tirunellaisubramanian@careerbuilder.com
115
+ - zachary.welch@careerbuilder.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - RUNNING_TESTS.md
128
+ - Rakefile
129
+ - lib/sqlserver/sequence.rb
130
+ - lib/sqlserver/sequence/version.rb
131
+ - spec/config.yml.sample
132
+ - spec/sequence_spec.rb
133
+ - spec/spec_helper.rb
134
+ - sqlserver-sequence.gemspec
135
+ homepage: https://github.com/zacharywelch/sqlserver-sequence
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.4.3
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Sequence number generation in Rails
159
+ test_files:
160
+ - spec/config.yml.sample
161
+ - spec/sequence_spec.rb
162
+ - spec/spec_helper.rb