nxt_support 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccf77d9e246edf43e011d66dfb2486622819c28adf921c8dfa28812c36d83028
4
- data.tar.gz: b75dfe040f31444f331cfb6d772429c3f3a1cc8659265fb80fdf4757fb05af30
3
+ metadata.gz: e2e9bc399563fcfba0a583d25022018b68a6f5041606d3751a1ad68f9253a62b
4
+ data.tar.gz: 6683f7e84615d951c4d2f57736ab8b1a1c3a5c4d4f77a668a8a3a774afa06572
5
5
  SHA512:
6
- metadata.gz: 402f17412b26c3c04a0945f075b52bafe3ca3f533bc61d427154572262d53eb74d730f12638efe27c923742e2ce8924431dce4a5f575e395f5651f24779810cc
7
- data.tar.gz: ddc4fc3c424dbc88c4b8431e022bf098510bdeea72df77414b16482c8491abb71ab447aa5d48112988f4374c4a71ba46b2c57f5f8208822950ae490aee95e901
6
+ metadata.gz: 44ac6bd228b5d74ae962ac4d58797141858d917a42e733746418c527b7c420cecae82534ad009eca117fcfc2b8a55ab1355a5d1d7887c4d836df66b942df9d08
7
+ data.tar.gz: 8277a6be281c241668ab376c26fff2a72c3e580594c180049232d22ef58a2a442de60937a7e5fc51ed6b9ef07ae7c47c53ba1e1baffe87b81b99e1a3c2a54706
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Unreleased
2
+
3
+ Todo
4
+
5
+ # v0.1.1 2019-02-04
6
+
7
+ ### Added
8
+
9
+ - Added `NxtSupport::EnumHash`.
10
+
11
+ [Compare v0.1.0...v0.1.1](https://github.com/nxt-insurance/nxt_support/compare/v0.1.0...v0.1.1)
12
+
13
+ # v0.1.0 2019-09-29
14
+
15
+ Initial release.
data/Gemfile.lock CHANGED
@@ -1,77 +1,78 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_support (0.1.0)
4
+ nxt_support (0.1.1)
5
5
  rails
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actioncable (6.0.0)
11
- actionpack (= 6.0.0)
10
+ actioncable (6.0.2.1)
11
+ actionpack (= 6.0.2.1)
12
12
  nio4r (~> 2.0)
13
13
  websocket-driver (>= 0.6.1)
14
- actionmailbox (6.0.0)
15
- actionpack (= 6.0.0)
16
- activejob (= 6.0.0)
17
- activerecord (= 6.0.0)
18
- activestorage (= 6.0.0)
19
- activesupport (= 6.0.0)
14
+ actionmailbox (6.0.2.1)
15
+ actionpack (= 6.0.2.1)
16
+ activejob (= 6.0.2.1)
17
+ activerecord (= 6.0.2.1)
18
+ activestorage (= 6.0.2.1)
19
+ activesupport (= 6.0.2.1)
20
20
  mail (>= 2.7.1)
21
- actionmailer (6.0.0)
22
- actionpack (= 6.0.0)
23
- actionview (= 6.0.0)
24
- activejob (= 6.0.0)
21
+ actionmailer (6.0.2.1)
22
+ actionpack (= 6.0.2.1)
23
+ actionview (= 6.0.2.1)
24
+ activejob (= 6.0.2.1)
25
25
  mail (~> 2.5, >= 2.5.4)
26
26
  rails-dom-testing (~> 2.0)
27
- actionpack (6.0.0)
28
- actionview (= 6.0.0)
29
- activesupport (= 6.0.0)
30
- rack (~> 2.0)
27
+ actionpack (6.0.2.1)
28
+ actionview (= 6.0.2.1)
29
+ activesupport (= 6.0.2.1)
30
+ rack (~> 2.0, >= 2.0.8)
31
31
  rack-test (>= 0.6.3)
32
32
  rails-dom-testing (~> 2.0)
33
33
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
34
- actiontext (6.0.0)
35
- actionpack (= 6.0.0)
36
- activerecord (= 6.0.0)
37
- activestorage (= 6.0.0)
38
- activesupport (= 6.0.0)
34
+ actiontext (6.0.2.1)
35
+ actionpack (= 6.0.2.1)
36
+ activerecord (= 6.0.2.1)
37
+ activestorage (= 6.0.2.1)
38
+ activesupport (= 6.0.2.1)
39
39
  nokogiri (>= 1.8.5)
40
- actionview (6.0.0)
41
- activesupport (= 6.0.0)
40
+ actionview (6.0.2.1)
41
+ activesupport (= 6.0.2.1)
42
42
  builder (~> 3.1)
43
43
  erubi (~> 1.4)
44
44
  rails-dom-testing (~> 2.0)
45
45
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
- activejob (6.0.0)
47
- activesupport (= 6.0.0)
46
+ activejob (6.0.2.1)
47
+ activesupport (= 6.0.2.1)
48
48
  globalid (>= 0.3.6)
49
- activemodel (6.0.0)
50
- activesupport (= 6.0.0)
51
- activerecord (6.0.0)
52
- activemodel (= 6.0.0)
53
- activesupport (= 6.0.0)
54
- activestorage (6.0.0)
55
- actionpack (= 6.0.0)
56
- activejob (= 6.0.0)
57
- activerecord (= 6.0.0)
49
+ activemodel (6.0.2.1)
50
+ activesupport (= 6.0.2.1)
51
+ activerecord (6.0.2.1)
52
+ activemodel (= 6.0.2.1)
53
+ activesupport (= 6.0.2.1)
54
+ activestorage (6.0.2.1)
55
+ actionpack (= 6.0.2.1)
56
+ activejob (= 6.0.2.1)
57
+ activerecord (= 6.0.2.1)
58
58
  marcel (~> 0.3.1)
59
- activesupport (6.0.0)
59
+ activesupport (6.0.2.1)
60
60
  concurrent-ruby (~> 1.0, >= 1.0.2)
61
61
  i18n (>= 0.7, < 2)
62
62
  minitest (~> 5.1)
63
63
  tzinfo (~> 1.1)
64
- zeitwerk (~> 2.1, >= 2.1.8)
65
- builder (3.2.3)
64
+ zeitwerk (~> 2.2)
65
+ builder (3.2.4)
66
+ coderay (1.1.2)
66
67
  concurrent-ruby (1.1.5)
67
- crass (1.0.4)
68
+ crass (1.0.6)
68
69
  diff-lcs (1.3)
69
70
  erubi (1.9.0)
70
71
  globalid (0.4.2)
71
72
  activesupport (>= 4.2.0)
72
- i18n (1.6.0)
73
+ i18n (1.8.2)
73
74
  concurrent-ruby (~> 1.0)
74
- loofah (2.3.0)
75
+ loofah (2.4.0)
75
76
  crass (~> 1.0.2)
76
77
  nokogiri (>= 1.5.9)
77
78
  mail (2.7.1)
@@ -79,74 +80,77 @@ GEM
79
80
  marcel (0.3.3)
80
81
  mimemagic (~> 0.3.2)
81
82
  method_source (0.9.2)
82
- mimemagic (0.3.3)
83
+ mimemagic (0.3.4)
83
84
  mini_mime (1.0.2)
84
85
  mini_portile2 (2.4.0)
85
- minitest (5.12.2)
86
+ minitest (5.14.0)
86
87
  nio4r (2.5.2)
87
- nokogiri (1.10.4)
88
+ nokogiri (1.10.7)
88
89
  mini_portile2 (~> 2.4.0)
89
- rack (2.0.7)
90
+ pry (0.12.2)
91
+ coderay (~> 1.1.0)
92
+ method_source (~> 0.9.0)
93
+ rack (2.1.2)
90
94
  rack-test (1.1.0)
91
95
  rack (>= 1.0, < 3)
92
- rails (6.0.0)
93
- actioncable (= 6.0.0)
94
- actionmailbox (= 6.0.0)
95
- actionmailer (= 6.0.0)
96
- actionpack (= 6.0.0)
97
- actiontext (= 6.0.0)
98
- actionview (= 6.0.0)
99
- activejob (= 6.0.0)
100
- activemodel (= 6.0.0)
101
- activerecord (= 6.0.0)
102
- activestorage (= 6.0.0)
103
- activesupport (= 6.0.0)
96
+ rails (6.0.2.1)
97
+ actioncable (= 6.0.2.1)
98
+ actionmailbox (= 6.0.2.1)
99
+ actionmailer (= 6.0.2.1)
100
+ actionpack (= 6.0.2.1)
101
+ actiontext (= 6.0.2.1)
102
+ actionview (= 6.0.2.1)
103
+ activejob (= 6.0.2.1)
104
+ activemodel (= 6.0.2.1)
105
+ activerecord (= 6.0.2.1)
106
+ activestorage (= 6.0.2.1)
107
+ activesupport (= 6.0.2.1)
104
108
  bundler (>= 1.3.0)
105
- railties (= 6.0.0)
109
+ railties (= 6.0.2.1)
106
110
  sprockets-rails (>= 2.0.0)
107
111
  rails-dom-testing (2.0.3)
108
112
  activesupport (>= 4.2.0)
109
113
  nokogiri (>= 1.6)
110
- rails-html-sanitizer (1.2.0)
111
- loofah (~> 2.2, >= 2.2.2)
112
- railties (6.0.0)
113
- actionpack (= 6.0.0)
114
- activesupport (= 6.0.0)
114
+ rails-html-sanitizer (1.3.0)
115
+ loofah (~> 2.3)
116
+ railties (6.0.2.1)
117
+ actionpack (= 6.0.2.1)
118
+ activesupport (= 6.0.2.1)
115
119
  method_source
116
120
  rake (>= 0.8.7)
117
121
  thor (>= 0.20.3, < 2.0)
118
- rake (10.5.0)
119
- rspec (3.8.0)
120
- rspec-core (~> 3.8.0)
121
- rspec-expectations (~> 3.8.0)
122
- rspec-mocks (~> 3.8.0)
123
- rspec-core (3.8.2)
124
- rspec-support (~> 3.8.0)
125
- rspec-expectations (3.8.4)
122
+ rake (13.0.1)
123
+ rspec (3.9.0)
124
+ rspec-core (~> 3.9.0)
125
+ rspec-expectations (~> 3.9.0)
126
+ rspec-mocks (~> 3.9.0)
127
+ rspec-core (3.9.0)
128
+ rspec-support (~> 3.9.0)
129
+ rspec-expectations (3.9.0)
126
130
  diff-lcs (>= 1.2.0, < 2.0)
127
- rspec-support (~> 3.8.0)
128
- rspec-mocks (3.8.1)
131
+ rspec-support (~> 3.9.0)
132
+ rspec-mocks (3.9.0)
129
133
  diff-lcs (>= 1.2.0, < 2.0)
130
- rspec-support (~> 3.8.0)
131
- rspec-support (3.8.2)
134
+ rspec-support (~> 3.9.0)
135
+ rspec-support (3.9.0)
132
136
  rspec_junit_formatter (0.4.1)
133
137
  rspec-core (>= 2, < 4, != 2.12.0)
134
- sprockets (3.7.2)
138
+ sprockets (4.0.0)
135
139
  concurrent-ruby (~> 1.0)
136
140
  rack (> 1, < 3)
137
141
  sprockets-rails (3.2.1)
138
142
  actionpack (>= 4.0)
139
143
  activesupport (>= 4.0)
140
144
  sprockets (>= 3.0.0)
141
- sqlite3 (1.4.1)
142
- thor (0.20.3)
145
+ sqlite3 (1.4.2)
146
+ thor (1.0.1)
143
147
  thread_safe (0.3.6)
144
- tzinfo (1.2.5)
148
+ tzinfo (1.2.6)
145
149
  thread_safe (~> 0.1)
146
150
  websocket-driver (0.7.1)
147
151
  websocket-extensions (>= 0.1.0)
148
152
  websocket-extensions (0.1.4)
149
- zeitwerk (2.1.10)
153
+ zeitwerk (2.2.2)
150
154
 
151
155
  PLATFORMS
152
156
  ruby
@@ -154,10 +158,11 @@ PLATFORMS
154
158
  DEPENDENCIES
155
159
  bundler
156
160
  nxt_support!
161
+ pry
157
162
  rake
158
163
  rspec
159
164
  rspec_junit_formatter
160
165
  sqlite3
161
166
 
162
167
  BUNDLED WITH
163
- 2.0.2
168
+ 2.1.4
data/README.md CHANGED
@@ -24,11 +24,11 @@ Or install it yourself as:
24
24
 
25
25
  Here's an overview all the supporting features.
26
26
 
27
- ### NxtSupport::Models
27
+ ### NxtSupport/Models
28
28
 
29
29
  Enjoy support for your models.
30
30
 
31
- #### NxtSupport::Models::SafelyFindOrCreateable
31
+ #### NxtSupport::SafelyFindOrCreateable
32
32
 
33
33
  The `NxtSupport::Models::SafelyFindOrCreateable` concern is aimed at ActiveRecord models with a uniqueness database constraint. If you use `find_or_create_by` from ActiveRecord, it can happen that the `find_by` call returns `nil` (because no record for the given conditions exists), but in the small timeframe between the `find_by` and the `create` call, another thread inserts a record, so that the `create` call raises an error.
34
34
 
@@ -36,12 +36,35 @@ The `safely_find_or_create_by` method provided by this concern catches such an e
36
36
 
37
37
  ```ruby
38
38
  class Book < ApplicationRecord
39
- include NxtSupport::Models::SafelyFindOrCreateable
39
+ include NxtSupport::SafelyFindOrCreateable
40
40
  end
41
41
 
42
42
  Book.safely_find_or_create_by!(market: 'de', title: 'Moche!')
43
43
  ```
44
44
 
45
+ ### NxtSupport/Util
46
+
47
+ Enjoy some useful utilities
48
+
49
+ #### NxtSupport::EnumHash
50
+
51
+ `NxtSupport::EnumHash` is a simple hash with indifferent access to organize a collection of enums.
52
+ Keys will be normalized to be underscore and downcase and access with [] is raising a KeyError in case there is
53
+ no value for the key.
54
+
55
+ ```ruby
56
+ class Book
57
+ STATES = NxtSupport::EnumHash['draft', 'revised', 'Published', 'in weird State']
58
+ end
59
+
60
+ Book::STATES[:draft] # 'draft'
61
+ Book::STATES['revised'] # 'revised'
62
+ Book::STATES['published'] # 'Published'
63
+ Book::STATES['Published'] # KeyError
64
+ Book::STATES['in_weird_state'] # 'in weird State'
65
+
66
+ ```
67
+
45
68
  ## Development
46
69
 
47
70
  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.
data/lib/nxt_support.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'active_support/all'
2
2
  require "nxt_support/version"
3
3
  require "nxt_support/models"
4
+ require "nxt_support/util"
4
5
 
5
6
  module NxtSupport
6
7
  end
@@ -1,24 +1,22 @@
1
1
  module NxtSupport
2
- module Models
3
- module SafelyFindOrCreateable
4
- extend ActiveSupport::Concern
2
+ module SafelyFindOrCreateable
3
+ extend ActiveSupport::Concern
5
4
 
6
- module ClassMethods
7
- def safely_find_or_create_by(attributes, &block)
8
- transaction(requires_new: true) { create(attributes, &block) }
9
- rescue ActiveRecord::RecordNotUnique
10
- find_by(attributes)
11
- end
5
+ module ClassMethods
6
+ def safely_find_or_create_by(attributes, &block)
7
+ transaction(requires_new: true) { create(attributes, &block) }
8
+ rescue ActiveRecord::RecordNotUnique
9
+ find_by(attributes)
10
+ end
12
11
 
13
- def safely_find_or_create_by!(attributes, &block)
14
- transaction(requires_new: true) { create!(attributes, &block) }
15
- rescue ActiveRecord::RecordNotUnique
16
- find_by!(attributes)
17
- rescue ActiveRecord::RecordInvalid => e
18
- all_errors_are_uniqueness_failures = e.record.errors.details.all? { |_key, errs| errs.all? { |err| err[:error] == :taken } }
19
- raise unless all_errors_are_uniqueness_failures
20
- find_by!(attributes)
21
- end
12
+ def safely_find_or_create_by!(attributes, &block)
13
+ transaction(requires_new: true) { create!(attributes, &block) }
14
+ rescue ActiveRecord::RecordNotUnique
15
+ find_by!(attributes)
16
+ rescue ActiveRecord::RecordInvalid => e
17
+ all_errors_are_uniqueness_failures = e.record.errors.details.all? { |_key, errs| errs.all? { |err| err[:error] == :taken } }
18
+ raise unless all_errors_are_uniqueness_failures
19
+ find_by!(attributes)
22
20
  end
23
21
  end
24
22
  end
@@ -0,0 +1 @@
1
+ require_relative 'util/enum_hash'
@@ -0,0 +1,27 @@
1
+ module NxtSupport
2
+ class EnumHash < ActiveSupport::HashWithIndifferentAccess
3
+ def initialize(*keys)
4
+ super()
5
+
6
+ keys.each do |key|
7
+ self[normalized_key(key)] = key.to_s
8
+ end
9
+
10
+ freeze
11
+ end
12
+
13
+ def self.[](*keys)
14
+ new(*keys)
15
+ end
16
+
17
+ def [](key)
18
+ super(key) || (raise KeyError, "No value for key '#{key}' in #{inspect}")
19
+ end
20
+
21
+ private
22
+
23
+ def normalized_key(key)
24
+ key.to_s.downcase.underscore.gsub(/\s+/, '_')
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module NxtSupport
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1".freeze
3
3
  end
data/nxt_support.gemspec CHANGED
@@ -5,8 +5,8 @@ require "nxt_support/version"
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "nxt_support"
7
7
  spec.version = NxtSupport::VERSION
8
- spec.authors = ["Nils Sommer"]
9
- spec.email = ["mail@nilssommer.de"]
8
+ spec.authors = ["Nils Sommer", "Andreas Robecke"]
9
+ spec.email = ["mail@nilssommer.de", "a.robecke@getsafe.de"]
10
10
 
11
11
  spec.summary = "Support through reusable Mixins and Helpers for Ruby on Rails Applications"
12
12
  spec.homepage = "https://github.com/nxt-insurance/nxt_support"
@@ -15,8 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/nxt-insurance/nxt_init"
19
- spec.metadata["changelog_uri"] = "https://github.com/nxt-insurance/nxt_init/CHANGELOG.md"
18
+ spec.metadata["source_code_uri"] = "https://github.com/nxt-insurance/nxt_support"
19
+ spec.metadata["changelog_uri"] = "https://github.com/nxt-insurance/nxt_support/CHANGELOG.md"
20
20
 
21
21
  # Specify which files should be added to the gem when it is released.
22
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency "rake"
33
33
  spec.add_development_dependency "rspec"
34
34
  spec.add_development_dependency "sqlite3"
35
+ spec.add_development_dependency "pry"
35
36
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nxt_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nils Sommer
8
+ - Andreas Robecke
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2019-09-29 00:00:00.000000000 Z
12
+ date: 2020-02-04 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rails
@@ -80,9 +81,24 @@ dependencies:
80
81
  - - ">="
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
83
98
  description:
84
99
  email:
85
100
  - mail@nilssommer.de
101
+ - a.robecke@getsafe.de
86
102
  executables: []
87
103
  extensions: []
88
104
  extra_rdoc_files: []
@@ -93,6 +109,7 @@ files:
93
109
  - ".rspec"
94
110
  - ".ruby_version"
95
111
  - ".travis.yml"
112
+ - CHANGELOG.md
96
113
  - Gemfile
97
114
  - Gemfile.lock
98
115
  - LICENSE.txt
@@ -103,6 +120,8 @@ files:
103
120
  - lib/nxt_support.rb
104
121
  - lib/nxt_support/models.rb
105
122
  - lib/nxt_support/models/safely_find_or_createable.rb
123
+ - lib/nxt_support/util.rb
124
+ - lib/nxt_support/util/enum_hash.rb
106
125
  - lib/nxt_support/version.rb
107
126
  - nxt_support.gemspec
108
127
  homepage: https://github.com/nxt-insurance/nxt_support
@@ -111,8 +130,8 @@ licenses:
111
130
  metadata:
112
131
  allowed_push_host: https://rubygems.org
113
132
  homepage_uri: https://github.com/nxt-insurance/nxt_support
114
- source_code_uri: https://github.com/nxt-insurance/nxt_init
115
- changelog_uri: https://github.com/nxt-insurance/nxt_init/CHANGELOG.md
133
+ source_code_uri: https://github.com/nxt-insurance/nxt_support
134
+ changelog_uri: https://github.com/nxt-insurance/nxt_support/CHANGELOG.md
116
135
  post_install_message:
117
136
  rdoc_options: []
118
137
  require_paths: