nxt_support 0.1.3 → 0.1.4

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: 3f951700c6299147ea96c2065fc17079862d4cbf1c42a18f7b7cd93a4b0b14fb
4
- data.tar.gz: 19ccfbd579f45fdd335a3ebe5033fe5c697172df65a758ce825d88f97ea18e27
3
+ metadata.gz: 291cef0c8832ad19e20d28d8b74f49aa9f5a3b5fa7de6790f2e63ed8eb509639
4
+ data.tar.gz: f5079328af526df44ce25c107cf078a3536ce797962e11d309f8df8707c9b786
5
5
  SHA512:
6
- metadata.gz: '084dbc6eec41a7b755a5b762494f063e0f02676adc34740554e30e3f32b73999521e76fdacd64e6a90ab61f81028bf9c123c29932c898414c8df549ef7e23cb3'
7
- data.tar.gz: fd42adb4583b76b9bfa08302b0ebed82489e31a7e17fd2eee4f98bab6fa0f8becc85cd585ac1e38b1fadf4fdf41c8572d4307c666b5c72170f8292e111afdbf6
6
+ metadata.gz: 8790a32d160b0735eb142cbd08afb37c0a56fe9cbf9481fd1d019c677e0218a039f9e944cb2c80389e922cb961b2cc52e16578eb73fe6cc1af10769713b797ba
7
+ data.tar.gz: e73f904229c0752092ee9991ad74d5bf4dbda6f774a8207467ec79cf66e51a37fd51700ad26c77d2936ff3d18dc8ca8a83e111ac3cb976ac10b089288a1c5391
@@ -1,67 +1,68 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_support (0.1.3)
4
+ nxt_support (0.1.4)
5
+ nxt_init
5
6
  rails
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
10
- actioncable (6.0.2.1)
11
- actionpack (= 6.0.2.1)
11
+ actioncable (6.0.3.1)
12
+ actionpack (= 6.0.3.1)
12
13
  nio4r (~> 2.0)
13
14
  websocket-driver (>= 0.6.1)
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)
15
+ actionmailbox (6.0.3.1)
16
+ actionpack (= 6.0.3.1)
17
+ activejob (= 6.0.3.1)
18
+ activerecord (= 6.0.3.1)
19
+ activestorage (= 6.0.3.1)
20
+ activesupport (= 6.0.3.1)
20
21
  mail (>= 2.7.1)
21
- actionmailer (6.0.2.1)
22
- actionpack (= 6.0.2.1)
23
- actionview (= 6.0.2.1)
24
- activejob (= 6.0.2.1)
22
+ actionmailer (6.0.3.1)
23
+ actionpack (= 6.0.3.1)
24
+ actionview (= 6.0.3.1)
25
+ activejob (= 6.0.3.1)
25
26
  mail (~> 2.5, >= 2.5.4)
26
27
  rails-dom-testing (~> 2.0)
27
- actionpack (6.0.2.1)
28
- actionview (= 6.0.2.1)
29
- activesupport (= 6.0.2.1)
28
+ actionpack (6.0.3.1)
29
+ actionview (= 6.0.3.1)
30
+ activesupport (= 6.0.3.1)
30
31
  rack (~> 2.0, >= 2.0.8)
31
32
  rack-test (>= 0.6.3)
32
33
  rails-dom-testing (~> 2.0)
33
34
  rails-html-sanitizer (~> 1.0, >= 1.2.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)
35
+ actiontext (6.0.3.1)
36
+ actionpack (= 6.0.3.1)
37
+ activerecord (= 6.0.3.1)
38
+ activestorage (= 6.0.3.1)
39
+ activesupport (= 6.0.3.1)
39
40
  nokogiri (>= 1.8.5)
40
- actionview (6.0.2.1)
41
- activesupport (= 6.0.2.1)
41
+ actionview (6.0.3.1)
42
+ activesupport (= 6.0.3.1)
42
43
  builder (~> 3.1)
43
44
  erubi (~> 1.4)
44
45
  rails-dom-testing (~> 2.0)
45
46
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
- activejob (6.0.2.1)
47
- activesupport (= 6.0.2.1)
47
+ activejob (6.0.3.1)
48
+ activesupport (= 6.0.3.1)
48
49
  globalid (>= 0.3.6)
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)
50
+ activemodel (6.0.3.1)
51
+ activesupport (= 6.0.3.1)
52
+ activerecord (6.0.3.1)
53
+ activemodel (= 6.0.3.1)
54
+ activesupport (= 6.0.3.1)
55
+ activestorage (6.0.3.1)
56
+ actionpack (= 6.0.3.1)
57
+ activejob (= 6.0.3.1)
58
+ activerecord (= 6.0.3.1)
58
59
  marcel (~> 0.3.1)
59
- activesupport (6.0.2.1)
60
+ activesupport (6.0.3.1)
60
61
  concurrent-ruby (~> 1.0, >= 1.0.2)
61
62
  i18n (>= 0.7, < 2)
62
63
  minitest (~> 5.1)
63
64
  tzinfo (~> 1.1)
64
- zeitwerk (~> 2.2)
65
+ zeitwerk (~> 2.2, >= 2.2.2)
65
66
  builder (3.2.4)
66
67
  coderay (1.1.2)
67
68
  concurrent-ruby (1.1.6)
@@ -70,52 +71,54 @@ GEM
70
71
  erubi (1.9.0)
71
72
  globalid (0.4.2)
72
73
  activesupport (>= 4.2.0)
73
- i18n (1.8.2)
74
+ i18n (1.8.3)
74
75
  concurrent-ruby (~> 1.0)
75
- loofah (2.4.0)
76
+ loofah (2.5.0)
76
77
  crass (~> 1.0.2)
77
78
  nokogiri (>= 1.5.9)
78
79
  mail (2.7.1)
79
80
  mini_mime (>= 0.1.1)
80
81
  marcel (0.3.3)
81
82
  mimemagic (~> 0.3.2)
82
- method_source (0.9.2)
83
- mimemagic (0.3.4)
83
+ method_source (1.0.0)
84
+ mimemagic (0.3.5)
84
85
  mini_mime (1.0.2)
85
86
  mini_portile2 (2.4.0)
86
- minitest (5.14.0)
87
+ minitest (5.14.1)
87
88
  nio4r (2.5.2)
88
- nokogiri (1.10.8)
89
+ nokogiri (1.10.9)
89
90
  mini_portile2 (~> 2.4.0)
90
- pry (0.12.2)
91
- coderay (~> 1.1.0)
92
- method_source (~> 0.9.0)
91
+ nxt_init (0.1.5)
92
+ activesupport
93
+ pry (0.13.1)
94
+ coderay (~> 1.1)
95
+ method_source (~> 1.0)
93
96
  rack (2.2.2)
94
97
  rack-test (1.1.0)
95
98
  rack (>= 1.0, < 3)
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)
99
+ rails (6.0.3.1)
100
+ actioncable (= 6.0.3.1)
101
+ actionmailbox (= 6.0.3.1)
102
+ actionmailer (= 6.0.3.1)
103
+ actionpack (= 6.0.3.1)
104
+ actiontext (= 6.0.3.1)
105
+ actionview (= 6.0.3.1)
106
+ activejob (= 6.0.3.1)
107
+ activemodel (= 6.0.3.1)
108
+ activerecord (= 6.0.3.1)
109
+ activestorage (= 6.0.3.1)
110
+ activesupport (= 6.0.3.1)
108
111
  bundler (>= 1.3.0)
109
- railties (= 6.0.2.1)
112
+ railties (= 6.0.3.1)
110
113
  sprockets-rails (>= 2.0.0)
111
114
  rails-dom-testing (2.0.3)
112
115
  activesupport (>= 4.2.0)
113
116
  nokogiri (>= 1.6)
114
117
  rails-html-sanitizer (1.3.0)
115
118
  loofah (~> 2.3)
116
- railties (6.0.2.1)
117
- actionpack (= 6.0.2.1)
118
- activesupport (= 6.0.2.1)
119
+ railties (6.0.3.1)
120
+ actionpack (= 6.0.3.1)
121
+ activesupport (= 6.0.3.1)
119
122
  method_source
120
123
  rake (>= 0.8.7)
121
124
  thor (>= 0.20.3, < 2.0)
@@ -135,7 +138,7 @@ GEM
135
138
  rspec-support (3.9.0)
136
139
  rspec_junit_formatter (0.4.1)
137
140
  rspec-core (>= 2, < 4, != 2.12.0)
138
- sprockets (4.0.0)
141
+ sprockets (4.0.2)
139
142
  concurrent-ruby (~> 1.0)
140
143
  rack (> 1, < 3)
141
144
  sprockets-rails (3.2.1)
@@ -145,12 +148,12 @@ GEM
145
148
  sqlite3 (1.4.2)
146
149
  thor (1.0.1)
147
150
  thread_safe (0.3.6)
148
- tzinfo (1.2.6)
151
+ tzinfo (1.2.7)
149
152
  thread_safe (~> 0.1)
150
- websocket-driver (0.7.1)
153
+ websocket-driver (0.7.2)
151
154
  websocket-extensions (>= 0.1.0)
152
- websocket-extensions (0.1.4)
153
- zeitwerk (2.2.2)
155
+ websocket-extensions (0.1.5)
156
+ zeitwerk (2.3.0)
154
157
 
155
158
  PLATFORMS
156
159
  ruby
data/README.md CHANGED
@@ -81,8 +81,8 @@ Enjoy some useful utilities
81
81
  #### NxtSupport::EnumHash
82
82
 
83
83
  `NxtSupport::EnumHash` is a simple hash with indifferent access to organize a collection of enums.
84
- Keys will be normalized to be underscore and downcase and access with [] is raising a KeyError in case there is
85
- no value for the key.
84
+ Keys will be normalized to be underscore and downcase and access with [] is raising a `KeyError` in case there is
85
+ no value for the key.
86
86
 
87
87
  ```ruby
88
88
  class Book
@@ -97,6 +97,78 @@ Book::STATES['in_weird_state'] # 'in weird State'
97
97
 
98
98
  ```
99
99
 
100
+ #### NxtSupport::HashTranslator
101
+
102
+ `NxtSupport::HashTranslator` is a module that allows you to manipulate keys and values of original hash through tuple hash.
103
+ Tuple hash is a hash where `key` - represent's the key of original hash, and `value` - represents the key of result hash.
104
+ Use `#translate_hash` method to get the result hash.
105
+
106
+ ```ruby
107
+ class TestClass
108
+ include NxtSupport::HashTranslator
109
+ end
110
+
111
+ TestClass.translate_hash(firstname: 'John', firstname: :first_name)
112
+ => { 'first_name' => 'John' }
113
+ ```
114
+ The `value` also could be a `Hash` where key represents the new key in result hash and value must be a lambda or Proc
115
+ that would be used to process value from origin hash. If the tuple hash contains more than 1 key-value paris or value in key value pair
116
+ is not a callable block `InvalidTranslationArgument` error would be raised.
117
+
118
+ ```ruby
119
+ class TestClass
120
+ include NxtSupport::HashTranslator
121
+ end
122
+
123
+ hash = { firstname: 'John', phonenumber: '11-22-33-445' }
124
+ tuple = {
125
+ firstname: :first_name,
126
+ phonenumber: {
127
+ phone_number: ->(number) { number.to_s.prepend('+49-') }
128
+ }
129
+ }
130
+
131
+ TestClass.translate_hash(hash, tuple)
132
+ => { 'first_name' => 'John', 'phone_number' => '+49-11-22-33-445' }
133
+
134
+ hash = { firstname: 'John', phonenumber: '11-22-33-445' }
135
+ tuple = {
136
+ firstname: :first_name,
137
+ phonenumber: {
138
+ phone_number: :some_symbol
139
+ }
140
+ }
141
+
142
+ TestClass.translate_hash(hash, tuple)
143
+ => InvalidTranslationArgument (some_symbol must be a callable block!)
144
+
145
+ hash = { firstname: 'John', phonenumber: '11-22-33-445' }
146
+ tuple = {
147
+ firstname: :first_name,
148
+ phonenumber: {
149
+ phone_number: ->(number) { number.to_s.prepend('+49-') },
150
+ unused_key: :some_symbol
151
+ }
152
+ }
153
+
154
+ TestClass.translate_hash(hash, tuple)
155
+ => InvalidTranslationArgument ({:phone_number=>#<Proc:0x00007ff503175b88 (lambda), :unused_key=>:some_symbol} must contain only 1 key-value pair!)
156
+ ```
157
+
158
+ Or an `Array`.
159
+
160
+ ```ruby
161
+ class TestClass
162
+ include NxtSupport::HashTranslator
163
+ end
164
+
165
+ hash = { firstname: 'John', lastname: 'Doe' }
166
+ tuple = { firstname: :first_name, lastname: [:last_name, :maiden_name] }
167
+
168
+ TestClass.translate_hash(hash, tuple)
169
+ => { 'first_name' => 'John', 'last_name' => 'Doe', 'maiden_name' => 'Doe' }
170
+ ```
171
+
100
172
  ## Development
