monolens 0.5.1 → 0.5.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
  SHA1:
3
- metadata.gz: 9f60bac69139624ed4d81f0ae72e710b384ca42f
4
- data.tar.gz: f69b94c2440bd260a61d7619f9883501fa5237d7
3
+ metadata.gz: 2b96981f78af52f468676e9de0aeb3f79e79ab66
4
+ data.tar.gz: fb6445fc49c216be5868b27bef78cdfba79a1264
5
5
  SHA512:
6
- metadata.gz: 5e9cebb9122bac25f9787f8061242b69315bac0bfa5c27eb9cea4c22802060b8b3c7d28e82c536d25011b23756f14663f00030f75d1a8852fea688b5ac6a3a55
7
- data.tar.gz: de6e9e3fedf61c3ef624d11a958ff2de6a5e3d8babd6b8d6f65175179e015e8eff77088bcf7c93e1d571aeeb97ea67b30b94a3b90ff65922c1acd2058b1bbe96
6
+ metadata.gz: 110e2c7da4dcc2195599813adf977af62d1698255339d0ba087bb1b1a7fb886ffd90bd12ded6eeb720547c327294eec758ae462f7e98024fffaecddb8e56c2cd
7
+ data.tar.gz: 1621c07cbba992d5a74c0811e420dd8489ff1c064eaf26bd698404b61bc3ab64ca60da65827ac3a061d0dfad28c483d3e2c07f83e50b4ec5eb22ea944b91d134
data/README.md CHANGED
@@ -102,6 +102,8 @@ coerce.integer - Coerces the input value to an integer
102
102
  array.compact - Removes null from the input array
103
103
  array.join - Joins values of the input array as a string
104
104
  array.map - Apply a lens to each member of an Array
105
+
106
+ check.notEmpty - Throws an error if the input is null or empty
105
107
  ```
106
108
 
107
109
  ## 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
@@ -0,0 +1,11 @@
1
+ module Monolens
2
+ module Check
3
+ def notEmpty(options)
4
+ NotEmpty.new(options)
5
+ end
6
+ module_function :notEmpty
7
+
8
+ Monolens.define_namespace 'check', self
9
+ end
10
+ end
11
+ require_relative 'check/not_empty'
@@ -27,7 +27,7 @@ module Monolens
27
27
  end
28
28
  end
29
29
 
30
- fail!(first_error.message, world)
30
+ fail!("Invalid date `#{arg}`", world) if first_error
31
31
  end
32
32
 
33
33
  def strptime(arg, format = nil)
@@ -27,7 +27,7 @@ module Monolens
27
27
  end
28
28
  end
29
29
 
30
- fail!(first_error.message, world)
30
+ fail!("Invalid DateTime `#{arg}`", world) if first_error
31
31
  end
32
32
 
33
33
  private
@@ -2,7 +2,7 @@ module Monolens
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 5
5
- TINY = 1
5
+ TINY = 2
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
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.1
4
+ version: 0.5.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-05-13 00:00:00.000000000 Z
11
+ date: 2022-05-20 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
@@ -120,6 +122,7 @@ files:
120
122
  - spec/monolens/array/test_compact.rb
121
123
  - spec/monolens/array/test_join.rb
122
124
  - spec/monolens/array/test_map.rb
125
+ - spec/monolens/check/test_not_empty.rb
123
126
  - spec/monolens/coerce/test_date.rb
124
127
  - spec/monolens/coerce/test_datetime.rb
125
128
  - spec/monolens/coerce/test_integer.rb