validy 1.1.3 → 1.1.5

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 +61 -18
  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: ae1ac788467cb913065937d6a6d46707f3e4b6806ab54f0d7066f4e14b5eae50
4
+ data.tar.gz: 01ecb46cd5f4049055c096104d66378cd52588459b433758ae39615cef46d802
5
5
  SHA512:
6
- metadata.gz: a6d6f968d08f66ec0016052595607c91d8b65977338c62114b6262de7e4e720f003a92d73828fc1effed0d8f1282574a29f7bfab4e9052bb68a15d911e8411f2
7
- data.tar.gz: 21f5f6e2935367cd191d4aa49e77ef218da902d7b18c0f99089ddfbb511bdf0c690c4cb5629aa498d0d6c4e41b26ee8e86fbcbb1f74f54ab5f7ca021af2ab3ff
6
+ metadata.gz: c2d1e5e617d22d62580be51d63d626e96d0f4d49b3d4c42373d2c8493b324d29b20678f8cb45cadd671d2032fafbd0cd489ac0e4f179fe6fb39b4572a51156c7
7
+ data.tar.gz: f7a4b0e171cfce9bea900d0e073156d1221cff2fad65a868055605fbdc9a10643535897ebe53eadab9687a9461488bca503473924d777a1c06bd02cbaadfa4ba
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
- OverImplemented = Class.new(StandardError)
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 = 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, 'validy method given from validy_on method: argument, must be implemented!'
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<String>] - optional, list of the instance variables for checking valid state while using setter
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
- define_method :method_with_bang do
41
- method_with_bang_name
42
- end
41
+ define_validation_methods_name(method_with_bang_name, method_without_bang_name)
43
42
 
44
- define_method :method_without_bang do
45
- method_without_bang_name
46
- end
43
+ define_validation_triggers(method, setters)
44
+ end
47
45
 
46
+ private
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
@@ -84,6 +110,12 @@ module Validy
84
110
  @valid
85
111
  end
86
112
 
113
+ # "invalid?" returns opposite value of inner valid state
114
+ # @return [Boolean]
115
+ def invalid?
116
+ !valid?
117
+ end
118
+
87
119
  # "errors" returns errors hash
88
120
  # @return [Hash]
89
121
  def errors
@@ -96,6 +128,7 @@ module Validy
96
128
  # @param [Proc] block
97
129
  def condition(method, error = nil, &block)
98
130
  return self unless valid?
131
+ return self if skip_optional?
99
132
 
100
133
  condition = method.respond_to?(:call) ? method.call : send(method)
101
134
  validate_condition(condition, error, &block)
@@ -109,8 +142,8 @@ module Validy
109
142
  def required(attribute, error = nil, &block)
110
143
  return self unless valid?
111
144
 
112
- @evaluating_attribute = instance_variable_get("@#{attribute}")
113
- validate_condition(@evaluating_attribute, error || "#{attribute} required!", &block)
145
+ @evaluating_attribute_value = instance_variable_get("@#{attribute}")
146
+ validate_condition(@evaluating_attribute_value, error || "#{attribute} required!", &block)
114
147
  self
115
148
  end
116
149
 
@@ -119,7 +152,8 @@ module Validy
119
152
  def optional(attribute)
120
153
  return self unless valid?
121
154
 
122
- @evaluating_attribute = instance_variable_get("@#{attribute}")
155
+ @optional = true
156
+ @evaluating_attribute_value = instance_variable_get("@#{attribute}")
123
157
  self
124
158
  end
125
159
 
@@ -129,14 +163,23 @@ module Validy
129
163
  # @param [Proc] block
130
164
  def type(clazz, error = nil, &block)
131
165
  return self unless valid?
166
+ return self if skip_optional?
132
167
 
133
- validate_condition(@evaluating_attribute&.is_a?(clazz),
134
- error || "`#{@evaluating_attribute}` is not a type #{clazz}", &block)
168
+ validate_condition(
169
+ @evaluating_attribute_value&.is_a?(clazz),
170
+ error || "`#{@evaluating_attribute_value}` is not a type #{clazz}", &block
171
+ )
135
172
  self
136
173
  end
137
174
 
138
175
  private
139
176
 
177
+ def skip_optional?
178
+ return false unless @optional
179
+
180
+ @evaluating_attribute_value.nil?
181
+ end
182
+
140
183
  def method_presented?(method)
141
184
  method_to_symed = method.to_sym
142
185
  methods.any? { |m| m == method_to_symed }
@@ -155,8 +198,8 @@ module Validy
155
198
  def validate_condition(condition, error = nil, &block)
156
199
  return if condition
157
200
 
158
- error_hash = error_hash(error)
159
- add_error(error_hash)
201
+ error_hash = error_hash error
202
+ add_error error_hash
160
203
 
161
204
  block.call if block_given?
162
205
  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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov