validy 1.1.4 → 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 +41 -13
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5ba15eaa85e57c3dad933c2ec2529bedab3b4df3c7ee872fa2b6e76ec84c48f
4
- data.tar.gz: 6b2c4411763c4a9759c90297700b82d28a133199eeba688df55b3aba45a107ec
3
+ metadata.gz: ae1ac788467cb913065937d6a6d46707f3e4b6806ab54f0d7066f4e14b5eae50
4
+ data.tar.gz: 01ecb46cd5f4049055c096104d66378cd52588459b433758ae39615cef46d802
5
5
  SHA512:
6
- metadata.gz: 28042c029f3d322ff70540f5ebe5cbe5a8121463f820320bab838823e0882028ccae920536a2f0429430d9866e8f7396beedc2d0b0fd1bbab717bf98856d88b8
7
- data.tar.gz: 6badd77f876d0803f286c2bc11be71499075b481885a14761e4c08c5a6df733ab99064adf4b22dcd3650f4090d6b8179aa8cce3ed80291d9b8e12f38a6d591ca
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_value = 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
45
+
46
+ private
47
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
@@ -139,8 +165,10 @@ module Validy
139
165
  return self unless valid?
140
166
  return self if skip_optional?
141
167
 
142
- validate_condition(@evaluating_attribute_value&.is_a?(clazz),
143
- error || "`#{@evaluating_attribute_value}` 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
+ )
144
172
  self
145
173
  end
146
174
 
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.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov