validy 1.1.3 → 1.1.4

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/validy.rb +23 -8
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a9e940d02f71db4c73e4685ba00dbbfd39e3a88bf743b77612873555e85a071
4
- data.tar.gz: 12f5aba56bf4638452a61c57be6adae2a5bb4a153a39c40e48acdf0ba6134533
3
+ metadata.gz: d5ba15eaa85e57c3dad933c2ec2529bedab3b4df3c7ee872fa2b6e76ec84c48f
4
+ data.tar.gz: 6b2c4411763c4a9759c90297700b82d28a133199eeba688df55b3aba45a107ec
5
5
  SHA512:
6
- metadata.gz: a6d6f968d08f66ec0016052595607c91d8b65977338c62114b6262de7e4e720f003a92d73828fc1effed0d8f1282574a29f7bfab4e9052bb68a15d911e8411f2
7
- data.tar.gz: 21f5f6e2935367cd191d4aa49e77ef218da902d7b18c0f99089ddfbb511bdf0c690c4cb5629aa498d0d6c4e41b26ee8e86fbcbb1f74f54ab5f7ca021af2ab3ff
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
 
@@ -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
- @evaluating_attribute = instance_variable_get("@#{attribute}")
113
- 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)
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
- @evaluating_attribute = instance_variable_get("@#{attribute}")
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(@evaluating_attribute&.is_a?(clazz),
134
- 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)
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(error)
159
- add_error(error_hash)
173
+ error_hash = error_hash error
174
+ add_error error_hash
160
175
 
161
176
  block.call if block_given?
162
177
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov