validy 1.1.3 → 1.1.4
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 +23 -8
- 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: d5ba15eaa85e57c3dad933c2ec2529bedab3b4df3c7ee872fa2b6e76ec84c48f
|
4
|
+
data.tar.gz: 6b2c4411763c4a9759c90297700b82d28a133199eeba688df55b3aba45a107ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28042c029f3d322ff70540f5ebe5cbe5a8121463f820320bab838823e0882028ccae920536a2f0429430d9866e8f7396beedc2d0b0fd1bbab717bf98856d88b8
|
7
|
+
data.tar.gz: 6badd77f876d0803f286c2bc11be71499075b481885a14761e4c08c5a6df733ab99064adf4b22dcd3650f4090d6b8179aa8cce3ed80291d9b8e12f38a6d591ca
|
data/lib/validy.rb
CHANGED
@@ -15,7 +15,7 @@ module Validy
|
|
15
15
|
def initialize(*)
|
16
16
|
@errors = {}
|
17
17
|
@valid = true
|
18
|
-
@
|
18
|
+
@evaluating_attribute_value = nil
|
19
19
|
|
20
20
|
super
|
21
21
|
|
@@ -84,6 +84,12 @@ module Validy
|
|
84
84
|
@valid
|
85
85
|
end
|
86
86
|
|
87
|
+
# "invalid?" returns opposite value of inner valid state
|
88
|
+
# @return [Boolean]
|
89
|
+
def invalid?
|
90
|
+
!valid?
|
91
|
+
end
|
92
|
+
|
87
93
|
# "errors" returns errors hash
|
88
94
|
# @return [Hash]
|
89
95
|
def errors
|
@@ -96,6 +102,7 @@ module Validy
|
|
96
102
|
# @param [Proc] block
|
97
103
|
def condition(method, error = nil, &block)
|
98
104
|
return self unless valid?
|
105
|
+
return self if skip_optional?
|
99
106
|
|
100
107
|
condition = method.respond_to?(:call) ? method.call : send(method)
|
101
108
|
validate_condition(condition, error, &block)
|
@@ -109,8 +116,8 @@ module Validy
|
|
109
116
|
def required(attribute, error = nil, &block)
|
110
117
|
return self unless valid?
|
111
118
|
|
112
|
-
@
|
113
|
-
validate_condition(@
|
119
|
+
@evaluating_attribute_value = instance_variable_get("@#{attribute}")
|
120
|
+
validate_condition(@evaluating_attribute_value, error || "#{attribute} required!", &block)
|
114
121
|
self
|
115
122
|
end
|
116
123
|
|
@@ -119,7 +126,8 @@ module Validy
|
|
119
126
|
def optional(attribute)
|
120
127
|
return self unless valid?
|
121
128
|
|
122
|
-
@
|
129
|
+
@optional = true
|
130
|
+
@evaluating_attribute_value = instance_variable_get("@#{attribute}")
|
123
131
|
self
|
124
132
|
end
|
125
133
|
|
@@ -129,14 +137,21 @@ module Validy
|
|
129
137
|
# @param [Proc] block
|
130
138
|
def type(clazz, error = nil, &block)
|
131
139
|
return self unless valid?
|
140
|
+
return self if skip_optional?
|
132
141
|
|
133
|
-
validate_condition(@
|
134
|
-
error || "`#{@
|
142
|
+
validate_condition(@evaluating_attribute_value&.is_a?(clazz),
|
143
|
+
error || "`#{@evaluating_attribute_value}` is not a type #{clazz}", &block)
|
135
144
|
self
|
136
145
|
end
|
137
146
|
|
138
147
|
private
|
139
148
|
|
149
|
+
def skip_optional?
|
150
|
+
return false unless @optional
|
151
|
+
|
152
|
+
@evaluating_attribute_value.nil?
|
153
|
+
end
|
154
|
+
|
140
155
|
def method_presented?(method)
|
141
156
|
method_to_symed = method.to_sym
|
142
157
|
methods.any? { |m| m == method_to_symed }
|
@@ -155,8 +170,8 @@ module Validy
|
|
155
170
|
def validate_condition(condition, error = nil, &block)
|
156
171
|
return if condition
|
157
172
|
|
158
|
-
error_hash = error_hash
|
159
|
-
add_error
|
173
|
+
error_hash = error_hash error
|
174
|
+
add_error error_hash
|
160
175
|
|
161
176
|
block.call if block_given?
|
162
177
|
end
|