sqlserver-sequence 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea055f44adbc2fdc216305cab3d9fca8d158f947
4
- data.tar.gz: 648a4d03260ab26bc7437bc61196687d8142e638
3
+ metadata.gz: ae433f7e06f518a5cb7e1e8ea7658ad1ebf4622b
4
+ data.tar.gz: 57e0f2334be57255b6422ea4c74aaab12398c6c2
5
5
  SHA512:
6
- metadata.gz: fd6eabfe7c6a303167b62fadf6af2caf87d42ee3564187657b627dd7ea5b50a1196a8d97fe573186832a6ab175d296c45a3f8814dec6400d5c53c39f4e91627e
7
- data.tar.gz: 158697f35073a7f5068307cbf03706643f7e51d6136bbda21154d5a50434c8dd275650921e84bddc9ba71b7b859df480bdbf147d53dffbea16e1a56559ea0591
6
+ metadata.gz: 806c283d8fb10cacfb17666cdcb0c82bf2697d519a13db6ad97422c9e9fa0a4932abbe5a6af1460e77bce891b616efe21f7f79383c8884e92cda2939e1a9f00d
7
+ data.tar.gz: 7c668647ae2d70b22e53a22d91a1aeaeb33354d028d373517167e8d54206477cce0bd2ecac29dac15d1ea3932e3a3b10cfb256ed673d8b0c997084264992a112
data/.gitignore CHANGED
@@ -14,7 +14,7 @@
14
14
  mkmf.log
15
15
  .idea
16
16
  /bin
17
-
17
+ .rspec
18
18
  .idea
19
19
  bin
20
20
 
@@ -35,7 +35,7 @@ This code of conduct applies both within project spaces and in public spaces
35
35
  when an individual is representing the project or its community.
36
36
 
37
37
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
- reported by contacting a project maintainer at ramanathan.tirunellaisubramanian@careerbuilder.com. All
38
+ reported by contacting one or more of the project maintainers. All
39
39
  complaints will be reviewed and investigated and will result in a response that
40
40
  is deemed necessary and appropriate to the circumstances. Maintainers are
41
41
  obligated to maintain confidentiality with regard to the reporter of an
data/README.md CHANGED
@@ -93,13 +93,44 @@ end
93
93
  $ supplier.number
94
94
  > S-0000000010
95
95
 
96
- ## Testing with SQLite
97
96
 
98
- If your test suite uses SQLite you'll need to turn off sequence generation or provide a stub for `next_sequence_value`.
97
+ ## Configuration
98
+
99
+ Override any of these defaults in `config/initializers/sqlserver_sequence.rb`:
100
+
101
+ ```ruby
102
+ Sqlserver::Sequence.configure do |config|
103
+ config.next_value_strategy = Sqlserver::Sequence::Strategies::NextValueFor
104
+ end
105
+ ```
106
+
107
+ ### next_value_strategy
108
+
109
+ By default `Sqlserver::Sequence` will use a strategy that implements SQL Server's [NEXT VALUE FOR](https://msdn.microsoft.com/en-us/library/ff878370.aspx) function.
110
+
111
+ To change how sequences are generated you can also assign your own strategy:
112
+
113
+ ```ruby
114
+ # lib/size_strategy.rb
115
+ module SizeStrategy
116
+ def next_sequence_value(sequence_name)
117
+ self.class.size
118
+ end
119
+ end
120
+
121
+ # config/initializers/sqlserver_sequence.rb
122
+ Sqlserver::Sequence.configure do |config|
123
+ config.next_value_strategy = SizeStrategy
124
+ end
125
+ ```
126
+
127
+ ### Development and Testing without SQL Server
128
+
129
+ If your test or development environment uses something other than SQL Server (e.g. SQLite) a `Simple` strategy is provided to avoid errors.
99
130
 
100
131
  ```ruby
101
- before do
102
- allow_any_instance_of(Supplier).to receive(:next_sequence_value).and_return('10')
132
+ Sqlserver::Sequence.configure do |config|
133
+ config.next_value_strategy = Sqlserver::Sequence::Strategies::Simple
103
134
  end
104
135
  ```
105
136
 
@@ -1,15 +1,18 @@
1
1
  require 'active_support/concern'
