protokoll 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,213 +5,343 @@ class ProtokollTest < ActiveSupport::TestCase
5
5
  def setup
6
6
  time = Time.local(2011, 9, 25, 12, 0, 0)
7
7
  Timecop.travel(time)
8
-
9
- Protocol.protokoll.count = 0
10
8
  end
11
9
 
10
+ def teardown
11
+ reset_models
12
+ end
13
+
14
+ def reset_models
15
+ ProtokollTest.send(:remove_const, 'Protocol') if defined? Protocol
16
+ ProtokollTest.send(:remove_const, 'Call') if defined? Call
17
+ end
18
+
19
+ ##
20
+ # Tests
21
+ ##
22
+
12
23
  test "using new number_symbol with default pattern should get 20110900001" do
13
- Protocol.protokoll.number_symbol = "$"
14
- Protocol.protokoll.pattern = "%Y%m$$$$$"
24
+ class Protocol < ActiveRecord::Base
25
+ protokoll :number, :pattern => "%Y%m$$$$$", :number_symbol => "$"
26
+ end
27
+
28
+ protocol = Protocol.create
29
+ assert_equal "20110900001", protocol.number
30
+ end
31
+
32
+ test "should get 20110900001 (format number %Y%m#####)" do
33
+ class Protocol < ActiveRecord::Base
34
+ protokoll :number
35
+ end
36
+
37
+ protocol = Protocol.create
38
+ assert_equal "20110900001", protocol.number
39
+ end
40
+
41
+ test "should get 20110900002 for second save (format number %Y%m#####)" do
42
+ class Protocol < ActiveRecord::Base
43
+ protokoll :number
44
+ end
15
45
 
16
46
  protocol1 = Protocol.create
17
- assert_equal "20110900001", protocol1.number
47
+ protocol2 = Protocol.create
48
+
49
+ assert_equal "20110900002", protocol2.number
18
50
  end
19
51
 
20
- test "first using # should get 1" do
21
- Protocol.protokoll.pattern = "#"
22
-
52
+ test "should get 201100002 for second save (format number %Y#####)" do
53
+ class Protocol < ActiveRecord::Base
54
+ protokoll :number, :pattern => "%Y#####"
55
+ end
56
+
23
57
  protocol = Protocol.create
24
- assert_equal "1", protocol.number
58
+
59
+ assert_equal "201100001", protocol.number
25
60
  end
26
61
 
27
- test "second protocol on DB should have number should equals 2" do
28
- Protocol.protokoll.pattern = "#"
62
+ test "second protocol on DB should have number should equals 1" do
63
+ class Protocol < ActiveRecord::Base
64
+ protokoll :number, :pattern => "#"
65
+ end
29
66
 
30
- Protocol.create
31
67
  protocol = Protocol.create
32
- assert_equal "2", protocol.number
68
+ assert_equal "1", protocol.number
33
69
  end
34
-
70
+
35
71
  test "third protocol on DB should have number should equals 3" do
36
- Protocol.protokoll.pattern = "#"
37
-
38
- protocol1 = Protocol.create
39
- protocol2 = Protocol.create
40
- protocol3 = Protocol.create
72
+ class Protocol < ActiveRecord::Base
73
+ protokoll :number, :pattern => "#"
74
+ end
75
+
76
+ Protocol.new.save
77
+ Protocol.new.save
78
+ protocol3 = Protocol.new
79
+ protocol3.save
80
+
41
81
  assert_equal "3", protocol3.number
42
82
  end
43
-
83
+
44
84
  test "first using format A# should get A1" do
45
- Protocol.protokoll.pattern = "A#"
46
-
47
- protocol1 = Protocol.create
85
+ class Protocol < ActiveRecord::Base
86
+ protokoll :number, :pattern => "A#"
87
+ end
88
+
89
+ protocol1 = Protocol.new
90
+ protocol1.save
91
+
48
92
  assert_equal "A1", protocol1.number
49
93
  end
50
94
 
51
- test "second using format A# should get A2" do
52
- Protocol.protokoll.pattern = "A#"
53
-
54
- protocol1 = Protocol.create
55
- protocol2 = Protocol.create
56
- assert_equal "A2", protocol2.number
57
- end
58
-
59
95
  test "first using format A## should get A01" do
60
- Protocol.protokoll.pattern = "A##"
61
-
96
+ class Protocol < ActiveRecord::Base
97
+ protokoll :number, :pattern => "A##"
98
+ end
99
+
62
100
  protocol1 = Protocol.create
63
101
  assert_equal "A01", protocol1.number
64
102
  end
65
-
103
+
66
104
  test "second using format A## should get A02" do
67
- Protocol.protokoll.pattern = "A##"
68
-
105
+ class Protocol < ActiveRecord::Base
106
+ protokoll :number, :pattern => "A##"
107
+ end
108
+
69
109
  protocol1 = Protocol.create
70
110
  protocol2 = Protocol.create
71
111
  assert_equal "A02", protocol2.number
72
112
  end
73
-
113
+
74
114
  test "third using format A## should get A03" do
75
- Protocol.protokoll.pattern = "A##"
76
-
115
+ class Protocol < ActiveRecord::Base
116
+ protokoll :number, :pattern => "A##"
117
+ end
118
+
77
119
  protocol1 = Protocol.create
78
120
  protocol2 = Protocol.create
79
121
  protocol3 = Protocol.create
80
122
  assert_equal "A03", protocol3.number
81
123
  end
82
-
124
+
83
125
  test "first use of %y# should get 111" do
84
- Protocol.protokoll.pattern = "%y#"
85
-
126
+ class Protocol < ActiveRecord::Base
127
+ protokoll :number, :pattern => "%y#"
128
+ end
129
+
86
130
  protocol1 = Protocol.create
87
131
  assert_equal "111", protocol1.number
88
132
  end
89
-
90
- test "second use of %y## should get 1101" do
91
- Protocol.protokoll.pattern = "%y##"
92
-
93
- protocol1 = Protocol.create
94
- assert_equal "1101", protocol1.number
95
- end
96
-
133
+
134
+ #
97
135
  test "first use of %y%m## should get 110901" do
98
- Protocol.protokoll.pattern = "%y%m##"
99
-
136
+ class Protocol < ActiveRecord::Base
137
+ protokoll :number, :pattern => "%y%m##"
138
+ end
139
+
100
140
  protocol1 = Protocol.create
101
141
  assert_equal "110901", protocol1.number
102
142
  end
103
-
104
- test "second use of %y%m## on next month after should get 111001" do
105
- Protocol.protokoll.pattern = "%y%m##"
106
-
107
- protocol1 = Protocol.create
143
+ #
144
+ test "using %y%m## on next month after should get 111001" do
145
+ class Protocol < ActiveRecord::Base
146
+ protokoll :number, :pattern => "%y%m##"
147
+ end
148
+
149
+ time = Time.local(2011, 9, 25, 12, 0, 0)
150
+ Timecop.travel(time)
151
+
152
+ Protocol.create
153
+ Protocol.create
154
+
108
155
  Timecop.travel(Time.now + 1.month)
109
- protocol2 = Protocol.create
110
-
111
- assert_equal "111001", protocol2.number
112
- end
113
-
114
- test "%y%m%H#### should get 1109120001" do
115
- Protocol.protokoll.pattern = "%y%m%H####"
116
-
117
- protocol1 = Protocol.create
118
-
119
- assert_equal "1109120001", protocol1.number
156
+
157
+ protocol = Protocol.create
158
+ assert_equal "111001", protocol.number
120
159
  end
121
-
122
- test "%y## on next year should get 1201" do
123
- Protocol.protokoll.pattern = "%y##"
124
-
125
- time = Time.local(2011, 9, 25, 12, 3, 0) #
160
+
161
+ test "%y%m%H#### should get 1109120001" do
162
+ class Protocol < ActiveRecord::Base
163
+ protokoll :number, :pattern => "%y%m%H####"
164
+ end
165
+
166
+ protocol1 = Protocol.create
167
+
168
+ assert_equal "1109120001", protocol1.number
169
+ end
170
+
171
+ test "%y## on next year should get 1201" do
172
+ class Protocol < ActiveRecord::Base
173
+ protokoll :number, :pattern => "%y##"
174
+ end
175
+
176
+ time = Time.local(2011, 9, 25, 12, 3, 0) # 2011
126
177
  protocol1 = Protocol.create
127
- time = Time.local(2012, 9, 25, 12, 3, 0) # exactly 1 year after
178
+ time = Time.local(2012, 9, 25, 12, 3, 0) # 2012 - exactly 1 year after
128
179
  Timecop.travel(time)
129
-
180
+
130
181
  protocol2 = Protocol.create
131
-
182
+
132
183
  assert_equal "1201", protocol2.number
133
- end
134
-
184
+ end
185
+
135
186
  test "500.time create using %y%m%H#### should get 1109120500" do
136
- Protocol.protokoll.pattern = "%y%m%H####"
137
-
187
+ class Protocol < ActiveRecord::Base
188
+ protokoll :number, :pattern => "%y%m%H####"
189
+ end
190
+
138
191
  500.times { Protocol.create }
139
-
192
+
140
193
  assert_equal "1109120500", Protocol.last.number
141
194
  end
142
-
195
+
143
196
  test "PROT%H%m%y#### should get PROT%H%m%y0001" do
144
- Protocol.protokoll.pattern = "PROT%H%m%y####"
145
-
197
+ class Protocol < ActiveRecord::Base
198
+ protokoll :number, :pattern => "PROT%H%m%y####"
199
+ end
200
+
146
201
  protocol1 = Protocol.create
147
-
202
+
148
203
  assert_equal "PROT1209110001", protocol1.number
149
204
  end
150
-
205
+
151
206
  test "PROT%Y%m%H#### should get PROT201109120001" do
152
- Protocol.protokoll.pattern = "PROT%Y%m%H####"
153
-
207
+ class Protocol < ActiveRecord::Base
208
+ protokoll :number, :pattern => "PROT%Y%m%H####"
209
+ end
210
+
154
211
  protocol1 = Protocol.create
155
-
212
+
156
213
  assert_equal "PROT201109120001", protocol1.number
157
214
  end
158
-
215
+
159
216
  test "use of sufix ####PROTO should get 0001PROTO" do
160
- Protocol.protokoll.pattern = "####PROTO"
161
-
217
+ class Protocol < ActiveRecord::Base
218
+ protokoll :number, :pattern => "####PROTO"
219
+ end
220
+
162
221
  protocol1 = Protocol.create
163
-
222
+
164
223
  assert_equal "0001PROTO", protocol1.number
165
224
  end
166
-
225
+
167
226
  test "use of sufix %Y%M####PROTO on 12:03 should get 2011030001PROTO" do
168
- Protocol.protokoll.pattern = "%Y%M####PROTO"
169
-
227
+ class Protocol < ActiveRecord::Base
228
+ protokoll :number, :pattern => "%Y%M####PROTO"
229
+ end
230
+
170
231
  time = Time.local(2011, 9, 25, 12, 3, 0)
171
232
  Timecop.travel(time)
172
-
233
+
173
234
  protocol1 = Protocol.create
174
-
235
+
175
236
  assert_equal "2011030001PROTO", protocol1.number
176
237
  end
177
-
238
+
178
239
  test "use of sufix %Y%M####PROTO on 12:15 should get 2011150001PROTO" do
179
- Protocol.protokoll.pattern = "%Y%M####PROTO"
180
-
240
+ class Protocol < ActiveRecord::Base
241
+ protokoll :number, :pattern => "%Y%M####PROTO"
242
+ end
243
+
181
244
  time = Time.local(2011, 9, 25, 12, 15, 0)
182
245
  Timecop.travel(time)
183
-
246
+
184
247
  protocol1 = Protocol.create
185
-
248
+
186
249
  assert_equal "2011150001PROTO", protocol1.number
187
250
  end
188
251
 
189
252
  test "using 2 models" do
190
- Protocol.protokoll.pattern = "%Y##"
191
- Call.protokoll.pattern = "%Y##"
253
+ class Protocol < ActiveRecord::Base
254
+ protokoll :number, :pattern => "%Y##"
255
+ end
192
256
 
257
+ class Call < ActiveRecord::Base
258
+ protokoll :number, :pattern => "%Y##"
259
+ end
260
+
193
261
  protocol = Protocol.create
194
262
  call = Call.create
195
263
  assert_equal "201101", protocol.number
196
264
  assert_equal "201101", call.number
197
265
  end
198
266
 
199
- ## Can't reproduce this on test environmet!
200
- ## if you know how to do that, contact-me!
201
- # test "Start application with populated db." do
202
- # Protocol.protokoll.pattern = "%Y%M####PROTO"
203
- #
204
- # Protocol.create
205
- # Protocol.create
206
- # Protocol.create
207
- #
208
- # # restart application or reload Protocol model
209
- # # how do I do that?
210
- #
211
- # protocol = Protocol.create
212
- #
213
- # assert_equal "2011090004PROTO", protocol.number
214
- # end
267
+ test "using 2 models with different patterns" do
268
+ class Protocol < ActiveRecord::Base
269
+ protokoll :number, :pattern => "%YA#"
270
+ end
271
+
272
+ class Call < ActiveRecord::Base
273
+ protokoll :number, :pattern => "%YB#"
274
+ end
275
+
276
+ protocol = Protocol.create
277
+ call = Call.create
278
+ assert_equal "2011A1", protocol.number
279
+ assert_equal "2011B1", call.number
280
+ end
281
+
282
+ test "updating model should not get a different number" do
283
+ class Protocol < ActiveRecord::Base
284
+ protokoll :number, :pattern => "%Y##"
285
+ end
286
+
287
+ protocol = Protocol.new
288
+
289
+ protocol.save
290
+ assert_equal "201101", protocol.number
291
+
292
+ protocol.save
293
+ assert_equal "201101", protocol.number
294
+ end
295
+
296
+ test "reserve_number should set number to instance" do
297
+ class Protocol < ActiveRecord::Base
298
+ protokoll :number, :pattern => "%Y##"
299
+ end
300
+
301
+ protocol = Protocol.new
302
+ protocol.reserve_number!
303
+
304
+ assert_equal "201101", protocol.number
305
+ protocol.save
306
+ assert_equal "201101", protocol.number
307
+
308
+ protocol = Protocol.new
309
+ protocol.save
310
+ assert_equal "201102", protocol.number
311
+ end
312
+
313
+ test "reserve_number should assure number if reserved" do
314
+ class Protocol < ActiveRecord::Base
315
+ protokoll :number, :pattern => "%Y##"
316
+ end
317
+
318
+ protocol1 = Protocol.new
319
+ protocol1.reserve_number!
320
+
321
+ protocol2 = Protocol.new
322
+ protocol2.save
323
+
324
+ assert_equal "201101", protocol1.number
325
+ assert_equal "201102", protocol2.number
326
+ end
327
+
328
+ test "reserve_number should assure number if reserved if next month" do
329
+ class Protocol < ActiveRecord::Base
330
+ protokoll :number, :pattern => "%Y%m##"
331
+ end
332
+
333
+ protocol1 = Protocol.new
334
+ protocol1.reserve_number!
335
+
336
+ Timecop.travel(Time.now + 1.month)
337
+
338
+ protocol2 = Protocol.new
339
+ protocol2.save!
340
+
341
+ assert_equal "20110901", protocol1.number
342
+ assert_equal "20111001", protocol2.number
343
+ end
344
+
215
345
  end
216
346
 
217
347
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protokoll
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-12 00:00:00.000000000Z
12
+ date: 2012-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70244469257640 !ruby/object:Gem::Requirement
16
+ requirement: &70246610625400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 3.1.0
21
+ version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70244469257640
24
+ version_requirements: *70246610625400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &70244469257140 !ruby/object:Gem::Requirement
27
+ requirement: &70246610622920 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70244469257140
35
+ version_requirements: *70246610622920
36
36
  description: Rails 3 gem to enable creation of a custom autoincrement Time based string
37
37
  on a model defined by a pattern. ex. 20110001, 20110002, 20110003, 20120001, 20120002...
38
38
  email:
@@ -41,9 +41,12 @@ executables: []
41
41
  extensions: []
42
42
  extra_rdoc_files: []
43
43
  files:
44
- - lib/protokoll/auto_increment.rb
45
- - lib/protokoll/class_variable.rb
46
- - lib/protokoll/extract_number.rb
44
+ - lib/generators/protokoll/migration/migration_generator.rb
45
+ - lib/generators/protokoll/migration/templates/create_custom_auto_increments.rb
46
+ - lib/protokoll/counter.rb
47
+ - lib/protokoll/formater.rb
48
+ - lib/protokoll/models/custom_auto_increment.rb
49
+ - lib/protokoll/protokoll.rb
47
50
  - lib/protokoll/version.rb
48
51
  - lib/protokoll.rb
49
52
  - lib/tasks/protokoll_tasks.rake
@@ -54,9 +57,8 @@ files:
54
57
  - test/dummy/app/assets/stylesheets/application.css
55
58
  - test/dummy/app/controllers/application_controller.rb
56
59
  - test/dummy/app/helpers/application_helper.rb
57
- - test/dummy/app/models/call.rb
58
- - test/dummy/app/models/principal.rb
59
- - test/dummy/app/models/protocol.rb
60
+ - test/dummy/app/models/call_defined_in_tests.rb
61
+ - test/dummy/app/models/protocol_defined_in_tests.rb
60
62
  - test/dummy/app/views/layouts/application.html.erb
61
63
  - test/dummy/config/application.rb
62
64
  - test/dummy/config/boot.rb
@@ -76,8 +78,8 @@ files:
76
78
  - test/dummy/config.ru
77
79
  - test/dummy/db/development.sqlite3
78
80
  - test/dummy/db/migrate/20110923024431_create_protocols.rb
79
- - test/dummy/db/migrate/20110923101536_create_principals.rb
80
81
  - test/dummy/db/migrate/20110928013630_create_calls.rb
82
+ - test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb
81
83
  - test/dummy/db/schema.rb
82
84
  - test/dummy/db/seeds.rb
83
85
  - test/dummy/db/test.sqlite3
@@ -111,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
113
  version: '0'
112
114
  requirements: []
113
115
  rubyforge_project:
114
- rubygems_version: 1.8.10
116
+ rubygems_version: 1.8.15
115
117
  signing_key:
116
118
  specification_version: 3
117
119
  summary: A simple Rails gem to create custom autoincrement Time base values to a database
@@ -121,9 +123,8 @@ test_files:
121
123
  - test/dummy/app/assets/stylesheets/application.css
122
124
  - test/dummy/app/controllers/application_controller.rb
123
125
  - test/dummy/app/helpers/application_helper.rb
124
- - test/dummy/app/models/call.rb
125
- - test/dummy/app/models/principal.rb
126
- - test/dummy/app/models/protocol.rb
126
+ - test/dummy/app/models/call_defined_in_tests.rb
127
+ - test/dummy/app/models/protocol_defined_in_tests.rb
127
128
  - test/dummy/app/views/layouts/application.html.erb
128
129
  - test/dummy/config/application.rb
129
130
  - test/dummy/config/boot.rb
@@ -143,8 +144,8 @@ test_files:
143
144
  - test/dummy/config.ru
144
145
  - test/dummy/db/development.sqlite3
145
146
  - test/dummy/db/migrate/20110923024431_create_protocols.rb
146
- - test/dummy/db/migrate/20110923101536_create_principals.rb
147
147
  - test/dummy/db/migrate/20110928013630_create_calls.rb
148
+ - test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb
148
149
  - test/dummy/db/schema.rb
149
150
  - test/dummy/db/seeds.rb
150
151
  - test/dummy/db/test.sqlite3
@@ -1,95 +0,0 @@
1
- module Protokoll
2
-
3
- class AutoIncrement
4
- attr_accessor :options
5
- attr_accessor :count
6
-
7
- def initialize
8
- @count = 0
9
- end
10
-
11
- def pattern=(p)
12
- options[:pattern] = p
13
- end
14
-
15
- def pattern
16
- options[:pattern]
17
- end
18
-
19
- def number_symbol=(s)
20
- options[:number_symbol] = s
21
- end
22
-
23
- def number_symbol
24
- options[:number_symbol]
25
- end
26
-
27
- def next_custom_number(column, number)
28
- prefix(options[:pattern]).to_s +
29
- counter(options[:pattern], number).to_s +
30
- sufix(options[:pattern]).to_s
31
- end
32
-
33
- def prefix(pattern)
34
- prefx = extract_prefix(pattern)
35
- expand_times(prefx.to_s)
36
- end
37
-
38
- def counter(pattern, n)
39
- format_counter(digits_size(pattern), n)
40
- end
41
-
42
- def sufix(pattern)
43
- sufx = extract_sufix(pattern)
44
- expand_times(sufx.to_s)
45
- end
46
-
47
- def format_counter(zeros, value)
48
- "%0#{zeros}d" % value
49
- end
50
-
51
- def extract_prefix(pattern)
52
- # Company#### => Company
53
- symbol = options[:number_symbol]
54
- (pattern =~ /^(\s|\d)*[^#{symbol}]+/ and $&)
55
- end
56
-
57
- def extract_sufix(pattern)
58
- # ###Company => Company
59
- symbol = options[:number_symbol]
60
- (pattern =~ /[^#{symbol}]+$/ and $&)
61
- end
62
-
63
- def expand_times(pattern)
64
- pattern.sub!("%y", Time.now.strftime("%y"))
65
- pattern.sub!("%Y", Time.now.strftime("%Y"))
66
- pattern.sub!("%d", Time.now.strftime("%d"))
67
- pattern.sub!("%m", Time.now.strftime("%m"))
68
- pattern.sub!("%M", Time.now.strftime("%M"))
69
- pattern.sub("%H", Time.now.strftime("%H"))
70
- end
71
-
72
- def digits_size(pattern)
73
- symbol = options[:number_symbol]
74
- (pattern =~ /[#{symbol}]+/ and $&).length
75
- end
76
-
77
- def outdated?(record)
78
- Time.now.strftime(update_event).to_i > record.created_at.strftime(update_event).to_i
79
- end
80
-
81
- # BUG 1 : If using %y%m# and only year changes (user stays
82
- # more then one year without updating the model) counter doesn't updates!
83
- def update_event
84
- pattern = options[:pattern]
85
- event = String.new
86
-
87
- event += "%Y" if pattern.include? "%y" or pattern.include? "%Y"
88
- event += "%m" if pattern.include? "%m"
89
- event += "%H" if pattern.include? "%H"
90
- event += "%M" if pattern.include? "%M"
91
- event
92
- end
93
-
94
- end
95
- end
@@ -1,13 +0,0 @@
1
- module Protokoll
2
- module ClassVariable
3
- def self.add_to(klass, instance, options = nil)
4
- if not klass.class_variables.include? instance
5
- klass.class_variable_set instance, options[:default]
6
- end
7
- end
8
-
9
- def self.get_from(klass, instance)
10
- klass.class_variable_get(instance)
11
- end
12
- end
13
- end