gen_sid 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3e85f018eee3cb1b5826585b177b232370ff9ac0
4
+ data.tar.gz: 17a7ccae61acfd7fe180b73781f430e499384a15
5
+ SHA512:
6
+ metadata.gz: 651db99fe7a9cb5497edb9ae74e30dc1c866827c38a1d1eccce1de30024af47b85c2422d015ebae470026eae717f74b137d0c40c976520d02dcc71546fef108f
7
+ data.tar.gz: 9c05dc4279c60cef6b81750649cb64886caedbd682f12d857c6d8f1cccc8628535312734898a8c48600ae25429bb90de58fa74277d8aa7ae4f293a7fb1700ac2
@@ -0,0 +1,15 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gen_sid.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tracy Flynn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # GenSid
2
+
3
+ Generate sequential alphanumeric identifiers from a template
4
+
5
+ e.g. Template 'AA9' produces 'AA0', 'AA1','AA2,'AA3', 'AA4', 'AA5', 'AA6', 'AA7', 'AA8', 'AA9', 'AB0', 'AB1', ....
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'gen_sid'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install gen_sid
22
+
23
+ ## Usage
24
+
25
+ require 'gen_sid'
26
+ seq = GenSid::SeqID.new('EF27Y')
27
+ # Note that the first 'next_value' call returns the initial value
28
+ seq.next_value # => "EF27Y"
29
+ seq.next_value # => "EF27Z"
30
+ seq.next_value # => "EF28A"
31
+
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/tflynn/gen_sid/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gen_sid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gen_sid"
8
+ spec.version = GenSid::VERSION
9
+ spec.authors = ["Tracy Flynn"]
10
+ spec.email = ["tracy.flynn@verymuchme.com"]
11
+ spec.summary = %q{Generate sequential identifiers}
12
+ spec.description = %q{Generate sequential identifiers using alpha-numeric template}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'rspec', '~> 3.0.0.beta1'
24
+
25
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "gen_sid/version"
2
+ require_relative "gen_sid/seq_id"
3
+ require_relative "gen_sid/counter"
4
+ require_relative "gen_sid/counter_alpha"
5
+ require_relative "gen_sid/counter_num"
@@ -0,0 +1,22 @@
1
+ module GenSid
2
+
3
+
4
+ class Counter
5
+
6
+ attr_reader :initial_value, :current_value
7
+ attr_accessor :skip_first_value
8
+
9
+ def initialize(initial_value)
10
+ @initial_value = initial_value
11
+ @previous_value = nil
12
+ @current_value = nil
13
+ @skip_first_value = false
14
+ end
15
+
16
+ def next_value
17
+ raise "Method next_value must be implemented by sub class"
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,39 @@
1
+ module GenSid
2
+
3
+ class CounterAlpha < GenSid::Counter
4
+
5
+ def initialize(initial_value = 'A')
6
+ super(initial_value)
7
+ end
8
+
9
+ def value
10
+ @current_value.nil? ? @initial_value : @current_value
11
+ end
12
+
13
+ def next_value
14
+ increment
15
+ if @skip_first_value
16
+ increment
17
+ @skip_first_value = false
18
+ end
19
+ next_val = @current_value
20
+ status = @previous_value == 'Z' ? :rollover : :normal
21
+ [next_val,status]
22
+ end
23
+
24
+ def increment
25
+ @previous_value = @current_value
26
+ if @current_value.nil?
27
+ @current_value = @initial_value
28
+ else
29
+ if @current_value == 'Z'
30
+ @current_value = 'A'
31
+ else
32
+ @current_value = (@current_value[0].ord + 1).chr
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,39 @@
1
+ module GenSid
2
+
3
+ class CounterNum < GenSid::Counter
4
+
5
+ def initialize(initial_value = 0)
6
+ super(initial_value)
7
+ end
8
+
9
+ def value
10
+ @current_value.nil? ? @initial_value : @current_value
11
+ end
12
+
13
+ def next_value
14
+ increment
15
+ if @skip_first_value
16
+ increment
17
+ @skip_first_value = false
18
+ end
19
+ next_val = @current_value
20
+ status = @previous_value == 9 ? :rollover : :normal
21
+ [next_val,status]
22
+ end
23
+
24
+ def increment
25
+ @previous_value = @current_value
26
+ if @current_value.nil?
27
+ @current_value = @initial_value
28
+ else
29
+ if @current_value == 9
30
+ @current_value = 0
31
+ else
32
+ @current_value += 1
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,66 @@
1
+ module GenSid
2
+
3
+ class SeqID
4
+
5
+ attr_reader :total, :pattern
6
+
7
+ def initialize(pattern = 'A1')
8
+ @pattern = pattern
9
+ @reversed_counters = parse_pattern(@pattern).reverse
10
+ # Set all but first counter to skip first value on increment
11
+ 1.upto(@reversed_counters.size - 1) do |pos|
12
+ @reversed_counters[pos].skip_first_value = true
13
+ end
14
+ end
15
+
16
+ def next_value
17
+
18
+ # Counters are managed in reverse order - from least-significant (i.e. most rapidly varying)
19
+ # to most-significant (i.e. least rapidly varying)
20
+
21
+ last_processed = 0
22
+ all_next = ""
23
+
24
+ # Stop at first counter that yields a next value without a rollover
25
+ 0.upto(@reversed_counters.size - 1) do |pos|
26
+ last_processed = pos
27
+ counter = @reversed_counters[pos]
28
+ next_value, status = counter.next_value
29
+ all_next << next_value.to_s
30
+ break if (status == :normal)
31
+ end
32
+
33
+ # Deal with remainder - get current values
34
+ (last_processed + 1).upto(@reversed_counters.size - 1) do |pos|
35
+ counter = @reversed_counters[pos]
36
+ all_next << counter.value.to_s
37
+ end
38
+
39
+ # Reverse the result
40
+ all_next.reverse
41
+
42
+ end
43
+
44
+ private # Private from here on
45
+
46
+ # Returns an array of Counters, one per pattern element in the same order - i.e. most significant first
47
+ def parse_pattern(pattern)
48
+ counters = Array.new
49
+ pattern.split("").each do |element|
50
+ if element =~ /\d/
51
+ counters << CounterNum.new(element.to_i)
52
+ elsif element =~ /[[:alpha:]]/
53
+ counters << CounterAlpha.new(element.upcase)
54
+ else
55
+ # Do nothing
56
+ end
57
+ end
58
+
59
+ counters
60
+ end
61
+
62
+
63
+ end
64
+
65
+
66
+ end
@@ -0,0 +1,3 @@
1
+ module GenSid
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,70 @@
1
+ require 'rspec'
2
+ require 'gen_sid'
3
+
4
+ describe 'An alpha counter' do
5
+
6
+ it 'should initialize with an explicit value' do
7
+ counter = GenSid::CounterAlpha.new
8
+ expect(counter.initial_value).to eq('A')
9
+ end
10
+
11
+ it 'should return a next value' do
12
+ counter = GenSid::CounterAlpha.new
13
+ expect(counter.next_value).to be
14
+ end
15
+
16
+ it "should return 'A' as the initial next value without marking it as a rollover" do
17
+ counter = GenSid::CounterAlpha.new
18
+ next_value, status = counter.next_value
19
+ expect(next_value).to eq('A')
20
+ expect(status).to eq(:normal)
21
+ end
22
+
23
+ it "should return 'B' as the second value" do
24
+ counter = GenSid::CounterAlpha.new
25
+ next_value, status = counter.next_value
26
+ expect(next_value).to eq('A')
27
+ expect(status).to eq(:normal)
28
+ next_value, status = counter.next_value
29
+ expect(next_value).to eq('B')
30
+ expect(status).to eq(:normal)
31
+ end
32
+
33
+
34
+ it "should indicate the first rollover when the first range is exceeded" do
35
+ counter = GenSid::CounterAlpha.new
36
+ next_value, status = counter.next_value # initial 'A'
37
+ expect(next_value).to eq('A')
38
+ expect(status).to eq(:normal)
39
+
40
+ # 'B' - 'Y'
41
+ 1.upto(24) do |i|
42
+ counter.next_value
43
+ end
44
+
45
+ next_value, status = counter.next_value # 'Z'
46
+ expect(next_value).to eq('Z')
47
+ expect(status).to eq(:normal)
48
+
49
+ next_value, status = counter.next_value # 'A' with rollover
50
+ expect(next_value).to eq('A')
51
+ expect(status).to eq(:rollover)
52
+
53
+ next_value, status = counter.next_value # 'B'
54
+ expect(next_value).to eq('B')
55
+ expect(status).to eq(:normal)
56
+
57
+ end
58
+
59
+
60
+ it "should initialize to any supplied value and increment properly" do
61
+ counter = GenSid::CounterAlpha.new('Z')
62
+ next_value, status = counter.next_value # initial 'Z'
63
+ expect(next_value).to eq('Z')
64
+ expect(status).to eq(:normal)
65
+
66
+ next_value, status = counter.next_value # 'A' with rollover
67
+ expect(next_value).to eq('A')
68
+ expect(status).to eq(:rollover)
69
+ end
70
+ end
@@ -0,0 +1,59 @@
1
+ require 'rspec'
2
+ require 'gen_sid'
3
+
4
+ describe 'A numeric counter' do
5
+
6
+ it 'should initialize with an explicit value' do
7
+ counter = GenSid::CounterNum.new
8
+ expect(counter.initial_value).to eq(0)
9
+ end
10
+
11
+ it 'should return a next value' do
12
+ counter = GenSid::CounterNum.new
13
+ expect(counter.next_value).to be
14
+ end
15
+
16
+ it "should return 0 as the initial next value without marking it as a rollover" do
17
+ counter = GenSid::CounterNum.new
18
+ next_value, status = counter.next_value
19
+ expect(next_value).to eq(0)
20
+ expect(status).to eq(:normal)
21
+ end
22
+
23
+ it "should indicate the first rollover when the first range is exceeded" do
24
+ counter = GenSid::CounterNum.new
25
+ next_value, status = counter.next_value # initial 0
26
+ expect(next_value).to eq(0)
27
+ expect(status).to eq(:normal)
28
+
29
+ # 1 through 8
30
+ 1.upto(8) do |i|
31
+ counter.next_value
32
+ end
33
+
34
+ next_value, status = counter.next_value # 9
35
+ expect(next_value).to eq(9)
36
+ expect(status).to eq(:normal)
37
+
38
+ next_value, status = counter.next_value # 0 with rollover
39
+ expect(next_value).to eq(0)
40
+ expect(status).to eq(:rollover)
41
+
42
+ next_value, status = counter.next_value # 1
43
+ expect(next_value).to eq(1)
44
+ expect(status).to eq(:normal)
45
+
46
+ end
47
+
48
+ it "should initialize to any supplied value and increment properly" do
49
+ counter = GenSid::CounterNum.new(9)
50
+ next_value, status = counter.next_value # initial 9
51
+ expect(next_value).to eq(9)
52
+ expect(status).to eq(:normal)
53
+
54
+ next_value, status = counter.next_value # 0 with rollover
55
+ expect(next_value).to eq(0)
56
+ expect(status).to eq(:rollover)
57
+ end
58
+
59
+ end
@@ -0,0 +1,12 @@
1
+ require 'rspec'
2
+ require 'gen_sid'
3
+
4
+ describe "Initialize SeqID" do
5
+
6
+ it 'should supply defaults' do
7
+ sid = GenSid::SeqID.new
8
+ expect(sid.pattern).to eq('A1')
9
+ end
10
+
11
+
12
+ end
@@ -0,0 +1,38 @@
1
+ require 'rspec'
2
+ require 'gen_sid'
3
+
4
+ describe 'SeqID' do
5
+
6
+ it 'should handle the default case correctly' do
7
+ seq = GenSid::SeqID.new
8
+ # Default value is 'A1'
9
+ expect(seq.next_value).to eq('A1')
10
+ 7.times {|i| seq.next_value}
11
+ expect(seq.next_value).to eq('A9')
12
+ expect(seq.next_value).to eq('B0')
13
+ 8.times {|i| seq.next_value}
14
+ expect(seq.next_value).to eq('B9')
15
+ expect(seq.next_value).to eq('C0')
16
+ end
17
+
18
+ it "should handle a more complex case properly" do
19
+ seq = GenSid::SeqID.new('CC1B')
20
+ expect(seq.next_value).to eq('CC1B')
21
+ expect(seq.next_value).to eq('CC1C')
22
+ 22.times {|i| seq.next_value}
23
+ expect(seq.next_value).to eq('CC1Z')
24
+ expect(seq.next_value).to eq('CC2A')
25
+ end
26
+
27
+ it "should handle starting in the middle of a complex sequence properly" do
28
+ seq = GenSid::SeqID.new('EF27Y')
29
+ expect(seq.next_value).to eq('EF27Y')
30
+ expect(seq.next_value).to eq('EF27Z')
31
+ expect(seq.next_value).to eq('EF28A')
32
+ seq = GenSid::SeqID.new('EF9ZZ')
33
+ expect(seq.next_value).to eq('EF9ZZ')
34
+ expect(seq.next_value).to eq('EG0AA')
35
+
36
+ end
37
+
38
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gen_sid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Tracy Flynn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0.beta1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0.beta1
55
+ description: Generate sequential identifiers using alpha-numeric template
56
+ email:
57
+ - tracy.flynn@verymuchme.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - gen_sid.gemspec
68
+ - lib/gen_sid.rb
69
+ - lib/gen_sid/counter.rb
70
+ - lib/gen_sid/counter_alpha.rb
71
+ - lib/gen_sid/counter_num.rb
72
+ - lib/gen_sid/seq_id.rb
73
+ - lib/gen_sid/version.rb
74
+ - test/gen_sid/counter_alpha_spec.rb
75
+ - test/gen_sid/counter_num_spec.rb
76
+ - test/gen_sid/gen_sid_spec.rb
77
+ - test/gen_sid/seq_id_spec.rb
78
+ homepage: ''
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Generate sequential identifiers
102
+ test_files:
103
+ - test/gen_sid/counter_alpha_spec.rb
104
+ - test/gen_sid/counter_num_spec.rb
105
+ - test/gen_sid/gen_sid_spec.rb
106
+ - test/gen_sid/seq_id_spec.rb