2
+ require 'sqlserver/sequence/configuration'
3
+ require 'sqlserver/sequence//strategies'
2
4
  require 'sqlserver/sequence/version'
3
5
 
4
6
  module Sqlserver
5
7
  module Sequence
6
8
  extend ActiveSupport::Concern
7
9
 
8
- class_methods do
10
+ module ClassMethods
9
11
  def sequence(field, options = {})
10
12
  unless defined?(sequences)
11
13
  include Sqlserver::Sequence::InstanceMethods
12
-
14
+ include next_value_strategy
15
+
13
16
  class_attribute :sequences
14
17
  self.sequences = {}
15
18
 
@@ -19,16 +22,17 @@ module Sqlserver
19
22
  default_options = { name: field.to_s, format: nil, prefix: nil }
20
23
  self.sequences[field] = default_options.merge(options)
21
24
  end
25
+
26
+ private
27
+
28
+ def next_value_strategy
29
+ Sqlserver::Sequence.configuration.next_value_strategy ||
30
+ Strategies::NextValueFor
31
+ end
22
32
  end
23
33
 
24
34
  module InstanceMethods
25
35
 
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
36
  private
33
37
 
34
38
  def set_sequences
@@ -0,0 +1,21 @@
1
+ module Sqlserver
2
+ module Sequence
3
+ class Configuration
4
+ # The next value strategy is used when generating the next sequence value.
5
+ # Defaults to {Sqlserver::Sequence::Strategies::NextValueFor}.
6
+ attr_accessor :next_value_strategy
7
+ end
8
+
9
+ def self.configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def self.configuration=(config)
14
+ @configuration = config
15
+ end
16
+
17
+ def self.configure
18
+ yield configuration
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ module Sqlserver
2
+ module Sequence
3
+ module Strategies
4
+ autoload :NextValueFor, 'sqlserver/sequence/strategies/next_value_for'
5
+ autoload :Simple, 'sqlserver/sequence/strategies/simple'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ module Sqlserver
2
+ module Sequence
3
+ module Strategies
4
+ module NextValueFor
5
+ def next_sequence_value(sequence_name)
6
+ self.class.connection.select_value(
7
+ "select next value for #{sequence_name}"
8
+ )
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Sqlserver
2
+ module Sequence
3
+ module Strategies
4
+ module Simple
5
+ def next_sequence_value(sequence_name)
6
+ self.class.maximum(:id).to_i.next
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  module Sqlserver
2
2
  module Sequence
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sqlserver::Sequence::Configuration do
4
+
5
+ context 'when next_value_strategy is set' do
6
+ let(:mock_strategy) { Module.new }
7
+
8
+ before do
9
+ Sqlserver::Sequence.configure do |config|
10
+ config.next_value_strategy = mock_strategy
11
+ end
12
+
13
+ spawn_model(:Supplier) { sequence :number }
14
+ end
15
+
16
+ it 'includes that strategy' do
17
+ expect(Supplier.new).to be_kind_of(mock_strategy)
18
+ end
19
+ end
20
+
21
+ context 'when next_value_strategy is not set' do
22
+ before do
23
+ Sqlserver::Sequence.configure do |config|
24
+ config.next_value_strategy = nil
25
+ end
26
+
27
+ spawn_model(:Supplier) { sequence :number }
28
+ end
29
+
30
+ it 'includes Sqlserver::NextValueStrategies::NextValueFor' do
31
+ expect(Supplier.new).to be_kind_of(
32
+ Sqlserver::Sequence::Strategies::NextValueFor
33
+ )
34
+ end
35
+ end
36
+ end
@@ -1,15 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
- class Supplier < ActiveRecord::Base; end
4
-
5
3
  describe Sqlserver::Sequence do
6
4
 
7
- after(:each) { Supplier.sequences = {} }
5
+ before { spawn_model :Supplier }
8
6
 
9
7
  describe '.sequences' do
10
8
 
11
9
  context 'single sequence' do
12
-
13
10
  before do
14
11
  Supplier.sequence :number, name: 'sequence', prefix: 'N'
15
12
  end
@@ -28,7 +25,6 @@ describe Sqlserver::Sequence do
28
25
  end
29
26
 
30
27
  context 'multiple sequences' do
31
-
32
28
  before do
33
29
  Supplier.sequence :number1, name: 'sequence1', prefix: 'N'
34
30
  Supplier.sequence :number2, name: 'sequence2', prefix: 'N'
@@ -53,7 +49,6 @@ describe Sqlserver::Sequence do
53
49
  end
54
50
 
55
51
  context 'with defaults' do
56
-
57
52
  before do
58
53
  Supplier.sequence :number
59
54
  end
@@ -72,20 +67,6 @@ describe Sqlserver::Sequence do
72
67
  end
73
68
  end
74
69
 
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
70
  describe 'saving' do
90
71
 
91
72
  before do
@@ -27,3 +27,17 @@ ActiveRecord::Schema.define do
27
27
  INSERTSEQUENCESQL
28
28
  end
29
29
  end
30
+
31
+ RSpec.configure do |config|
32
+ config.include Sqlserver::Sequence::Testing::ModelMacros
33
+
34
+ config.before :each do
35
+ @spawned_models = []
36
+ end
37
+
38
+ config.after :each do
39
+ @spawned_models.each do |model|
40
+ Object.instance_eval { remove_const model } if Object.const_defined?(model)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sqlserver::Sequence::Strategies::Simple do
4
+
5
+ before do
6
+ Sqlserver::Sequence.configure do |config|
7
+ config.next_value_strategy = Sqlserver::Sequence::Strategies::Simple
8
+ end
9
+
10
+ spawn_model(:Supplier) { sequence :number }
11
+ end
12
+
13
+ describe 'saving' do
14
+
15
+ let!(:supplier) { Supplier.create }
16
+ let!(:other_supplier) { Supplier.create }
17
+
18
+ it 'returns the next value' do
19
+ expect(other_supplier.number).to be > supplier.number
20
+ end
21
+
22
+ it 'uses the maximum id' do
23
+ expected = Supplier.maximum(:id).next.to_s
24
+ expect(Supplier.create.number).to eq expected
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ module Sqlserver::Sequence
2
+ module Testing
3
+ module ModelMacros
4
+ def spawn_model(klass, &block)
5
+ Object.instance_eval { remove_const klass } if Object.const_defined?(klass)
6
+ Object.const_set klass, Class.new(ActiveRecord::Base)
7
+ Object.const_get(klass).class_eval(&block) if block_given?
8
+ @spawned_models << klass.to_sym
9
+ end
10
+ end
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlserver-sequence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zac
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-12 00:00:00.000000000 Z
12
+ date: 2016-07-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -119,7 +119,6 @@ extra_rdoc_files: []
119
119
  files:
120
120
  - ".gitignore"
121
121
  - ".rspec"
122
- - ".travis.yml"
123
122
  - CODE_OF_CONDUCT.md
124
123
  - Gemfile
125
124
  - LICENSE.txt
@@ -127,10 +126,17 @@ files:
127
126
  - RUNNING_TESTS.md
128
127
  - Rakefile
129
128
  - lib/sqlserver/sequence.rb
129
+ - lib/sqlserver/sequence/configuration.rb
130
+ - lib/sqlserver/sequence/strategies.rb
131
+ - lib/sqlserver/sequence/strategies/next_value_for.rb
132
+ - lib/sqlserver/sequence/strategies/simple.rb
130
133
  - lib/sqlserver/sequence/version.rb
131
134
  - spec/config.yml.sample
135
+ - spec/configuration_spec.rb
132
136
  - spec/sequence_spec.rb
133
137
  - spec/spec_helper.rb
138
+ - spec/strategies/simple_spec.rb
139
+ - spec/support/model_macros.rb
134
140
  - sqlserver-sequence.gemspec
135
141
  homepage: https://github.com/zacharywelch/sqlserver-sequence
136
142
  licenses:
@@ -158,5 +164,8 @@ specification_version: 4
158
164
  summary: Sequence number generation in Rails
159
165
  test_files:
160
166
  - spec/config.yml.sample
167
+ - spec/configuration_spec.rb
161
168
  - spec/sequence_spec.rb
162
169
  - spec/spec_helper.rb
170
+ - spec/strategies/simple_spec.rb
171
+ - spec/support/model_macros.rb
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.2.3
5
- before_install: gem install bundler -v 1.12.5