validy 0.1.1 → 1.1.3
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 +4 -4
- data/lib/validy.rb +24 -2
- 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: 4a9e940d02f71db4c73e4685ba00dbbfd39e3a88bf743b77612873555e85a071
|
4
|
+
data.tar.gz: 12f5aba56bf4638452a61c57be6adae2a5bb4a153a39c40e48acdf0ba6134533
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6d6f968d08f66ec0016052595607c91d8b65977338c62114b6262de7e4e720f003a92d73828fc1effed0d8f1282574a29f7bfab4e9052bb68a15d911e8411f2
|
7
|
+
data.tar.gz: 21f5f6e2935367cd191d4aa49e77ef218da902d7b18c0f99089ddfbb511bdf0c690c4cb5629aa498d0d6c4e41b26ee8e86fbcbb1f74f54ab5f7ca021af2ab3ff
|
data/lib/validy.rb
CHANGED
@@ -30,7 +30,10 @@ module Validy
|
|
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<String>] - optional, list of the instance variables for checking valid state while using setter
|
35
|
+
# validation state
|
36
|
+
def validy_on(method:, setters: [])
|
34
37
|
method_with_bang_name = (method[-1] == '!' ? method.to_s : "#{method}!")
|
35
38
|
method_without_bang_name = method_with_bang_name.gsub('!', '')
|
36
39
|
|
@@ -53,6 +56,15 @@ module Validy
|
|
53
56
|
end
|
54
57
|
raise ::Validy::Error, stringified_error unless valid?
|
55
58
|
end
|
59
|
+
|
60
|
+
if setters.any?
|
61
|
+
setters.each do |name|
|
62
|
+
define_method("#{name}=".to_sym) do |val|
|
63
|
+
instance_variable_set("@#{name}", val)
|
64
|
+
method[-1] == '!' ? send(method_with_bang) : send(method_without_bang)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
56
68
|
end
|
57
69
|
prepend hooks
|
58
70
|
end
|
@@ -79,6 +91,9 @@ module Validy
|
|
79
91
|
end
|
80
92
|
|
81
93
|
# "condition" evaluates either passed block or instance method represented in the instance
|
94
|
+
# @param [String|block] method or callable object which will be triggered for validation
|
95
|
+
# @param [String|Hash] error definition
|
96
|
+
# @param [Proc] block
|
82
97
|
def condition(method, error = nil, &block)
|
83
98
|
return self unless valid?
|
84
99
|
|
@@ -88,6 +103,9 @@ module Validy
|
|
88
103
|
end
|
89
104
|
|
90
105
|
# "required" checks presence of the variable
|
106
|
+
# @param [String] attribute a target one
|
107
|
+
# @param [String|Hash] error custom defined error message
|
108
|
+
# @param [Proc] block
|
91
109
|
def required(attribute, error = nil, &block)
|
92
110
|
return self unless valid?
|
93
111
|
|
@@ -97,6 +115,7 @@ module Validy
|
|
97
115
|
end
|
98
116
|
|
99
117
|
# "optional" starts void validation for the given attribute
|
118
|
+
# @param [String] attribute a target one
|
100
119
|
def optional(attribute)
|
101
120
|
return self unless valid?
|
102
121
|
|
@@ -105,11 +124,14 @@ module Validy
|
|
105
124
|
end
|
106
125
|
|
107
126
|
# "type" validates type of the instance variable
|
127
|
+
# @param [Object] clazz for checking type of the target attribute
|
128
|
+
# @param [nil] error custom defined error message
|
129
|
+
# @param [Proc] block
|
108
130
|
def type(clazz, error = nil, &block)
|
109
131
|
return self unless valid?
|
110
132
|
|
111
133
|
validate_condition(@evaluating_attribute&.is_a?(clazz),
|
112
|
-
error || "
|
134
|
+
error || "`#{@evaluating_attribute}` is not a type #{clazz}", &block)
|
113
135
|
self
|
114
136
|
end
|
115
137
|
|
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:
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Saltykov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|