noid 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,153 @@
1
+ describe Noid::Minter do
2
+ it "should mint a few random 3-digit numbers" do
3
+ minter = Noid::Minter.new(:template => '.rddd')
4
+ minter.mint.should =~ /\d\d\d/
5
+ end
6
+
7
+ it "should mint random 3-digit numbers, stopping after the 1000th" do
8
+ minter = Noid::Minter.new(:template => '.rddd')
9
+ 1000.times { minter.mint.should =~ /^\d\d\d$/ }
10
+ expect { minter.mint }.to raise_exception
11
+ end
12
+
13
+ it "should mint sequential numbers without limit, adding new digits as needed" do
14
+ minter = Noid::Minter.new(:template => '.zd')
15
+ minter.mint.should == "0"
16
+ 999.times { minter.mint.should =~ /\d/ }
17
+ minter.mint.should == "1000"
18
+ end
19
+
20
+ it "should mint random 4-digit numbers with constant prefix bc" do
21
+ minter = Noid::Minter.new(:template => 'bc.rdddd')
22
+ 1000.times { minter.mint.should =~ /^bc\d\d\d\d$/ }
23
+ end
24
+
25
+ it "should mint sequential 2-digit numbers with constant prefix 8rf" do
26
+ minter = Noid::Minter.new(:template => '8rf.sdd')
27
+ minter.mint.should == "8rf00"
28
+ 10.times { minter.mint.should =~ /^8rf\d\d$/ }
29
+ minter.mint.should == "8rf11"
30
+ 88.times { minter.mint.should =~ /^8rf\d\d$/ }
31
+ expect { minter.mint }.to raise_exception
32
+ end
33
+
34
+ it "should mint sequential extended-digits" do
35
+ minter = Noid::Minter.new(:template => '.se')
36
+ 29.times.map { minter.mint }.join('').should == "0123456789bcdfghjkmnpqrstvwxz"
37
+ end
38
+
39
+ it "should mint random 3-extended-digit numbers with constant prefix h9" do
40
+ minter = Noid::Minter.new(:template => 'h9.reee')
41
+
42
+ (minter.template.max).times { minter.mint.should =~ /^h9\w\w\w$/ }
43
+ expect { minter.mint }.to raise_exception
44
+ end
45
+
46
+ it "should mint unlimited sequential numbers with at least 3 extended digits" do
47
+ minter = Noid::Minter.new(:template => '.zeee')
48
+ (29*29*29).times { minter.mint.should =~ /^\w\w\w/ }
49
+ minter.mint.should =~ /^\w\w\w\w/
50
+ end
51
+
52
+ it "should mint random 7-char numbers, with extended digits at chars 2,4,and 5" do
53
+ minter = Noid::Minter.new(:template => '.rdedeedd')
54
+ 1000.times { minter.mint.should =~ /^\d\w\d\w\w\d\d$/ }
55
+ end
56
+
57
+ it "should mint unlimited mixed digits, adding new extended digits as needed" do
58
+ minter = Noid::Minter.new(:template => '.zedededed')
59
+ minter.mint.should == "00000000"
60
+ end
61
+
62
+ it "should mint sequential 4-mixed-digit with constant prefix sdd" do
63
+ minter = Noid::Minter.new(:template => 'sdd.sdede')
64
+ minter.mint.should == 'sdd0000'
65
+ 1000.times { minter.mint.should =~ /^sdd\d\w\d\w$/}
66
+ minter.mint.should == "sdd034h"
67
+ end
68
+
69
+ it "should mint random 3 mixed digits plus final (4th) computed check character" do
70
+ minter = Noid::Minter.new(:template => '.rdedk')
71
+ 1000.times { minter.mint.should =~ /^\d\w\d\w$/ }
72
+ end
73
+
74
+ it "should mint 5 sequential mixed digits plus final extended digit check char" do
75
+ minter = Noid::Minter.new(:template => '.sdeeedk')
76
+ minter.mint.should == "000000"
77
+ minter.mint.should == "000015"
78
+ minter.mint.should == "00002b"
79
+ 1000.times { minter.mint.should =~ /^\d\w\w\w\d\w$/ }
80
+ minter.mint.should == "003f3m"
81
+ end
82
+
83
+ it "should mint sequential digits plus check char, with new digits added as needed" do
84
+ minter = Noid::Minter.new(:template => ".zdeek")
85
+ minter.mint.should == "0000"
86
+ minter.mint.should == "0013"
87
+ (10*29*29-2).times { minter.mint.should =~ /^\d\w\w\w$/ }
88
+ minter.mint.should == "10001"
89
+ end
90
+
91
+ it "should mint prefix plus random 3 mixed digits plus a check char" do
92
+ minter = Noid::Minter.new(:template => "63q.redek")
93
+ minter.mint.should =~ /63q\w\d\w\w/
94
+ end
95
+
96
+ describe "seed" do
97
+ it "given a specific seed, identifiers should be replicable" do
98
+ minter = Noid::Minter.new(:template => "63q.redek")
99
+ minter.seed(0)
100
+ minter.mint.should == "63qk208"
101
+
102
+ minter = Noid::Minter.new(:template => "63q.redek")
103
+ minter.seed(0)
104
+ minter.mint.should == "63qk208"
105
+ end
106
+
107
+ it "given a specific seed and sequence, identifiers should be replicable" do
108
+ minter = Noid::Minter.new(:template => "63q.redek")
109
+ minter.seed(23456789, 567)
110
+ minter.mint.should == "63qh305"
111
+
112
+ minter = Noid::Minter.new(:template => "63q.redek")
113
+ minter.seed(23456789, 567)
114
+ minter.mint.should == "63qh305"
115
+ end
116
+ end
117
+
118
+ describe "dump" do
119
+ it "should dump the minter state" do
120
+ minter = Noid::Minter.new(:template => ".sddd")
121
+ d = minter.dump
122
+ d[:template].should == ".sddd"
123
+ d[:seq].should == 0
124
+
125
+ minter.mint
126
+ minter.mint
127
+ d = minter.dump
128
+ d[:seq] == 2
129
+ end
130
+
131
+ it "should dump the seed, sequence, and counters for the RNG" do
132
+ minter = Noid::Minter.new(:template => ".rddd")
133
+ d = minter.dump
134
+ d[:seq] == 0
135
+ d[:seed].should == minter.instance_variable_get('@seed')
136
+ end
137
+
138
+ it "should allow a random identifier minter to be 'replayed' accurately" do
139
+ minter = Noid::Minter.new(:template => '.rd')
140
+ d = minter.dump
141
+ arr = 10.times.map { minter.mint }
142
+
143
+ minter = Noid::Minter.new(d)
144
+
145
+ arr2 = 10.times.map { minter.mint }
146
+
147
+ arr.should == arr2
148
+
149
+ end
150
+
151
+ end
152
+
153
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'noid'
4
+
5
+ RSpec.configure do |config|
6
+
7
+ end
metadata CHANGED
@@ -1,194 +1,106 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: noid
3
- version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 4
9
- - 0
10
- version: 0.4.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Chris Beer
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-12-23 00:00:00 -05:00
12
+ date: 2011-07-13 00:00:00.000000000 -04:00
19
13
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- type: :runtime
23
- prerelease: false
24
- name: anvl
25
- version_requirements: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- hash: 3
31
- segments:
32
- - 0
33
- version: "0"
34
- requirement: *id001
35
- - !ruby/object:Gem::Dependency
36
- type: :runtime
37
- prerelease: false
38
- name: json
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- version: "0"
48
- requirement: *id002
49
- - !ruby/object:Gem::Dependency
50
- type: :runtime
51
- prerelease: false
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
52
16
  name: backports
53
- version_requirements: &id003 !ruby/object:Gem::Requirement
17
+ requirement: &2153449260 !ruby/object:Gem::Requirement
54
18
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
62
- requirement: *id003
63
- - !ruby/object:Gem::Dependency
64
- type: :development
65
- prerelease: false
66
- name: shoulda
67
- version_requirements: &id004 !ruby/object:Gem::Requirement
68
- none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
74
- - 0
75
- version: "0"
76
- requirement: *id004
77
- - !ruby/object:Gem::Dependency
78
- type: :development
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
79
24
  prerelease: false
25
+ version_requirements: *2153449260
26
+ - !ruby/object:Gem::Dependency
80
27
  name: bundler
81
- version_requirements: &id005 !ruby/object:Gem::Requirement
28
+ requirement: &2153448740 !ruby/object:Gem::Requirement
82
29
  none: false
83
- requirements:
30
+ requirements:
84
31
  - - ~>
85
- - !ruby/object:Gem::Version
86
- hash: 23
87
- segments:
88
- - 1
89
- - 0
90
- - 0
32
+ - !ruby/object:Gem::Version
91
33
  version: 1.0.0
92
- requirement: *id005
93
- - !ruby/object:Gem::Dependency
94
34
  type: :development
95
35
  prerelease: false
96
- name: jeweler
97
- version_requirements: &id006 !ruby/object:Gem::Requirement
36
+ version_requirements: *2153448740
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &2153448240 !ruby/object:Gem::Requirement
98
40
  none: false
99
- requirements:
100
- - - ~>
101
- - !ruby/object:Gem::Version
102
- hash: 1
103
- segments:
104
- - 1
105
- - 5
106
- - 1
107
- version: 1.5.1
108
- requirement: *id006
109
- - !ruby/object:Gem::Dependency
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '2.0'
110
45
  type: :development
111
46
  prerelease: false
47
+ version_requirements: *2153448240
48
+ - !ruby/object:Gem::Dependency
112
49
  name: rcov
113
- version_requirements: &id007 !ruby/object:Gem::Requirement
50
+ requirement: &2153447780 !ruby/object:Gem::Requirement
114
51
  none: false
115
- requirements:
116
- - - ">="
117
- - !ruby/object:Gem::Version
118
- hash: 3
119
- segments:
120
- - 0
121
- version: "0"
122
- requirement: *id007
123
- description: ""
124
- email: chris@cbeer.info
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *2153447780
59
+ description: ''
60
+ email:
61
+ - chris@cbeer.info
125
62
  executables: []
126
-
127
63
  extensions: []
128
-
129
- extra_rdoc_files:
130
- - LICENSE.txt
131
- - README.rdoc
132
- files:
133
- - .document
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
134
67
  - Gemfile
135
68
  - LICENSE.txt
136
69
  - README.rdoc
137
70
  - Rakefile
138
71
  - VERSION
139
- - doc/active_record_sample.rb
140
72
  - lib/noid.rb
141
- - lib/noid/active_record.rb
142
- - lib/noid/active_record/provider.rb
143
- - lib/noid/base.rb
144
- - lib/noid/identifier.rb
145
- - lib/noid/identifier/anvl.rb
146
- - lib/noid/identifier/singleton.rb
147
73
  - lib/noid/minter.rb
148
- - lib/noid/persistence.rb
149
- - lib/noid/persistence/base.rb
150
- - lib/noid/persistence/json.rb
74
+ - lib/noid/template.rb
75
+ - lib/noid/version.rb
151
76
  - noid.gemspec
152
- - test/helper.rb
153
- - test/test_binding.rb
154
- - test/test_noid.rb
155
- - test/test_persistence.rb
77
+ - spec/lib/minter_spec.rb
78
+ - spec/spec_helper.rb
156
79
  has_rdoc: true
157
- homepage: http://github.com/cbeer/noid
158
- licenses:
159
- - MIT
80
+ homepage: http://github.com/microservices/noid
81
+ licenses: []
160
82
  post_install_message:
161
83
  rdoc_options: []
162
-
163
- require_paths:
84
+ require_paths:
164
85
  - lib
165
- required_ruby_version: !ruby/object:Gem::Requirement
86
+ required_ruby_version: !ruby/object:Gem::Requirement
166
87
  none: false
167
- requirements:
168
- - - ">="
169
- - !ruby/object:Gem::Version
170
- hash: 3
171
- segments:
172
- - 0
173
- version: "0"
174
- required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
93
  none: false
176
- requirements:
177
- - - ">="
178
- - !ruby/object:Gem::Version
179
- hash: 3
180
- segments:
181
- - 0
182
- version: "0"
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
183
98
  requirements: []
184
-
185
- rubyforge_project:
186
- rubygems_version: 1.3.7
99
+ rubyforge_project: noid
100
+ rubygems_version: 1.6.2
187
101
  signing_key:
188
102
  specification_version: 3
189
103
  summary: Nice Opaque Identifier
190
- test_files:
191
- - test/helper.rb
192
- - test/test_binding.rb
193
- - test/test_noid.rb
194
- - test/test_persistence.rb
104
+ test_files:
105
+ - spec/lib/minter_spec.rb
106
+ - spec/spec_helper.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
@@ -1,6 +0,0 @@
1
- class Noidtest < ActiveRecord::Base
2
- include Noid::ActiveRecord::Provider
3
-
4
- identifier_field :identifier
5
- minter Noid::Persistence::JSON.new(:template => 's.sdd', :filename => persistence_path)
6
- end
@@ -1,5 +0,0 @@
1
- module Noid
2
- module ActiveRecord
3
-
4
- end
5
- end
@@ -1,36 +0,0 @@
1
- module Noid::ActiveRecord
2
- module Provider
3
- def self.included base
4
- base.send :before_create, :mint_identifier
5
- # base.send :attr_accessor, :minter
6
- class << base
7
- def minter value = nil
8
- @minter = value if value
9
- @minter
10
- end
11
-
12
- def identifier_field value = nil
13
- @identifier_field = value if value
14
- @identifier_field
15
- end
16
-
17
- def persistence_path
18
- File.join RAILS_ROOT, "db/#{self.to_s}_NOID.js"
19
- end
20
- end
21
- end
22
-
23
- def minter= value
24
- @minter = value
25
- end
26
-
27
- def minter
28
- @minter || self.class.minter
29
- end
30
-
31
- def mint_identifier
32
- self.send "#{self.class.instance_variable_get('@identifier_field')}=", self.minter.mint unless self.attribute_present? self.class.instance_variable_get('@identifier_field')
33
- end
34
-
35
- end
36
- end