monolens 0.6.1 → 0.6.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aea34c867c10c00a01b0f7d986d7aaa76df80048d828d9ebd7083d9aa35853e9
4
- data.tar.gz: cb611d504ff32059dd57736c0964acade1983b7e962d69504f1873fb7100a4d7
3
+ metadata.gz: e0de27df8fbe83531b35a1b3c4b92d94669108ef10f17df849d9127a07698ad2
4
+ data.tar.gz: abf82eebdd9511578b0cd73524430335f9b9c8ecb60fe74598eec3e6c0d5f5e5
5
5
  SHA512:
6
- metadata.gz: cbecb6300c62e819c18ae4b4d8926041acb60519cfa8b33aea9e93b9c2826484c479069513e3903547ae13619e06e699b9b8c7e8f766982e97040f195388a214
7
- data.tar.gz: '079b220b7dacd7ee54f7a3b668351f832b37ee70c2c5a9c17cf21ed6c10324eff2da4f291e0fe75e0a1f59c2932fccbdfa1cbaf66d95e742079869477920149b'
6
+ metadata.gz: 3909ab4f03d872d0083884ddea46e3d2546caeef268c83e1145204789050c89e34cd219161a672e07a5b34727c40f21a23be501b85ea7aaed2dd9c1eb54abf66
7
+ data.tar.gz: 394d89daddae630912035d43eefc6ab1a591a45eee3b305b203983cc40c673ad83feb65a3c9510d9a6534311f5c70af7726c83356a64cc6c35a4d2e6949512ee
@@ -8,12 +8,18 @@ module Monolens
8
8
  values: [Type::Object, false], # deprecated
9
9
  default: [Type::Any, false],
10
10
  fallback: [Type::Callback, false],
11
+ key_hash: [Type::Lenses, false],
11
12
  on_missing: [Type::Strategy.missing(%w{default fail fallback keep null}), false]
12
13
  })
13
14
 
14
15
  def call(arg, world = {})
16
+ original_arg = arg
17
+ if key_hash = option(:key_hash, nil)
18
+ arg = key_hash.call(arg, world)
19
+ end
20
+
15
21
  option(:defn, option(:values, {})).fetch(arg) do
16
- on_missing(arg, world)
22
+ on_missing(original_arg, world)
17
23
  end
18
24
  end
19
25
 
@@ -2,7 +2,7 @@ module Monolens
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 6
5
- TINY = 1
5
+ TINY = 2
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -21,6 +21,47 @@ describe Monolens, 'core.mapping' do
21
21
  end
22
22
  end
23
23
 
24
+ context 'with key_hash option' do
25
+ let(:mapping) do
26
+ {
27
+ 'key_hash' => 'str.downcase',
28
+ 'defn' => { 'todo' => 'open' }
29
+ }
30
+ end
31
+
32
+ subject do
33
+ Monolens.lens('core.mapping' => mapping)
34
+ end
35
+
36
+ it 'replaces the value by its mapped' do
37
+ expect(subject.call('todo')).to eql('open')
38
+ end
39
+
40
+ it 'uses the key_hash before looking for mapping' do
41
+ expect(subject.call('TODO')).to eql('open')
42
+ end
43
+
44
+ it 'raises if not found' do
45
+ expect {
46
+ subject.call('nosuchone')
47
+ }.to raise_error(Monolens::LensError)
48
+ end
49
+
50
+ context 'with on_missing: keep' do
51
+ subject do
52
+ Monolens.lens('core.mapping' => mapping.merge('on_missing' => 'keep'))
53
+ end
54
+
55
+ it 'keeps it if missing' do
56
+ expect(subject.call('nosuchone')).to eql('nosuchone')
57
+ end
58
+
59
+ it 'keeps the original one, not the key_hashed one' do
60
+ expect(subject.call('NOSUCHONE')).to eql('NOSUCHONE')
61
+ end
62
+ end
63
+ end
64
+
24
65
  context 'on_missing: default' do
25
66
  subject do
26
67
  Monolens.lens('core.mapping' => mapping.merge('on_missing' => 'default', 'default' => 'foo'))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monolens
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-15 00:00:00.000000000 Z
11
+ date: 2022-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest