nxt_support 0.1.6 → 0.1.7

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: 683364c398008994e7e9efb8007e03bf7056dd816dec3e396a4482ede9e546d9
4
- data.tar.gz: 4e8cee47fa7c145b01ae07b5e8c8ded7da56211ba1272794823e2f8b3a8be5ce
3
+ metadata.gz: b9cd2e7ab48d6e58c2b05de88fecef08c69df0bf8bc5e9de3b1955b86d900187
4
+ data.tar.gz: 155c904d6bdcc15d03ebae72b688f06bcf32cf497b3ac4728e447c23fa512727
5
5
  SHA512:
6
- metadata.gz: de2c12071f41de18b97e49d1516faa54d01b343ab68ff0949004713c8e2345b443cdd60f2f051394e41e966480aca79451b9327d189a60915261814b297620a5
7
- data.tar.gz: 8b3a6e21cedd45c22ca925099c02a2e62fe1c7f3f25dae05770e09952c754cf433c0737746f699da42fb6caddb966673daafe20795f1618bcdb5411a1db9ac31
6
+ metadata.gz: 452a21d24a142cd56a027b5cfb835c37b4ecc16e2966dd27e3c41be364ecef75ff225bf23c06506e6b1a984aff42b27e09082a0be2360d27a92c8abfd036620e
7
+ data.tar.gz: 9c6afc5fe2aa482f9cb3e76e158f681db66fd6c996d2f3dd831cb620489bbc96ec1a4540d892d1980c71bc59ba5039bfd671217efb7ed230f514855846c65d25
@@ -1,3 +1,7 @@
1
+ # v0.1.7 2020-08-20
2
+
3
+ - Added `NxtSupport::Crystalizer`
4
+
1
5
  # v0.1.6 2020-06-18
2
6
 
3
7
  - Added `NxtSupport::PreprocessAttributes`
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_support (0.1.6)
4
+ nxt_support (0.1.7)
5
5
  nxt_init
6
6
  nxt_registry
7
7
  rails
@@ -66,13 +66,13 @@ GEM
66
66
  zeitwerk (~> 2.2, >= 2.2.2)
67
67
  builder (3.2.4)
68
68
  coderay (1.1.2)
69
- concurrent-ruby (1.1.6)
69
+ concurrent-ruby (1.1.7)
70
70
  crass (1.0.6)
71
71
  diff-lcs (1.3)
72
72
  erubi (1.9.0)
73
73
  globalid (0.4.2)
74
74
  activesupport (>= 4.2.0)
75
- i18n (1.8.3)
75
+ i18n (1.8.5)
76
76
  concurrent-ruby (~> 1.0)
77
77
  loofah (2.6.0)
78
78
  crass (~> 1.0.2)
@@ -87,11 +87,11 @@ GEM
87
87
  mini_portile2 (2.4.0)
88
88
  minitest (5.14.1)
89
89
  nio4r (2.5.2)
90
- nokogiri (1.10.9)
90
+ nokogiri (1.10.10)
91
91
  mini_portile2 (~> 2.4.0)
92
92
  nxt_init (0.1.5)
93
93
  activesupport
94
- nxt_registry (0.1.5)
94
+ nxt_registry (0.2.1)
95
95
  activesupport
96
96
  pry (0.13.1)
97
97
  coderay (~> 1.1)
@@ -153,10 +153,10 @@ GEM
153
153
  thread_safe (0.3.6)
154
154
  tzinfo (1.2.7)
155
155
  thread_safe (~> 0.1)
156
- websocket-driver (0.7.2)
156
+ websocket-driver (0.7.3)
157
157
  websocket-extensions (>= 0.1.0)
158
158
  websocket-extensions (0.1.5)
159
- zeitwerk (2.3.0)
159
+ zeitwerk (2.4.0)
160
160
 
161
161
  PLATFORMS
162
162
  ruby
data/README.md CHANGED
@@ -247,6 +247,20 @@ TestClass.translate_hash(hash, tuple)
247
247
  => { 'first_name' => 'John', 'last_name' => 'Doe', 'maiden_name' => 'Doe' }
248
248
  ```
249
249
 
250
+ #### NxtSupport::Crystalizer
251
+
252
+ `NxtSupport::Crystalizer` crystallizes a shared value from an array of elements and screams in case the value is not
253
+ the same across the collection. This is useful in a scenario where you want to guarantee that certain objects share the same
254
+ attribute. Let's say you want to ensure that all users in your collection reference the same department, then the idea
255
+ is that you can crystallize the department from your collection.
256
+
257
+ ```ruby
258
+ NxtSupport::Crystalizer.new(collection: ['andy', 'andy']).call # => 'andy'
259
+ NxtSupport::Crystalizer.new(collection: []).call # NxtSupport::Crystalizer::Error
260
+ NxtSupport::Crystalizer.new(collection: ['andy', 'scotty']).call # NxtSupport::Crystalizer::Error
261
+ NxtSupport::Crystalizer.new(collection: insurances, attribute: :effective_at).call # => shared effective_at or error in case of different effective_ats
262
+ ```
263
+
250
264
  ## Development
251
265
 
252
266
  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,2 +1,3 @@
1
1
  require_relative 'util/enum_hash'
2
2
  require_relative 'util/hash_translator'
3
+ require_relative 'util/crystalizer'
@@ -0,0 +1,34 @@
1
+ module NxtSupport
2
+ class Crystalizer
3
+ Error = Class.new(StandardError)
4
+
5
+ include NxtInit
6
+
7
+ attr_init :collection, on_ambiguity: -> { default_ambiguity_handler }, with: :itself
8
+
9
+ def call
10
+ ensure_unanimity
11
+ unique_values.first
12
+ end
13
+
14
+ private
15
+
16
+ def unique_values
17
+ @unique_values ||= resolved_collection.uniq
18
+ end
19
+
20
+ def resolved_collection
21
+ @resolved_collection ||= collection.map { |value| with.is_a?(Proc) ? with.call(value) : value.send(with) }
22
+ end
23
+
24
+ def default_ambiguity_handler
25
+ ->(collection) { raise Error, "Values in collection are not unanimous: #{collection}" }
26
+ end
27
+
28
+ def ensure_unanimity
29
+ return if unique_values.size == 1
30
+
31
+ on_ambiguity.arity == 1 ? on_ambiguity.call(collection) : on_ambiguity.call
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module NxtSupport
2
- VERSION = "0.1.6".freeze
2
+ VERSION = "0.1.7".freeze
3
3
  end
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.6
4
+ version: 0.1.7
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-07-29 00:00:00.000000000 Z
12
+ date: 2020-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -159,6 +159,7 @@ files:
159
159
  - lib/nxt_support/serializers.rb
160
160
  - lib/nxt_support/serializers/has_time_attributes.rb
161
161
  - lib/nxt_support/util.rb
162
+ - lib/nxt_support/util/crystalizer.rb
162
163
  - lib/nxt_support/util/enum_hash.rb
163
164
  - lib/nxt_support/util/hash_translator.rb
164
165
  - lib/nxt_support/version.rb
@@ -186,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
187
  - !ruby/object:Gem::Version
187
188
  version: '0'
188
189
  requirements: []
189
- rubygems_version: 3.0.8
190
+ rubygems_version: 3.0.3
190
191
  signing_key:
191
192
  specification_version: 4
192
193
  summary: Support through reusable Mixins and Helpers for Ruby on Rails Applications