monolens 0.6.1 → 0.6.3

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: aea34c867c10c00a01b0f7d986d7aaa76df80048d828d9ebd7083d9aa35853e9
4
- data.tar.gz: cb611d504ff32059dd57736c0964acade1983b7e962d69504f1873fb7100a4d7
3
+ metadata.gz: 937b78e577006ce716022cb0ae10fe2dfd21f174be62e67c2f9eefe161f07151
4
+ data.tar.gz: 90493bf5855603ea1181a5c86a9ba313937954cadb659d95dbca6386b5ba085b
5
5
  SHA512:
6
- metadata.gz: cbecb6300c62e819c18ae4b4d8926041acb60519cfa8b33aea9e93b9c2826484c479069513e3903547ae13619e06e699b9b8c7e8f766982e97040f195388a214
7
- data.tar.gz: '079b220b7dacd7ee54f7a3b668351f832b37ee70c2c5a9c17cf21ed6c10324eff2da4f291e0fe75e0a1f59c2932fccbdfa1cbaf66d95e742079869477920149b'
6
+ metadata.gz: 362211783818b1906bfa90fb42979084e9f1d2bdf7e893313ad5b1d0ef7f2184e1441661221a8df78cfcd530231f7011e678091f26900e041d06414ec92ff116
7
+ data.tar.gz: ad30e2d1b2fddd5f72ec7c939f275ab098586400d70f4f8d6b4e0e4453c30bd63a32c10b6ebc68d815d8e6bde3aaddd041d50b4bb4b8342f84eb23419d31dda7
@@ -0,0 +1,13 @@
1
+ module Monolens
2
+ module Coerce
3
+ class Array
4
+ include Lens
5
+
6
+ signature(Type::Any, Type::Array)
7
+
8
+ def call(arg, world = {})
9
+ Array(arg)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -22,6 +22,11 @@ module Monolens
22
22
  end
23
23
  module_function :string
24
24
 
25
+ def array(options, registry)
26
+ Array.new(options, registry)
27
+ end
28
+ module_function :array
29
+
25
30
  Monolens.define_namespace 'coerce', self
26
31
  end
27
32
  end
@@ -29,3 +34,4 @@ require_relative 'coerce/date'
29
34
  require_relative 'coerce/date_time'
30
35
  require_relative 'coerce/integer'
31
36
  require_relative 'coerce/string'
37
+ require_relative 'coerce/array'
@@ -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 = 3
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Monolens, 'coerce.array' do
4
+ subject do
5
+ Monolens.lens('coerce.array')
6
+ end
7
+
8
+ it 'works' do
9
+ expect(subject.call(12)).to eql([12])
10
+ expect(subject.call(nil)).to eql([])
11
+ expect(subject.call([12, 13])).to eql([12, 13])
12
+ end
13
+ 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.3
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: 2024-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -130,6 +130,7 @@ files:
130
130
  - lib/monolens/stdlib/check.rb
131
131
  - lib/monolens/stdlib/check/not_empty.rb
132
132
  - lib/monolens/stdlib/coerce.rb
133
+ - lib/monolens/stdlib/coerce/array.rb
133
134
  - lib/monolens/stdlib/coerce/date.rb
134
135
  - lib/monolens/stdlib/coerce/date_time.rb
135
136
  - lib/monolens/stdlib/coerce/integer.rb
@@ -193,6 +194,7 @@ files:
193
194
  - spec/monolens/stdlib/array/test_join.rb
194
195
  - spec/monolens/stdlib/array/test_map.rb
195
196
  - spec/monolens/stdlib/check/test_not_empty.rb
197
+ - spec/monolens/stdlib/coerce/test_array.rb
196
198
  - spec/monolens/stdlib/coerce/test_date.rb
197
199
  - spec/monolens/stdlib/coerce/test_datetime.rb
198
200
  - spec/monolens/stdlib/coerce/test_integer.rb
@@ -242,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
242
244
  - !ruby/object:Gem::Version
243
245
  version: '0'
244
246
  requirements: []
245
- rubygems_version: 3.1.4
247
+ rubygems_version: 3.3.27
246
248
  signing_key:
247
249
  specification_version: 4
248
250
  summary: Data transformations inspired by Cambria lenses