nxt_error_registry 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 +15 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +148 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/register_error_generator.rb +16 -0
- data/lib/nxt_error_registry/codes_harness.rb +44 -0
- data/lib/nxt_error_registry/default_code_validator.rb +42 -0
- data/lib/nxt_error_registry/registry.rb +39 -0
- data/lib/nxt_error_registry/version.rb +3 -0
- data/lib/nxt_error_registry.rb +48 -0
- data/lib/railtie.rb +8 -0
- data/lib/tasks/generate_code.rake +15 -0
- data/nxt_error_registry.gemspec +44 -0
- metadata +151 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1416b01c67cb9983abfddad361cc89b8c4def0493a7ef77814ed938708070439
|
|
4
|
+
data.tar.gz: 45ae14a632c3400ffdc01f626a4313ba141850c3ef8ea23746e025bf0782383e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: af1e86ac007ab52f8720d6707233d6853acb5f53565ff14e660fe153f27e768ae7bfb8a48a04a63a4220e84ba460b65933dd2fcc5e7ece080af008bfde491cf1
|
|
7
|
+
data.tar.gz: 772c4a08c6db03a3d9e69147e4788becb91fd2c727a567807a9ac0881107b1c96d3454090c74a43a84d4f4ce72a186b9ca0ce1023bfa26cbb22a16761d56012b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
nxt_error_registry (0.1.0)
|
|
5
|
+
activesupport (~> 5.2)
|
|
6
|
+
rails (~> 5.2)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
actioncable (5.2.2.1)
|
|
12
|
+
actionpack (= 5.2.2.1)
|
|
13
|
+
nio4r (~> 2.0)
|
|
14
|
+
websocket-driver (>= 0.6.1)
|
|
15
|
+
actionmailer (5.2.2.1)
|
|
16
|
+
actionpack (= 5.2.2.1)
|
|
17
|
+
actionview (= 5.2.2.1)
|
|
18
|
+
activejob (= 5.2.2.1)
|
|
19
|
+
mail (~> 2.5, >= 2.5.4)
|
|
20
|
+
rails-dom-testing (~> 2.0)
|
|
21
|
+
actionpack (5.2.2.1)
|
|
22
|
+
actionview (= 5.2.2.1)
|
|
23
|
+
activesupport (= 5.2.2.1)
|
|
24
|
+
rack (~> 2.0)
|
|
25
|
+
rack-test (>= 0.6.3)
|
|
26
|
+
rails-dom-testing (~> 2.0)
|
|
27
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
|
28
|
+
actionview (5.2.2.1)
|
|
29
|
+
activesupport (= 5.2.2.1)
|
|
30
|
+
builder (~> 3.1)
|
|
31
|
+
erubi (~> 1.4)
|
|
32
|
+
rails-dom-testing (~> 2.0)
|
|
33
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
|
34
|
+
activejob (5.2.2.1)
|
|
35
|
+
activesupport (= 5.2.2.1)
|
|
36
|
+
globalid (>= 0.3.6)
|
|
37
|
+
activemodel (5.2.2.1)
|
|
38
|
+
activesupport (= 5.2.2.1)
|
|
39
|
+
activerecord (5.2.2.1)
|
|
40
|
+
activemodel (= 5.2.2.1)
|
|
41
|
+
activesupport (= 5.2.2.1)
|
|
42
|
+
arel (>= 9.0)
|
|
43
|
+
activestorage (5.2.2.1)
|
|
44
|
+
actionpack (= 5.2.2.1)
|
|
45
|
+
activerecord (= 5.2.2.1)
|
|
46
|
+
marcel (~> 0.3.1)
|
|
47
|
+
activesupport (5.2.2.1)
|
|
48
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
49
|
+
i18n (>= 0.7, < 2)
|
|
50
|
+
minitest (~> 5.1)
|
|
51
|
+
tzinfo (~> 1.1)
|
|
52
|
+
arel (9.0.0)
|
|
53
|
+
builder (3.2.3)
|
|
54
|
+
coderay (1.1.2)
|
|
55
|
+
concurrent-ruby (1.1.5)
|
|
56
|
+
crass (1.0.4)
|
|
57
|
+
diff-lcs (1.3)
|
|
58
|
+
erubi (1.8.0)
|
|
59
|
+
globalid (0.4.2)
|
|
60
|
+
activesupport (>= 4.2.0)
|
|
61
|
+
i18n (1.6.0)
|
|
62
|
+
concurrent-ruby (~> 1.0)
|
|
63
|
+
loofah (2.2.3)
|
|
64
|
+
crass (~> 1.0.2)
|
|
65
|
+
nokogiri (>= 1.5.9)
|
|
66
|
+
mail (2.7.1)
|
|
67
|
+
mini_mime (>= 0.1.1)
|
|
68
|
+
marcel (0.3.3)
|
|
69
|
+
mimemagic (~> 0.3.2)
|
|
70
|
+
method_source (0.9.2)
|
|
71
|
+
mimemagic (0.3.3)
|
|
72
|
+
mini_mime (1.0.1)
|
|
73
|
+
mini_portile2 (2.4.0)
|
|
74
|
+
minitest (5.11.3)
|
|
75
|
+
nio4r (2.3.1)
|
|
76
|
+
nokogiri (1.10.1)
|
|
77
|
+
mini_portile2 (~> 2.4.0)
|
|
78
|
+
pry (0.12.2)
|
|
79
|
+
coderay (~> 1.1.0)
|
|
80
|
+
method_source (~> 0.9.0)
|
|
81
|
+
rack (2.0.6)
|
|
82
|
+
rack-test (1.1.0)
|
|
83
|
+
rack (>= 1.0, < 3)
|
|
84
|
+
rails (5.2.2.1)
|
|
85
|
+
actioncable (= 5.2.2.1)
|
|
86
|
+
actionmailer (= 5.2.2.1)
|
|
87
|
+
actionpack (= 5.2.2.1)
|
|
88
|
+
actionview (= 5.2.2.1)
|
|
89
|
+
activejob (= 5.2.2.1)
|
|
90
|
+
activemodel (= 5.2.2.1)
|
|
91
|
+
activerecord (= 5.2.2.1)
|
|
92
|
+
activestorage (= 5.2.2.1)
|
|
93
|
+
activesupport (= 5.2.2.1)
|
|
94
|
+
bundler (>= 1.3.0)
|
|
95
|
+
railties (= 5.2.2.1)
|
|
96
|
+
sprockets-rails (>= 2.0.0)
|
|
97
|
+
rails-dom-testing (2.0.3)
|
|
98
|
+
activesupport (>= 4.2.0)
|
|
99
|
+
nokogiri (>= 1.6)
|
|
100
|
+
rails-html-sanitizer (1.0.4)
|
|
101
|
+
loofah (~> 2.2, >= 2.2.2)
|
|
102
|
+
railties (5.2.2.1)
|
|
103
|
+
actionpack (= 5.2.2.1)
|
|
104
|
+
activesupport (= 5.2.2.1)
|
|
105
|
+
method_source
|
|
106
|
+
rake (>= 0.8.7)
|
|
107
|
+
thor (>= 0.19.0, < 2.0)
|
|
108
|
+
rake (10.5.0)
|
|
109
|
+
rspec (3.8.0)
|
|
110
|
+
rspec-core (~> 3.8.0)
|
|
111
|
+
rspec-expectations (~> 3.8.0)
|
|
112
|
+
rspec-mocks (~> 3.8.0)
|
|
113
|
+
rspec-core (3.8.0)
|
|
114
|
+
rspec-support (~> 3.8.0)
|
|
115
|
+
rspec-expectations (3.8.2)
|
|
116
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
117
|
+
rspec-support (~> 3.8.0)
|
|
118
|
+
rspec-mocks (3.8.0)
|
|
119
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
120
|
+
rspec-support (~> 3.8.0)
|
|
121
|
+
rspec-support (3.8.0)
|
|
122
|
+
sprockets (3.7.2)
|
|
123
|
+
concurrent-ruby (~> 1.0)
|
|
124
|
+
rack (> 1, < 3)
|
|
125
|
+
sprockets-rails (3.2.1)
|
|
126
|
+
actionpack (>= 4.0)
|
|
127
|
+
activesupport (>= 4.0)
|
|
128
|
+
sprockets (>= 3.0.0)
|
|
129
|
+
thor (0.20.3)
|
|
130
|
+
thread_safe (0.3.6)
|
|
131
|
+
tzinfo (1.2.5)
|
|
132
|
+
thread_safe (~> 0.1)
|
|
133
|
+
websocket-driver (0.7.0)
|
|
134
|
+
websocket-extensions (>= 0.1.0)
|
|
135
|
+
websocket-extensions (0.1.3)
|
|
136
|
+
|
|
137
|
+
PLATFORMS
|
|
138
|
+
ruby
|
|
139
|
+
|
|
140
|
+
DEPENDENCIES
|
|
141
|
+
bundler (~> 2.0)
|
|
142
|
+
nxt_error_registry!
|
|
143
|
+
pry
|
|
144
|
+
rake (~> 10.0)
|
|
145
|
+
rspec (~> 3.0)
|
|
146
|
+
|
|
147
|
+
BUNDLED WITH
|
|
148
|
+
2.0.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Andreas Robecke
|
|
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,68 @@
|
|
|
1
|
+
# NxtErrorRegistry
|
|
2
|
+
|
|
3
|
+
Register all your errors in a sane way.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'nxt_error_registry'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install nxt_error_registry
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
class LevelOne
|
|
25
|
+
extend NxtErrorRegistry
|
|
26
|
+
register_error :LevelOneError, type: StandardError, code: '100.100'
|
|
27
|
+
# This will set the LevelOne::LevelOneError constant that you can raise anywhere
|
|
28
|
+
|
|
29
|
+
def raise_level_one_error
|
|
30
|
+
raise LevelOneError, 'There was an error on level'
|
|
31
|
+
rescue LevelOneError => e
|
|
32
|
+
puts e.code # would output '100.100'
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`register_error` will make sure that the code was not already registered anywhere else in your app.
|
|
38
|
+
|
|
39
|
+
### Generate an register_error statement to include in your code with the rails generator
|
|
40
|
+
|
|
41
|
+
All arguments are optional and will be set to a placeholder if not provided
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
rails g register_error --name NewErrorName --type SomeKindOfError
|
|
45
|
+
# => register_error :NewErrorName, type: SomeKindOfError, code: '100.000'
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Or use the rake task instead.
|
|
49
|
+
|
|
50
|
+
All arguments are optional and will be set to a placeholder if not provided
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
rake nxt_error_registry:generate_code\[ErrorName,ParentType\]
|
|
54
|
+
# => register_error :ErrorName, type: ParentType, code: '100.000'
|
|
55
|
+
```
|
|
56
|
+
## Development
|
|
57
|
+
|
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
59
|
+
|
|
60
|
+
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).
|
|
61
|
+
|
|
62
|
+
## Contributing
|
|
63
|
+
|
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nxt_error_registry.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "nxt_error_registry"
|
|
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
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class RegisterErrorGenerator < Rails::Generators::Base
|
|
2
|
+
desc "This generator creates a register_error statement to include in your code"
|
|
3
|
+
class_option :name, type: :string, default: 'ErrorName'
|
|
4
|
+
class_option :type, type: :string, default: 'ParentClass'
|
|
5
|
+
|
|
6
|
+
def register_error
|
|
7
|
+
Rails.application.eager_load!
|
|
8
|
+
name = options['name'].camelcase
|
|
9
|
+
type = options['type'].camelcase
|
|
10
|
+
harness = NxtErrorRegistry::CodesHarness.instance
|
|
11
|
+
code = harness.generate_code
|
|
12
|
+
puts '----------------------------------------------'
|
|
13
|
+
puts "register_error :#{name}, type: #{type}, code: '#{code}'"
|
|
14
|
+
puts '----------------------------------------------'
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
|
|
3
|
+
module NxtErrorRegistry
|
|
4
|
+
class CodesHarness
|
|
5
|
+
include Singleton
|
|
6
|
+
|
|
7
|
+
def generate_code
|
|
8
|
+
puts "WARNING: Codes are not in sequence: #{codes_not_in_sequence}" if codes_not_in_sequence.any?
|
|
9
|
+
|
|
10
|
+
last_id = codes_as_ids.last
|
|
11
|
+
return '100.000' unless last_id
|
|
12
|
+
|
|
13
|
+
next_id = last_id + 1
|
|
14
|
+
id_to_code(next_id)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def codes_not_in_sequence
|
|
18
|
+
previous_id = nil
|
|
19
|
+
|
|
20
|
+
codes_as_ids.inject([]) do |acc, id|
|
|
21
|
+
if !previous_id || previous_id + 1 == id
|
|
22
|
+
previous_id = id
|
|
23
|
+
acc
|
|
24
|
+
else
|
|
25
|
+
acc << [previous_id, id]
|
|
26
|
+
previous_id = id
|
|
27
|
+
acc
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def codes_as_ids
|
|
33
|
+
registry.codes.map { |code| Integer(code.delete('.')) }.sort
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def id_to_code(code)
|
|
37
|
+
"#{code.to_s[0..2]}.#{code.to_s[3..-1]}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def registry
|
|
41
|
+
NxtErrorRegistry::Registry.instance
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module NxtErrorRegistry
|
|
2
|
+
class DefaultCodeValidator
|
|
3
|
+
CodeAlreadyTakenError = Class.new(StandardError)
|
|
4
|
+
InvalidCodeFormatError = Class.new(StandardError)
|
|
5
|
+
|
|
6
|
+
FORMAT = /\A\d{3}\.\d{3}\z/
|
|
7
|
+
|
|
8
|
+
def initialize(name, type, code, context)
|
|
9
|
+
@name = name
|
|
10
|
+
@type = type
|
|
11
|
+
@code = code
|
|
12
|
+
@context = context
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def validate
|
|
16
|
+
validate_code_format
|
|
17
|
+
validate_code_uniqueness
|
|
18
|
+
|
|
19
|
+
code
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :name, :type, :code, :context
|
|
25
|
+
|
|
26
|
+
def validate_code_format
|
|
27
|
+
return if code =~ FORMAT
|
|
28
|
+
raise InvalidCodeFormatError, "Code #{code} for name #{name} violates format #{FORMAT} in context: #{context}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def validate_code_uniqueness
|
|
32
|
+
duplicates = registry.duplicate_codes
|
|
33
|
+
return if duplicates.empty?
|
|
34
|
+
|
|
35
|
+
raise CodeAlreadyTakenError, "The following codes are duplicated: #{duplicates.keys.join(',')}"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def registry
|
|
39
|
+
NxtErrorRegistry::Registry.instance
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module NxtErrorRegistry
|
|
2
|
+
class Registry
|
|
3
|
+
def self.instance
|
|
4
|
+
@instance ||= send(:new)
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
# Usually we don't want this to be initialized other than through instance
|
|
8
|
+
private_class_method :new
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@store = { }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
attr_reader :store
|
|
15
|
+
|
|
16
|
+
delegate_missing_to :store
|
|
17
|
+
delegate :to_s, :inspect, to: :store
|
|
18
|
+
|
|
19
|
+
def flat
|
|
20
|
+
values.map(&:values).flatten
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def codes
|
|
24
|
+
flat.map { |entry| entry.fetch(:code) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def entries_by_codes
|
|
28
|
+
flat.inject({}) do |acc, entry|
|
|
29
|
+
code = entry.fetch(:code)
|
|
30
|
+
(acc[code] ||= []) << entry
|
|
31
|
+
acc
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def duplicate_codes
|
|
36
|
+
entries_by_codes.select { |_, v| v.size > 1 }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
require "active_support/all"
|
|
2
|
+
require "nxt_error_registry/version"
|
|
3
|
+
require "nxt_error_registry/registry"
|
|
4
|
+
require "nxt_error_registry/default_code_validator"
|
|
5
|
+
require "nxt_error_registry/codes_harness"
|
|
6
|
+
require 'railtie' if defined?(Rails)
|
|
7
|
+
|
|
8
|
+
module NxtErrorRegistry
|
|
9
|
+
RegistrationError = Class.new(StandardError)
|
|
10
|
+
CodeValidator = DefaultCodeValidator
|
|
11
|
+
|
|
12
|
+
def register_error(name, type:, code:, **opts, &block)
|
|
13
|
+
raise_name_not_a_symbol_error(name) unless name.is_a?(Symbol)
|
|
14
|
+
raise_registration_error(name) if const_defined?(name)
|
|
15
|
+
|
|
16
|
+
error_class = Class.new(type, &block)
|
|
17
|
+
error_class.define_singleton_method :code, -> { code }
|
|
18
|
+
error_class.define_singleton_method :options, -> { opts }
|
|
19
|
+
|
|
20
|
+
const_set(name, error_class)
|
|
21
|
+
entry = { code: code, error_class: error_class, type: type, name: name, namespace: self.to_s, opts: opts }
|
|
22
|
+
error_registry[name.to_s] = entry
|
|
23
|
+
|
|
24
|
+
# This depends on duplicate entries so has to come last
|
|
25
|
+
validate_code(name, type, code)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def validate_code(name, type, code)
|
|
31
|
+
CodeValidator.new(name, type, code, self).validate
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def error_registry
|
|
35
|
+
@error_registry ||= begin
|
|
36
|
+
::NxtErrorRegistry::Registry.instance[self.to_s] ||= { }
|
|
37
|
+
::NxtErrorRegistry::Registry.instance[self.to_s]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def raise_registration_error(name)
|
|
42
|
+
raise RegistrationError, "#{name} was already registered in #{self}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def raise_name_not_a_symbol_error(name)
|
|
46
|
+
raise ArgumentError, "Error name '#{name}' must be a symbol"
|
|
47
|
+
end
|
|
48
|
+
end
|
data/lib/railtie.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
namespace :nxt_error_registry do
|
|
2
|
+
desc 'Generate a unique error code'
|
|
3
|
+
task :generate_code, [:name, :type] => [:environment] do |_, args|
|
|
4
|
+
Rails.application.eager_load!
|
|
5
|
+
name = args.fetch(:name, 'ErrorName').camelcase
|
|
6
|
+
type = args.fetch(:type, 'ParentClass').camelcase
|
|
7
|
+
|
|
8
|
+
harness = NxtErrorRegistry::CodesHarness.instance
|
|
9
|
+
code = harness.generate_code
|
|
10
|
+
puts '----------------------------------------------'
|
|
11
|
+
puts "register_error :#{name}, type: #{type}, code: '#{code}'"
|
|
12
|
+
puts '----------------------------------------------'
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "nxt_error_registry/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "nxt_error_registry"
|
|
8
|
+
spec.version = NxtErrorRegistry::VERSION
|
|
9
|
+
spec.authors = ["Andreas Robecke", "Nils Sommer", "Raphael Kallensee"]
|
|
10
|
+
spec.email = ["a.robecke@getsafe.de"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Write a short summary, because RubyGems requires one.}
|
|
13
|
+
spec.description = %q{Write a longer description or delete this line.}
|
|
14
|
+
spec.homepage = "https://github.com/nxt-insurance"
|
|
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"] = "https://github.com/nxt-insurance/nxt_init"
|
|
24
|
+
else
|
|
25
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
26
|
+
"public gem pushes."
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
31
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
33
|
+
end
|
|
34
|
+
spec.bindir = "exe"
|
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
36
|
+
spec.require_paths = ["lib"]
|
|
37
|
+
|
|
38
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
|
39
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
40
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
41
|
+
spec.add_development_dependency "pry"
|
|
42
|
+
spec.add_dependency "activesupport", "~> 5.2"
|
|
43
|
+
spec.add_dependency "rails", "~> 5.2"
|
|
44
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: nxt_error_registry
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andreas Robecke
|
|
8
|
+
- Nils Sommer
|
|
9
|
+
- Raphael Kallensee
|
|
10
|
+
autorequire:
|
|
11
|
+
bindir: exe
|
|
12
|
+
cert_chain: []
|
|
13
|
+
date: 2019-03-25 00:00:00.000000000 Z
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: bundler
|
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
|
18
|
+
requirements:
|
|
19
|
+
- - "~>"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '2.0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - "~>"
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '2.0'
|
|
29
|
+
- !ruby/object:Gem::Dependency
|
|
30
|
+
name: rake
|
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - "~>"
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '10.0'
|
|
36
|
+
type: :development
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - "~>"
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '10.0'
|
|
43
|
+
- !ruby/object:Gem::Dependency
|
|
44
|
+
name: rspec
|
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - "~>"
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '3.0'
|
|
50
|
+
type: :development
|
|
51
|
+
prerelease: false
|
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - "~>"
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: '3.0'
|
|
57
|
+
- !ruby/object:Gem::Dependency
|
|
58
|
+
name: pry
|
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '0'
|
|
64
|
+
type: :development
|
|
65
|
+
prerelease: false
|
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
71
|
+
- !ruby/object:Gem::Dependency
|
|
72
|
+
name: activesupport
|
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - "~>"
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '5.2'
|
|
78
|
+
type: :runtime
|
|
79
|
+
prerelease: false
|
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - "~>"
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: '5.2'
|
|
85
|
+
- !ruby/object:Gem::Dependency
|
|
86
|
+
name: rails
|
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
|
88
|
+
requirements:
|
|
89
|
+
- - "~>"
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: '5.2'
|
|
92
|
+
type: :runtime
|
|
93
|
+
prerelease: false
|
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
95
|
+
requirements:
|
|
96
|
+
- - "~>"
|
|
97
|
+
- !ruby/object:Gem::Version
|
|
98
|
+
version: '5.2'
|
|
99
|
+
description: Write a longer description or delete this line.
|
|
100
|
+
email:
|
|
101
|
+
- a.robecke@getsafe.de
|
|
102
|
+
executables: []
|
|
103
|
+
extensions: []
|
|
104
|
+
extra_rdoc_files: []
|
|
105
|
+
files:
|
|
106
|
+
- ".gitignore"
|
|
107
|
+
- ".rspec"
|
|
108
|
+
- ".travis.yml"
|
|
109
|
+
- Gemfile
|
|
110
|
+
- Gemfile.lock
|
|
111
|
+
- LICENSE.txt
|
|
112
|
+
- README.md
|
|
113
|
+
- Rakefile
|
|
114
|
+
- bin/console
|
|
115
|
+
- bin/setup
|
|
116
|
+
- lib/generators/register_error_generator.rb
|
|
117
|
+
- lib/nxt_error_registry.rb
|
|
118
|
+
- lib/nxt_error_registry/codes_harness.rb
|
|
119
|
+
- lib/nxt_error_registry/default_code_validator.rb
|
|
120
|
+
- lib/nxt_error_registry/registry.rb
|
|
121
|
+
- lib/nxt_error_registry/version.rb
|
|
122
|
+
- lib/railtie.rb
|
|
123
|
+
- lib/tasks/generate_code.rake
|
|
124
|
+
- nxt_error_registry.gemspec
|
|
125
|
+
homepage: https://github.com/nxt-insurance
|
|
126
|
+
licenses:
|
|
127
|
+
- MIT
|
|
128
|
+
metadata:
|
|
129
|
+
allowed_push_host: https://rubygems.org
|
|
130
|
+
homepage_uri: https://github.com/nxt-insurance
|
|
131
|
+
source_code_uri: https://github.com/nxt-insurance/nxt_init
|
|
132
|
+
post_install_message:
|
|
133
|
+
rdoc_options: []
|
|
134
|
+
require_paths:
|
|
135
|
+
- lib
|
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
137
|
+
requirements:
|
|
138
|
+
- - ">="
|
|
139
|
+
- !ruby/object:Gem::Version
|
|
140
|
+
version: '0'
|
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - ">="
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: '0'
|
|
146
|
+
requirements: []
|
|
147
|
+
rubygems_version: 3.0.1
|
|
148
|
+
signing_key:
|
|
149
|
+
specification_version: 4
|
|
150
|
+
summary: Write a short summary, because RubyGems requires one.
|
|
151
|
+
test_files: []
|