monolens 0.5.0 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/monolens/check/not_empty.rb +26 -0
- data/lib/monolens/check.rb +11 -0
- data/lib/monolens/coerce/date.rb +1 -1
- data/lib/monolens/coerce/date_time.rb +1 -1
- data/lib/monolens/core/literal.rb +11 -0
- data/lib/monolens/core.rb +6 -0
- data/lib/monolens/error_handler.rb +10 -0
- data/lib/monolens/version.rb +1 -1
- data/lib/monolens.rb +2 -1
- data/spec/monolens/check/test_not_empty.rb +50 -0
- data/spec/monolens/core/test_literal.rb +13 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bef48815edf1f5c767fd7e41ad3720046b4d6a7c
|
4
|
+
data.tar.gz: 366cfea423fca4d3a7aa7471a67fd19571016b50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e0ae29c6ab1c18b22d3a96c7455983116f5433ba537ddf8a4c6a8c5ef289eb562a3d2d955da6218eb7ccf27a34488405f3fb66798524b03943deb61498093b3
|
7
|
+
data.tar.gz: 8513b04a89c60ded94f11a7296b613bf23358515b4f5e035b933afa9259e1f4ad7c0cc282d1d0c96841feefcaf46743aad325ed877c7e4b476663cb079f281f3
|
data/README.md
CHANGED
@@ -79,6 +79,7 @@ result = lens.call(input)
|
|
79
79
|
core.dig - Extract from the input value (object or array) using a path.
|
80
80
|
core.chain - Applies a chain of lenses to an input value
|
81
81
|
core.mapping - Converts the input value via a key:value mapping
|
82
|
+
core.literal - Returns a constant value takens as lens definition
|
82
83
|
|
83
84
|
str.strip - Remove leading and trailing spaces of an input string
|
84
85
|
str.split - Splits the input string as an array
|
@@ -102,6 +103,8 @@ coerce.integer - Coerces the input value to an integer
|
|
102
103
|
array.compact - Removes null from the input array
|
103
104
|
array.join - Joins values of the input array as a string
|
104
105
|
array.map - Apply a lens to each member of an Array
|
106
|
+
|
107
|
+
check.notEmpty - Throws an error if the input is null or empty
|
105
108
|
```
|
106
109
|
|
107
110
|
## Public API
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module Monolens
|
4
|
+
module Check
|
5
|
+
class NotEmpty
|
6
|
+
include Lens
|
7
|
+
|
8
|
+
def call(arg, world = {})
|
9
|
+
if arg.nil?
|
10
|
+
do_fail!(arg, world)
|
11
|
+
elsif arg.respond_to?(:empty?) && arg.empty?
|
12
|
+
do_fail!(arg, world)
|
13
|
+
else
|
14
|
+
arg
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def do_fail!(arg, world)
|
21
|
+
message = option(:message, 'Input may not be empty')
|
22
|
+
fail!(message, world)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/monolens/coerce/date.rb
CHANGED
data/lib/monolens/core.rb
CHANGED
@@ -10,6 +10,11 @@ module Monolens
|
|
10
10
|
end
|
11
11
|
module_function :dig
|
12
12
|
|
13
|
+
def literal(options)
|
14
|
+
Literal.new(options)
|
15
|
+
end
|
16
|
+
module_function :literal
|
17
|
+
|
13
18
|
def mapping(options)
|
14
19
|
Mapping.new(options)
|
15
20
|
end
|
@@ -21,3 +26,4 @@ end
|
|
21
26
|
require_relative 'core/chain'
|
22
27
|
require_relative 'core/dig'
|
23
28
|
require_relative 'core/mapping'
|
29
|
+
require_relative 'core/literal'
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module Monolens
|
2
2
|
class ErrorHandler
|
3
|
+
include Enumerable
|
4
|
+
|
3
5
|
def initialize
|
4
6
|
@errors = []
|
5
7
|
end
|
@@ -8,6 +10,14 @@ module Monolens
|
|
8
10
|
@errors << error
|
9
11
|
end
|
10
12
|
|
13
|
+
def each(&bl)
|
14
|
+
@errors.each(&bl)
|
15
|
+
end
|
16
|
+
|
17
|
+
def size
|
18
|
+
@errors.size
|
19
|
+
end
|
20
|
+
|
11
21
|
def empty?
|
12
22
|
@errors.empty?
|
13
23
|
end
|
data/lib/monolens/version.rb
CHANGED
data/lib/monolens.rb
CHANGED
@@ -19,6 +19,7 @@ module Monolens
|
|
19
19
|
require_relative 'monolens/array'
|
20
20
|
require_relative 'monolens/object'
|
21
21
|
require_relative 'monolens/coerce'
|
22
|
+
require_relative 'monolens/check'
|
22
23
|
|
23
24
|
def load_file(file)
|
24
25
|
Monolens::File.new(YAML.safe_load(::File.read(file)))
|
@@ -59,7 +60,7 @@ module Monolens
|
|
59
60
|
raise "Invalid lens #{arg}" unless arg.size == 1
|
60
61
|
|
61
62
|
name, options = arg.to_a.first
|
62
|
-
namespace_name, lens_name = if name =~ /^[a-z]+\.[a-z]+$/
|
63
|
+
namespace_name, lens_name = if name =~ /^[a-z]+\.[a-z][a-zA-Z]+$/
|
63
64
|
name.to_s.split('.')
|
64
65
|
else
|
65
66
|
['core', name]
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Monolens, 'check.notEmpty' do
|
4
|
+
subject do
|
5
|
+
Monolens.lens('check.notEmpty' => options)
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'with default options' do
|
9
|
+
let(:options) do
|
10
|
+
{}
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'works on non empty strings' do
|
14
|
+
input = '12'
|
15
|
+
expect(subject.call(input)).to be(input)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'works on non empty arrays' do
|
19
|
+
input = ['12']
|
20
|
+
expect(subject.call(input)).to be(input)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'raises on empty strings' do
|
24
|
+
input = ''
|
25
|
+
expect {
|
26
|
+
subject.call(input)
|
27
|
+
}.to raise_error(Monolens::LensError, 'Input may not be empty')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'raises on empty arrays' do
|
31
|
+
input = []
|
32
|
+
expect {
|
33
|
+
subject.call(input)
|
34
|
+
}.to raise_error(Monolens::LensError, 'Input may not be empty')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with a specific error message' do
|
39
|
+
let(:options) do
|
40
|
+
{ message: 'Hello failure!' }
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'raises on empty strings' do
|
44
|
+
input = ''
|
45
|
+
expect {
|
46
|
+
subject.call(input)
|
47
|
+
}.to raise_error(Monolens::LensError, 'Hello failure!')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
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.5.
|
4
|
+
version: 0.5.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-
|
11
|
+
date: 2022-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -82,6 +82,8 @@ files:
|
|
82
82
|
- lib/monolens/array/compact.rb
|
83
83
|
- lib/monolens/array/join.rb
|
84
84
|
- lib/monolens/array/map.rb
|
85
|
+
- lib/monolens/check.rb
|
86
|
+
- lib/monolens/check/not_empty.rb
|
85
87
|
- lib/monolens/coerce.rb
|
86
88
|
- lib/monolens/coerce/date.rb
|
87
89
|
- lib/monolens/coerce/date_time.rb
|
@@ -91,6 +93,7 @@ files:
|
|
91
93
|
- lib/monolens/core.rb
|
92
94
|
- lib/monolens/core/chain.rb
|
93
95
|
- lib/monolens/core/dig.rb
|
96
|
+
- lib/monolens/core/literal.rb
|
94
97
|
- lib/monolens/core/mapping.rb
|
95
98
|
- lib/monolens/error.rb
|
96
99
|
- lib/monolens/error_handler.rb
|
@@ -120,6 +123,7 @@ files:
|
|
120
123
|
- spec/monolens/array/test_compact.rb
|
121
124
|
- spec/monolens/array/test_join.rb
|
122
125
|
- spec/monolens/array/test_map.rb
|
126
|
+
- spec/monolens/check/test_not_empty.rb
|
123
127
|
- spec/monolens/coerce/test_date.rb
|
124
128
|
- spec/monolens/coerce/test_datetime.rb
|
125
129
|
- spec/monolens/coerce/test_integer.rb
|
@@ -129,6 +133,7 @@ files:
|
|
129
133
|
- spec/monolens/command/names.json
|
130
134
|
- spec/monolens/command/robust-map-upcase.lens.yml
|
131
135
|
- spec/monolens/core/test_dig.rb
|
136
|
+
- spec/monolens/core/test_literal.rb
|
132
137
|
- spec/monolens/core/test_mapping.rb
|
133
138
|
- spec/monolens/lens/test_options.rb
|
134
139
|
- spec/monolens/object/test_extend.rb
|