validy 1.1.4 → 1.1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/validy.rb +42 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6879285b4152539ec21a22301c44dde44677bd5bace7f9dbf2a06dc8b17dff9f
|
4
|
+
data.tar.gz: 9894b76aef7cae5ed3d814227a6af9358cb66d83dd14cec4adeead016185db42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f9adb3f505ef053e0b0abb28c9ee2d6da8b88456435db42ae5b8fcd48942ebbe49cc6dbb43dff2919d08d71217bdc27d6f4cc683bd732c4d3d3833c346cff3e
|
7
|
+
data.tar.gz: 5d3c7eba42b4a836648a6e5194ba9b8f2948c2931dc803a79ac0b4d074c8b17bc32dda87db926dba44cc733ab944b5208fa18007c62b8f4f26bf45ac722d5717
|
data/lib/validy.rb
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
module Validy
|
4
4
|
Error = Class.new(StandardError)
|
5
5
|
NotImplementedError = Class.new(StandardError)
|
6
|
-
|
6
|
+
|
7
|
+
MUST_BE_IMPLEMENTED_ERROR = 'validy method given from validy_on method: argument, must be implemented!'
|
7
8
|
|
8
9
|
def self.included(base)
|
9
10
|
base.send(:extend, ClassMethods)
|
@@ -15,36 +16,36 @@ module Validy
|
|
15
16
|
def initialize(*)
|
16
17
|
@errors = {}
|
17
18
|
@valid = true
|
18
|
-
@evaluating_attribute_value = nil
|
19
19
|
|
20
20
|
super
|
21
|
-
|
21
|
+
# trigger validations
|
22
22
|
if method_presented?(method_without_bang)
|
23
23
|
send(method_without_bang)
|
24
24
|
elsif method_presented?(method_with_bang)
|
25
25
|
send(method_with_bang)
|
26
26
|
else
|
27
|
-
raise NotImplementedError,
|
27
|
+
raise NotImplementedError, MUST_BE_IMPLEMENTED_ERROR
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
module ClassMethods
|
33
33
|
# @param [String] method - indicates custom, must be implemented method for which will be triggered for defining
|
34
|
-
# @param [Array
|
34
|
+
# @param [Array] setters - optional, list of the instance variables for checking valid state while using setter
|
35
35
|
# validation state
|
36
|
+
# @return [void]
|
36
37
|
def validy_on(method:, setters: [])
|
37
38
|
method_with_bang_name = (method[-1] == '!' ? method.to_s : "#{method}!")
|
38
39
|
method_without_bang_name = method_with_bang_name.gsub('!', '')
|
39
40
|
|
40
|
-
|
41
|
-
method_with_bang_name
|
42
|
-
end
|
41
|
+
define_validation_methods_name(method_with_bang_name, method_without_bang_name)
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
define_validation_triggers(method, setters)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
47
|
|
48
|
+
def define_validation_triggers(method, setters)
|
48
49
|
hooks = Module.new do
|
49
50
|
method_with_bang_name = (method[-1] == '!' ? method.to_s : "#{method}!")
|
50
51
|
method_without_bang_name = method_with_bang_name.gsub('!', '')
|
@@ -54,6 +55,12 @@ module Validy
|
|
54
55
|
else
|
55
56
|
super(*args, &block)
|
56
57
|
end
|
58
|
+
|
59
|
+
if instance_variable_defined?('@evaluating_attribute_value')
|
60
|
+
remove_instance_variable(:@evaluating_attribute_value)
|
61
|
+
end
|
62
|
+
remove_instance_variable(:@optional) if instance_variable_defined?('@optional')
|
63
|
+
|
57
64
|
raise ::Validy::Error, stringified_error unless valid?
|
58
65
|
end
|
59
66
|
|
@@ -61,13 +68,32 @@ module Validy
|
|
61
68
|
setters.each do |name|
|
62
69
|
define_method("#{name}=".to_sym) do |val|
|
63
70
|
instance_variable_set("@#{name}", val)
|
71
|
+
|
64
72
|
method[-1] == '!' ? send(method_with_bang) : send(method_without_bang)
|
73
|
+
|
74
|
+
if instance_variable_defined?('@evaluating_attribute_value')
|
75
|
+
remove_instance_variable(:@evaluating_attribute_value)
|
76
|
+
end
|
77
|
+
remove_instance_variable(:@optional) if instance_variable_defined?('@optional')
|
78
|
+
|
79
|
+
val
|
65
80
|
end
|
66
81
|
end
|
67
82
|
end
|
68
83
|
end
|
84
|
+
|
69
85
|
prepend hooks
|
70
86
|
end
|
87
|
+
|
88
|
+
def define_validation_methods_name(with_bang_name, without_bang_name)
|
89
|
+
define_method :method_with_bang do
|
90
|
+
with_bang_name
|
91
|
+
end
|
92
|
+
|
93
|
+
define_method :method_without_bang do
|
94
|
+
without_bang_name
|
95
|
+
end
|
96
|
+
end
|
71
97
|
end
|
72
98
|
|
73
99
|
module InstanceMethods
|
@@ -139,8 +165,10 @@ module Validy
|
|
139
165
|
return self unless valid?
|
140
166
|
return self if skip_optional?
|
141
167
|
|
142
|
-
validate_condition(
|
143
|
-
|
168
|
+
validate_condition(
|
169
|
+
@evaluating_attribute_value&.is_a?(clazz),
|
170
|
+
error || "`#{@evaluating_attribute_value}` is not a type #{clazz}", &block
|
171
|
+
)
|
144
172
|
self
|
145
173
|
end
|
146
174
|
|
@@ -168,7 +196,7 @@ module Validy
|
|
168
196
|
end
|
169
197
|
|
170
198
|
def validate_condition(condition, error = nil, &block)
|
171
|
-
return if condition
|
199
|
+
return if condition.nil? # bugfix for boolean condition that can return false
|
172
200
|
|
173
201
|
error_hash = error_hash error
|
174
202
|
add_error error_hash
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Saltykov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|