101
173
 
102
174
  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.
@@ -1 +1,2 @@
1
1
  require_relative 'util/enum_hash'
2
+ require_relative 'util/hash_translator'
@@ -0,0 +1,34 @@
1
+ module NxtSupport
2
+ module HashTranslator
3
+ class HashTranslationService
4
+ class InvalidTranslationArgument < StandardError; end
5
+
6
+ require 'nxt_init'
7
+
8
+ include NxtInit
9
+ attr_init :hash, :tuples
10
+
11
+ def call
12
+ tuples.inject(hash.with_indifferent_access) do |acc, (old_key, new_key)|
13
+ if new_key.is_a?(Hash)
14
+ raise InvalidTranslationArgument.new("#{new_key} hash must contain only 1 key-value pair!") if new_key.size > 1
15
+ key, value = new_key.shift
16
+ raise InvalidTranslationArgument.new("#{value} must be a callable block!") unless value.respond_to?(:call)
17
+ acc[key] = value.call(acc.delete(old_key))
18
+ elsif new_key.is_a?(Array)
19
+ value = acc.delete(old_key)
20
+ new_key.each { |key| acc[key] = value }
21
+ else
22
+ acc[new_key] = acc.delete(old_key)
23
+ end
24
+
25
+ acc
26
+ end
27
+ end
28
+ end
29
+
30
+ def translate_hash(hash, **tuples)
31
+ HashTranslationService.new(hash: hash, tuples: tuples).call
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module NxtSupport
2
- VERSION = "0.1.3".freeze
2
+ VERSION = "0.1.4".freeze
3
3
  end
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = ["lib"]
29
29
 
30
30
  spec.add_dependency "rails"
31
+ spec.add_dependency "nxt_init"
31
32
  spec.add_development_dependency "bundler"
32
33
  spec.add_development_dependency "rake"
33
34
  spec.add_development_dependency "rspec"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nxt_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nils Sommer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-02-19 00:00:00.000000000 Z
12
+ date: 2020-06-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: nxt_init
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: bundler
30
44
  requirement: !ruby/object:Gem::Requirement
@@ -127,6 +141,7 @@ files:
127
141
  - lib/nxt_support/serializers/has_time_attributes.rb
128
142
  - lib/nxt_support/util.rb
129
143
  - lib/nxt_support/util/enum_hash.rb
144
+ - lib/nxt_support/util/hash_translator.rb
130
145
  - lib/nxt_support/version.rb
131
146
  - nxt_support.gemspec
132
147
  homepage: https://github.com/nxt-insurance/nxt_support
@@ -152,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
167
  - !ruby/object:Gem::Version
153
168
  version: '0'
154
169
  requirements: []
155
- rubygems_version: 3.0.3
170
+ rubygems_version: 3.1.2
156
171
  signing_key:
157
172
  specification_version: 4
158
173
  summary: Support through reusable Mixins and Helpers for Ruby on Rails Applications