validy 1.0.99 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/validy.rb +34 -9
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d89bc7ec77c5a43d71df94fa67ed470ec6865eb251779ece8e4cf8d34dd85247
4
- data.tar.gz: 3e8e93f9566766992b46c30db7e9b1013eb55d758b3a8524775545ed7c0bdad2
3
+ metadata.gz: d5ba15eaa85e57c3dad933c2ec2529bedab3b4df3c7ee872fa2b6e76ec84c48f
4
+ data.tar.gz: 6b2c4411763c4a9759c90297700b82d28a133199eeba688df55b3aba45a107ec
5
5
  SHA512:
6
- metadata.gz: 9eb955a7874c1df5da04f1f5ab99a8f5d5c84bbbc58f12532f8f69ba728559af93adcd48f4a85bb1117c569586683b9f608bacfba5654a1449b7a0db6850a9ea
7
- data.tar.gz: f3c7310ee04b5d41dbf35188afb4ba73a8ac779013636f485caa89afd803e580e846839387bf04e663944de20a561afc4d1b6dd3db5c7a357007fc2a43318f92
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
- @evaluating_attribute = nil
18
+ @evaluating_attribute_value = nil
19
19
 
20
20
  super
21
21
 
@@ -31,8 +31,9 @@ module Validy
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
34
35
  # validation state
35
- def validy_on(method:)
36
+ def validy_on(method:, setters: [])
36
37
  method_with_bang_name = (method[-1] == '!' ? method.to_s : "#{method}!")
37
38
  method_without_bang_name = method_with_bang_name.gsub('!', '')
38
39
 
@@ -55,6 +56,15 @@ module Validy
55
56
  end
56
57
  raise ::Validy::Error, stringified_error unless valid?
57
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
58
68
  end
59
69
  prepend hooks
60
70
  end
@@ -74,6 +84,12 @@ module Validy
74
84
  @valid
75
85
  end
76
86
 
87
+ # "invalid?" returns opposite value of inner valid state
88
+ # @return [Boolean]
89
+ def invalid?
90
+ !valid?
91
+ end
92
+
77
93
  # "errors" returns errors hash
78
94
  # @return [Hash]
79
95
  def errors
@@ -86,6 +102,7 @@ module Validy
86
102
  # @param [Proc] block
87
103
  def condition(method, error = nil, &block)
88
104
  return self unless valid?
105
+ return self if skip_optional?
89
106
 
90
107
  condition = method.respond_to?(:call) ? method.call : send(method)
91
108
  validate_condition(condition, error, &block)
@@ -99,8 +116,8 @@ module Validy
99
116
  def required(attribute, error = nil, &block)
100
117
  return self unless valid?
101
118
 
102
- @evaluating_attribute = instance_variable_get("@#{attribute}")
103
- validate_condition(@evaluating_attribute, error || "#{attribute} required!", &block)
119
+ @evaluating_attribute_value = instance_variable_get("@#{attribute}")
120
+ validate_condition(@evaluating_attribute_value, error || "#{attribute} required!", &block)
104
121
  self
105
122
  end
106
123
 
@@ -109,7 +126,8 @@ module Validy
109
126
  def optional(attribute)
110
127
  return self unless valid?
111
128
 
112
- @evaluating_attribute = instance_variable_get("@#{attribute}")
129
+ @optional = true
130
+ @evaluating_attribute_value = instance_variable_get("@#{attribute}")
113
131
  self
114
132
  end
115
133
 
@@ -119,14 +137,21 @@ module Validy
119
137
  # @param [Proc] block
120
138
  def type(clazz, error = nil, &block)
121
139
  return self unless valid?
140
+ return self if skip_optional?
122
141
 
123
- validate_condition(@evaluating_attribute&.is_a?(clazz),
124
- error || "`#{@evaluating_attribute}` is not a type #{clazz}", &block)
142
+ validate_condition(@evaluating_attribute_value&.is_a?(clazz),
143
+ error || "`#{@evaluating_attribute_value}` is not a type #{clazz}", &block)
125
144
  self
126
145
  end
127
146
 
128
147
  private
129
148
 
149
+ def skip_optional?
150
+ return false unless @optional
151
+
152
+ @evaluating_attribute_value.nil?
153
+ end
154
+
130
155
  def method_presented?(method)
131
156
  method_to_symed = method.to_sym
132
157
  methods.any? { |m| m == method_to_symed }
@@ -145,8 +170,8 @@ module Validy
145
170
  def validate_condition(condition, error = nil, &block)
146
171
  return if condition
147
172
 
148
- error_hash = error_hash(error)
149
- add_error(error_hash)
173
+ error_hash = error_hash error
174
+ add_error error_hash
150
175
 
151
176
  block.call if block_given?
152
177
  end
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.0.99
4
+ version: 1.1.4
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-04-09 00:00:00.000000000 Z
11
+ date: 2022-11-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: