validate-rb 1.0.0.alpha.2 → 1.0.0.alpha.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/validate/arguments.rb +29 -28
- data/lib/validate/constraints/validation_context.rb +6 -2
- data/lib/validate/errors.rb +3 -3
- data/lib/validate/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbb3ff4387fdcdf89831a255462ce6e01c647a3ac443d85b48d7eb4b61337f71
|
4
|
+
data.tar.gz: 1d8d0e388222fa64c92483293ff2ac15cc409fdb118bebfb807996e879c43f60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98bc8e416ee5c8e8b5d018cb5fe7a667aead58fc8ff27d818ef9129074f1f1d36f74807a00e6704fdac4311b30b1c06c98b1a61951d099ac75b9b7bd0a9e17e0
|
7
|
+
data.tar.gz: b1ce371355794068bb406a1721e4baa9d75d3101b51f915381a8866c8e44a53ed9b25f708a285ac13ee3c7c3ee961be0d9f872664dca95cc8bba6e51d3ee4a16
|
data/lib/validate/arguments.rb
CHANGED
@@ -3,32 +3,12 @@ module Validate
|
|
3
3
|
module ClassMethods
|
4
4
|
def method_added(method_name)
|
5
5
|
super
|
6
|
-
|
7
|
-
|
8
|
-
method = instance_method(method_name)
|
9
|
-
guard = ArgumentsGuard.new(method, @args.dup)
|
10
|
-
|
11
|
-
@methods_guard.__send__(:define_method, method_name) do |*args, &block|
|
12
|
-
guard.send(method_name, *args, &block)
|
13
|
-
super(*args, &block)
|
14
|
-
end
|
15
|
-
ensure
|
16
|
-
@args.clear
|
6
|
+
guard_method(instance_method(method_name), @methods_guard)
|
17
7
|
end
|
18
8
|
|
19
9
|
def singleton_method_added(method_name)
|
20
10
|
super
|
21
|
-
|
22
|
-
|
23
|
-
method = singleton_method(method_name)
|
24
|
-
guard = ArgumentsGuard.new(method, @args.dup)
|
25
|
-
|
26
|
-
@methods_guard.__send__(:define_singleton_method, method_name) do |*args, &block|
|
27
|
-
guard.send(method_name, *args, &block)
|
28
|
-
super(*args, &block)
|
29
|
-
end
|
30
|
-
ensure
|
31
|
-
@args.clear
|
11
|
+
guard_method(singleton_method(method_name), @singleton_methods_guard)
|
32
12
|
end
|
33
13
|
|
34
14
|
def arg(name, &body)
|
@@ -39,6 +19,24 @@ module Validate
|
|
39
19
|
@args[name] = Assertions.create(&body)
|
40
20
|
self
|
41
21
|
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def guard_method(method, guard_module)
|
26
|
+
return if @args.empty?
|
27
|
+
guard = ArgumentsGuard.new(method, @args)
|
28
|
+
guard_module.__send__(:define_method, method.name) do |*args, **kwargs, &block|
|
29
|
+
if kwargs.empty?
|
30
|
+
guard.enforce!(*args, &block)
|
31
|
+
super(*args, &block)
|
32
|
+
else
|
33
|
+
guard.enforce!(*args, **kwargs, &block)
|
34
|
+
super(*args, **kwargs, &block)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
ensure
|
38
|
+
@args = {}
|
39
|
+
end
|
42
40
|
end
|
43
41
|
|
44
42
|
def self.included(base)
|
@@ -46,6 +44,7 @@ module Validate
|
|
46
44
|
base.instance_exec do
|
47
45
|
@args = {}
|
48
46
|
prepend(@methods_guard = Module.new)
|
47
|
+
singleton_class.prepend(@singleton_methods_guard = Module.new)
|
49
48
|
end
|
50
49
|
end
|
51
50
|
|
@@ -73,17 +72,19 @@ module Validate
|
|
73
72
|
when :block
|
74
73
|
signature << "&#{name}"
|
75
74
|
else
|
76
|
-
raise Error::ArgumentError,
|
75
|
+
raise Error::ArgumentError,
|
76
|
+
"unsupported parameter type #{kind}"
|
77
77
|
end
|
78
78
|
next unless rules.include?(name)
|
79
79
|
|
80
|
-
assertions <<
|
80
|
+
assertions <<
|
81
|
+
"@rules[:#{name}].assert(#{name}, message: '#{name}') unless #{name}.eql?(DEFAULT_VALUE)"
|
81
82
|
end
|
82
83
|
|
83
|
-
singleton_class.class_eval(<<~RUBY, __FILE__, __LINE__
|
84
|
-
|
85
|
-
|
86
|
-
|
84
|
+
singleton_class.class_eval(<<~RUBY, __FILE__, __LINE__)
|
85
|
+
def enforce!(#{signature.join(', ')})
|
86
|
+
#{assertions.join("\n ")}
|
87
|
+
end
|
87
88
|
RUBY
|
88
89
|
|
89
90
|
@rules = rules
|
@@ -66,8 +66,10 @@ module Validate
|
|
66
66
|
!@violations.empty?
|
67
67
|
end
|
68
68
|
|
69
|
-
def to_err
|
70
|
-
Error::ConstraintViolationError.new(@violations.freeze)
|
69
|
+
def to_err(backtrace = [])
|
70
|
+
err = Error::ConstraintViolationError.new(@violations.freeze)
|
71
|
+
err.set_backtrace(backtrace)
|
72
|
+
err
|
71
73
|
end
|
72
74
|
|
73
75
|
def merge(other)
|
@@ -104,6 +106,8 @@ module Validate
|
|
104
106
|
Path.new(@paths.dup << path)
|
105
107
|
when Path
|
106
108
|
Path.new(@paths.dup << path.to_a)
|
109
|
+
else
|
110
|
+
raise ArgumentError, 'invalid path'
|
107
111
|
end
|
108
112
|
end
|
109
113
|
|
data/lib/validate/errors.rb
CHANGED
@@ -31,9 +31,9 @@ module Validate
|
|
31
31
|
|
32
32
|
def message
|
33
33
|
@violations.group_by(&:path)
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
.transform_values { |violations| violations.map(&:message) }
|
35
|
+
.map { |path, messages| "#{path}: #{messages.join(', ')}" }
|
36
|
+
.join("\n")
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
data/lib/validate/version.rb
CHANGED