validator_fn 0.1.1 → 0.1.2

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: 284455c8d1f77ee4413e6596c6bb2f4a3c320406d36c88c90284be93a55268fb
4
- data.tar.gz: 989078e17fd8e98f7fe28081f1453cd6796751d5de7b2235703ce7d03ac0ea2a
3
+ metadata.gz: 59d88acda61662b4adc0cf17e6abe58ccab42be28b84291191504ed2545e8d5a
4
+ data.tar.gz: 0c7485fe2f3208a490ad0e4220f00e5770cad7ecb1a1798f5cae98d7bde40a0e
5
5
  SHA512:
6
- metadata.gz: f6d0e379884cd064bfbbff8ab6d9502d5f4afb7a09223110240eadf94ee95e6b67ce7e4c3a85db53273bf5c084bb83200edb697f06043e84b0bc36792b955d9d
7
- data.tar.gz: 7e7422bd1f91b7fa3563081d2fc7c1466940f725852694119146659327beaeed8467a472bfee80a7fc92674192ec5d68c28cfd0035cd6d3da70c9f90a6baa891
6
+ metadata.gz: cea6572a3db872f38516950d3b763b67a81bd1d10dd576624662b84e9203a99e80c9fdaba10b21f6e77d56ba079d261f27d2a5a454bb825dc26b0f64be6664fa
7
+ data.tar.gz: f4917cd8611691a44dc56b49fa2c36b350c30edced673ac71a4aa6b41f412ffb0e538a22d2edcc98ba02522cb2c9aabb2624fa68b43898ee0ca3b22f52113e00
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem "rspec", "~> 3.0"
8
+ gem "pry-nav"
@@ -1,14 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validator_fn (0.1.1)
4
+ validator_fn (0.1.2)
5
5
  fn_reader
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
+ coderay (1.1.3)
10
11
  diff-lcs (1.4.4)
11
12
  fn_reader (0.1.0)
13
+ method_source (0.9.2)
14
+ pry (0.12.2)
15
+ coderay (~> 1.1.0)
16
+ method_source (~> 0.9.0)
17
+ pry-nav (0.3.0)
18
+ pry (>= 0.9.10, < 0.13.0)
12
19
  rake (12.3.3)
13
20
  rspec (3.10.0)
14
21
  rspec-core (~> 3.10.0)
@@ -28,6 +35,7 @@ PLATFORMS
28
35
  ruby
29
36
 
30
37
  DEPENDENCIES
38
+ pry-nav
31
39
  rake (~> 12.0)
32
40
  rspec (~> 3.0)
33
41
  validator_fn!
@@ -1,38 +1,65 @@
1
1
  require "validator_fn/version"
2
2
  require "fn_reader"
3
+ require "pry-nav"
3
4
 
4
5
  module ValidatorFn
5
6
  class Error < StandardError; end
6
7
 
7
8
  fn_reader :something, :matches, :either, :array_of, :any, :is_nil,
8
- :maybe, :present, :is_a, :int, :hash_of, :invalid, :generate_validator, :handle_error, :error_str
9
+ :maybe, :present, :is_a, :int, :hash_of, :invalid, :generate_validator, :handle_error, :error_str, :apply
9
10
 
10
- class Error < StandardError; end
11
+ class Error < StandardError
12
+ attr_reader :my_msg
13
+
14
+ def initialize(msg)
15
+ @my_msg = msg
16
+ super(msg)
17
+ end
18
+
19
+ def message(indent = 0)
20
+ if cause
21
+ cause_msg = if cause.kind_of?(Error)
22
+ cause.message(indent + 1)
23
+ else
24
+ cause.message
25
+ end
26
+ my_msg + "\n" + (" " * (indent + 1)) + cause_msg
27
+ else
28
+ my_msg
29
+ end
30
+ end
31
+ end
11
32
 
33
+ @@apply = ->fn, a {
34
+ begin
35
+ fn.(a)
36
+ rescue StandardError => e
37
+ invalid.("Error applying function")
38
+ end
39
+ }.curry
12
40
  @@invalid = ->msg { raise Error.new(msg) }
13
41
  @@something = ->a { invalid.("Cannot be nil") if a.nil?; a }
14
42
  @@matches = ->regex, a { invalid.("Should match") unless a =~ regex }.curry
15
43
  @@either = ->a, b, value {
16
44
  begin
17
- a.(value) || a.(value)
45
+ a.(value)
18
46
  rescue Error => e
47
+ b.(value)
19
48
  end
20
- a
49
+ value
21
50
  }.curry
22
51
  @@array_of = ->fn, array {
23
52
  is_a.(Array).(array)
24
- array.each_with_index do |a, idx|
53
+ array.each_with_index.map do |a, idx|
25
54
  fn.(a)
26
55
  rescue Error => e
27
56
  invalid.("Invalid value in Array at index #{idx}")
28
57
  end
29
- array
30
58
  }.curry
31
59
  @@any = ->a { a }
32
60
  @@is_nil = ->a { invalid.("Should be nil") unless a.nil?; a }
33
61
  @@maybe = either.(is_nil)
34
62
  @@is_a = ->klass, a { invalid.("Expected type #{klass}, got #{a.inspect}") unless a.is_a?(klass); a }.curry
35
- @@int = ->a { invalid.("#{a.inspect} is not a valid integer") unless a.is_a?(Integer) }
36
63
  @@hash_of = ->fields, hash {
37
64
  hash ||= {}
38
65
  fields.map do |(key, fn)|
@@ -1,3 +1,3 @@
1
1
  module ValidatorFn
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validator_fn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Chabot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-24 00:00:00.000000000 Z
11
+ date: 2020-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fn_reader