nummernkreis 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nummernkreis/version.rb +3 -0
- data/lib/nummernkreis.rb +112 -0
- data/nummernkreis.gemspec +43 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3a662d8dc677f9cf7f635ee3319ae1fb250f17945024bc714acce3e0a89c7b8d
|
4
|
+
data.tar.gz: 6abd42f788629f3d5b395dc49ee162dc1d45880de500502bffea0cd3a5c7a7fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd32be40f56ce8428833c7cb05ae5b548b3be0acdbdf9997420adbdaf4125c13addcebed3ac05a8f6158e8ec5ab248585e2dfba03f16c47be2f07b4133694508
|
7
|
+
data.tar.gz: eeb9242b94ed7fecc4d1f378440e58ec27a4aa1df8803303895d9cfec2a4aa32443e02cbaedec1fe7dd032978d658c3a2c66a85446f3d825bcd380378d1efd8c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nummernkreis (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.11.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
timecop (0.9.1)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.17)
|
18
|
+
minitest (~> 5.0)
|
19
|
+
nummernkreis!
|
20
|
+
rake (~> 10.0)
|
21
|
+
timecop (~> 0.9.0)
|
22
|
+
|
23
|
+
BUNDLED WITH
|
24
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Andreas Böhrnsen
|
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,64 @@
|
|
1
|
+
# Nummernkreis
|
2
|
+
|
3
|
+
This gem implements a number range ("Nummernkreis" in German) which is very common for German documents like invoices where the invoice number is not just a number sequence but also includes date components.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'nummernkreis'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nummernkreis
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
When generating a new number range you need to specify its pattern.
|
24
|
+
The following components are possible:
|
25
|
+
|
26
|
+
```
|
27
|
+
yyyy: year with full 4 digits (e.g. 1980 or 2019)
|
28
|
+
yy: year with two digits (e.g. 19 for 2019)
|
29
|
+
mm: month starting with 1 (e.g. 02 for February)
|
30
|
+
dd: day of the month starting with 1 (e.g. 01 for first day)
|
31
|
+
- : dash separator
|
32
|
+
# : part of the sequence that will be incremented.
|
33
|
+
```
|
34
|
+
|
35
|
+
Examples (assuming current date is 2019-04-15):
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
Nummernkreis.new('####').to_s
|
39
|
+
# '0001'
|
40
|
+
|
41
|
+
Nummernkreis.new('yymmdd-##').to_s
|
42
|
+
# '190425-01'
|
43
|
+
|
44
|
+
Nummernkreis.new('yymmdd-##').next
|
45
|
+
# '190425-02'
|
46
|
+
|
47
|
+
Nummernkreis.new('yymmdd-##').parse('181231-04').next
|
48
|
+
# '181231-05'
|
49
|
+
```
|
50
|
+
|
51
|
+
## Development
|
52
|
+
|
53
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
54
|
+
|
55
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/deepflame/nummernkreis.
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
64
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "number_range"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/nummernkreis.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require "nummernkreis/version"
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class Nummernkreis
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
def initialize pattern
|
8
|
+
@pattern = pattern
|
9
|
+
@number = PatternParser.new(pattern).sample
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
@number
|
14
|
+
end
|
15
|
+
|
16
|
+
def next
|
17
|
+
digit_count = @pattern.split('').count('#')
|
18
|
+
raise Error.new('only works if pattern has a digit part') if digit_count == 0
|
19
|
+
|
20
|
+
digits_start_at = @pattern.index('#')
|
21
|
+
digits_end_at = @pattern.rindex('#')
|
22
|
+
|
23
|
+
before_digits = digits_start_at < 1 ? '' : @number[0..digits_start_at-1]
|
24
|
+
digits = @number[digits_start_at..digits_end_at]
|
25
|
+
after_digits = @number[digits_end_at+1..@number.length]
|
26
|
+
|
27
|
+
new_digits = format("%0#{digit_count}d", digits.to_i + 1)
|
28
|
+
|
29
|
+
@number = "#{before_digits}#{new_digits}#{after_digits}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse number
|
33
|
+
@number = number
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
class PatternParser
|
40
|
+
|
41
|
+
def initialize pattern
|
42
|
+
@state = []
|
43
|
+
@buffer = []
|
44
|
+
@number_parsed = false
|
45
|
+
parse pattern
|
46
|
+
end
|
47
|
+
|
48
|
+
def sample
|
49
|
+
@buffer.join
|
50
|
+
end
|
51
|
+
|
52
|
+
def parse pattern
|
53
|
+
pattern_copy = pattern.dup
|
54
|
+
|
55
|
+
while pattern_copy.length > 0
|
56
|
+
token = pattern_copy.slice!(0)
|
57
|
+
case token
|
58
|
+
when 'y'
|
59
|
+
process_current_state unless @state.include? :year
|
60
|
+
@state << :year
|
61
|
+
when 'm'
|
62
|
+
process_current_state unless @state.include? :month
|
63
|
+
@state << :month
|
64
|
+
when 'd'
|
65
|
+
process_current_state unless @state.include? :day
|
66
|
+
@state << :day
|
67
|
+
when '#'
|
68
|
+
process_current_state unless @state.include? :digit
|
69
|
+
@state << :digit
|
70
|
+
when '-'
|
71
|
+
process_current_state
|
72
|
+
@state << :dash
|
73
|
+
else
|
74
|
+
raise Error.new "token not recognized: #{token}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
process_current_state
|
78
|
+
end
|
79
|
+
|
80
|
+
def process_current_state
|
81
|
+
raise Error.new 'error in parser' if @state.uniq.length > 1
|
82
|
+
|
83
|
+
case @state.first
|
84
|
+
when :year
|
85
|
+
case @state.length
|
86
|
+
when 2
|
87
|
+
@buffer << Date.today.strftime('%y')
|
88
|
+
when 4
|
89
|
+
@buffer << Date.today.strftime('%Y')
|
90
|
+
else
|
91
|
+
raise Error.new 'year has 2 or 4 characters'
|
92
|
+
end
|
93
|
+
when :month
|
94
|
+
raise Error.new 'month has two characters' if @state.length != 2
|
95
|
+
@buffer << Date.today.strftime('%m')
|
96
|
+
when :day
|
97
|
+
raise Error.new 'day has two characters' if @state.length != 2
|
98
|
+
@buffer << Date.today.strftime('%d')
|
99
|
+
when :digit
|
100
|
+
raise Error.new('can only have one digit part') if @number_parsed
|
101
|
+
@buffer << format("%0#{@state.length}d", 1)
|
102
|
+
@number_parsed = true
|
103
|
+
when :dash
|
104
|
+
@buffer << '-'
|
105
|
+
end
|
106
|
+
|
107
|
+
@state.clear
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "nummernkreis/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nummernkreis"
|
8
|
+
spec.version = Nummernkreis::VERSION
|
9
|
+
spec.authors = ["Andreas Böhrnsen"]
|
10
|
+
spec.email = ["andreas@frontrunner.io"]
|
11
|
+
|
12
|
+
spec.summary = %q{Number range (Nummernkreis) generator for documents like invoices.}
|
13
|
+
spec.description = %q{Parses number range specification and returns the next valid (document) number.}
|
14
|
+
spec.homepage = "https://github.com/frontrunnerio/nummernkreis"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
24
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
42
|
+
spec.add_development_dependency "timecop", "~> 0.9.0"
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nummernkreis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andreas Böhrnsen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-25 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.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: timecop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.9.0
|
69
|
+
description: Parses number range specification and returns the next valid (document)
|
70
|
+
number.
|
71
|
+
email:
|
72
|
+
- andreas@frontrunner.io
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/nummernkreis.rb
|
87
|
+
- lib/nummernkreis/version.rb
|
88
|
+
- nummernkreis.gemspec
|
89
|
+
homepage: https://github.com/frontrunnerio/nummernkreis
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata:
|
93
|
+
allowed_push_host: https://rubygems.org
|
94
|
+
homepage_uri: https://github.com/frontrunnerio/nummernkreis
|
95
|
+
source_code_uri: https://github.com/frontrunnerio/nummernkreis
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubygems_version: 3.0.3
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Number range (Nummernkreis) generator for documents like invoices.
|
115
|
+
test_files: []
|