validation 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b38597b7b728c0c9c06b6b93e71ecfc1e0218f650b6a1f540745c98ff7f71bc
4
- data.tar.gz: 0d61a0ce621e272d43965b716170d4a785f76cf586c705d67c57e7c934ba26d2
3
+ metadata.gz: 9ae8e75f29800d00d3e5bc5c33b74e50daa2ea08bb5df22d087d986bfc9ab955
4
+ data.tar.gz: fe7268f6cfe24e14fa245971d89259904278b1a07f03caa5fa50e534ff63a631
5
5
  SHA512:
6
- metadata.gz: 58bf4308da4efbf1aea2e69b679bed4266f148c85cbdaff9151d75c7973b5fe3aca21227aa567d4b7c7cbed001fc59b86b9d22f93c24d3bce647e09fada8e493
7
- data.tar.gz: 472355abb349e3f322d71d242911b3f5513105ecc3bbca2a5b01e73912b2b7d12370471b01cd6b98066548114d7a5e861468cef80389902fc45bc5aa05be4a70
6
+ metadata.gz: f6253eebfaca57e91b6acedbb0e523bd06050bf827f517a05904a9e3ea0ce6f3420a440bd09e7d79b93cf60d023183e31b4c64ff2761689957634c54e8f593ad
7
+ data.tar.gz: '093a3665ebfba09ff5d85dbf3c150ea54e56dc2d3f76d1e4dec9409fcd130c8425b3c464cce38881be30231bcdb1d43c56fdf5ce251b8d73064d4dd3a5ea6445'
data/README.md CHANGED
@@ -12,7 +12,7 @@ Require Ruby 2.6 or later
12
12
  Add below code into your Gemfile
13
13
 
14
14
  ```ruby
15
- gem 'validation', '>= 0.2.2', '< 0.3.0'
15
+ gem 'validation', '>= 0.3.0', '< 0.4.0'
16
16
  ```
17
17
 
18
18
  ### Overview
@@ -47,6 +47,8 @@ person.age = 12.9
47
47
  person.age #=> 12
48
48
  ```
49
49
 
50
+ The pattern builder DSL is just using [eqq](https://github.com/kachick/eqq)
51
+
50
52
  ## Links
51
53
 
52
54
  * [Repository](https://github.com/kachick/validation)
data/lib/validation.rb CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  # Copyright (c) 2011-2012 Kenichi Kamiya
5
5
 
6
+ require 'eqq'
7
+
6
8
  module Validation
7
9
  end
8
10
 
@@ -23,20 +23,20 @@ module Validation
23
23
  end
24
24
 
25
25
  # Adjuster Builders
26
- # Apply adjuster when passed condition.
27
- # @param condition [Proc, Method, #===]
26
+ # Apply adjuster when passed pattern.
27
+ # @param pattern [Proc, Method, #===]
28
28
  # @param adjuster [Proc, #to_proc]
29
29
  # @return [lambda]
30
- def WHEN(condition, adjuster)
31
- unless Validation.conditionable?(condition)
32
- raise TypeError, 'wrong object for condition'
30
+ def WHEN(pattern, adjuster)
31
+ unless Eqq.valid?(pattern)
32
+ raise TypeError, 'wrong object for pattern'
33
33
  end
34
34
 
35
35
  unless Validation.adjustable?(adjuster)
36
36
  raise TypeError, 'wrong object for adjuster'
37
37
  end
38
38
 
39
- ->v { _valid?(condition, v) ? adjuster.call(v) : v }
39
+ ->v { _valid?(pattern, v) ? adjuster.call(v) : v }
40
40
  end
41
41
 
42
42
  # Sequential apply all adjusters.
@@ -3,7 +3,6 @@
3
3
 
4
4
  require_relative 'version'
5
5
  require_relative 'errors'
6
- require_relative 'condition'
7
6
  require_relative 'adjustment'
8
7
  require_relative 'validatable'
9
8
  require_relative 'singleton_class'
@@ -4,10 +4,6 @@
4
4
  module Validation
5
5
  module_function
6
6
 
7
- def conditionable?(object)
8
- Condition.__send__(__callee__, object)
9
- end
10
-
11
7
  def adjustable?(object)
12
8
  Adjustment.__send__(__callee__, object)
13
9
  end
@@ -7,7 +7,7 @@ module Validation
7
7
 
8
8
  def included(mod)
9
9
  mod.module_eval do
10
- extend(Condition)
10
+ extend(Eqq::Buildable)
11
11
  extend(Adjustment)
12
12
  extend(Validatable::ClassMethods)
13
13
  include(Validatable)
@@ -14,17 +14,17 @@ module Validation
14
14
  module Validatable
15
15
  private
16
16
 
17
- # @param [Proc, Method, #===] condition
17
+ # @param [Proc, Method, #===] pattern
18
18
  # @param [Object] value
19
- def _valid?(condition, value)
19
+ def _valid?(pattern, value)
20
20
  !!(
21
- case condition
21
+ case pattern
22
22
  when Proc
23
- instance_exec(value, &condition)
23
+ instance_exec(value, &pattern)
24
24
  when Method
25
- condition.call(value)
25
+ pattern.call(value)
26
26
  else
27
- condition === value
27
+ pattern === value
28
28
  end
29
29
  )
30
30
  end
@@ -5,9 +5,9 @@ module Validation
5
5
  module Validatable
6
6
  module ClassMethods
7
7
  # @param name [Symbol, String]
8
- # @param condition [Proc, Method, #===]
8
+ # @param pattern [Proc, Method, #===]
9
9
  # @return [Symbol]
10
- def attr_reader_with_validation(name, condition)
10
+ def attr_reader_with_validation(name, pattern)
11
11
  define_method(name) do
12
12
  ivar = :"@#{name}"
13
13
  unless instance_variable_defined?(ivar)
@@ -16,7 +16,7 @@ module Validation
16
16
 
17
17
  value = instance_variable_get(ivar)
18
18
 
19
- unless _valid?(condition, value)
19
+ unless _valid?(pattern, value)
20
20
  raise InvalidReadingError,
21
21
  "#{value.inspect} is deficient for #{name} in #{self.class}"
22
22
  end
@@ -26,9 +26,9 @@ module Validation
26
26
  end
27
27
 
28
28
  # @param name [Symbol, String]
29
- # @param condition [Proc, Method, #===]
29
+ # @param pattern [Proc, Method, #===]
30
30
  # @return [Symbol]
31
- def attr_writer_with_validation(name, condition, &adjuster)
31
+ def attr_writer_with_validation(name, pattern, &adjuster)
32
32
  if adjuster
33
33
  adjustment = true
34
34
  end
@@ -44,7 +44,7 @@ module Validation
44
44
  end
45
45
  end
46
46
 
47
- if _valid?(condition, value)
47
+ if _valid?(pattern, value)
48
48
  instance_variable_set(:"@#{name}", value)
49
49
  else
50
50
  raise InvalidWritingError,
@@ -54,14 +54,14 @@ module Validation
54
54
  end
55
55
 
56
56
  # @param name [Symbol, String]
57
- # @param condition [Proc, Method, #===]
57
+ # @param pattern [Proc, Method, #===]
58
58
  # @param [Boolean] reader_validation
59
59
  # @param [Boolean] writer_validation
60
60
  # @return [Array<Symbol>]
61
- def attr_accessor_with_validation(name, condition, writer_validation: true, reader_validation: true, &adjuster)
61
+ def attr_accessor_with_validation(name, pattern, writer_validation: true, reader_validation: true, &adjuster)
62
62
  reader_name = (
63
63
  if reader_validation
64
- attr_reader_with_validation(name, condition)
64
+ attr_reader_with_validation(name, pattern)
65
65
  else
66
66
  attr_reader(name)
67
67
  end
@@ -69,7 +69,7 @@ module Validation
69
69
 
70
70
  writer_name = (
71
71
  if writer_validation
72
- attr_writer_with_validation(name, condition, &adjuster)
72
+ attr_writer_with_validation(name, pattern, &adjuster)
73
73
  else
74
74
  attr_writer(name)
75
75
  end
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Validation
5
- VERSION = '0.2.2'
5
+ VERSION = '0.3.0'
6
6
  end
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenichi Kamiya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-02 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eqq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.5
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.5
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.0
13
33
  description: " Validation in Ruby objects\n"
14
34
  email:
15
35
  - kachick1+ruby@gmail.com
@@ -22,7 +42,6 @@ files:
22
42
  - lib/validation.rb
23
43
  - lib/validation/adjustment.rb
24
44
  - lib/validation/bootstrap.rb
25
- - lib/validation/condition.rb
26
45
  - lib/validation/errors.rb
27
46
  - lib/validation/module_functions.rb
28
47
  - lib/validation/singleton_class.rb
@@ -1,283 +0,0 @@
1
- # coding: us-ascii
2
- # frozen_string_literal: true
3
-
4
- module Validation
5
- module Condition
6
- module_function
7
-
8
- # @group Support Building Conditions
9
-
10
- # true if object is sufficient for condition.
11
- # @param [Object] object
12
- def conditionable?(object)
13
- case object
14
- when Proc, Method
15
- object.arity == 1
16
- else
17
- object.respond_to?(:===)
18
- end
19
- end
20
-
21
- # Condition Builders
22
- # A inner method for some condition builders.
23
- # For build conditions AND, NAND, OR, NOR, XOR, XNOR.
24
- # @param delegated [Symbol]
25
- # @return [lambda]
26
- def _logical_operator(delegated, *conditions)
27
- unless conditions.all? { |c| conditionable?(c) }
28
- raise TypeError, 'wrong object for condition'
29
- end
30
-
31
- ->v {
32
- conditions.__send__(delegated) { |condition|
33
- _valid?(condition, v)
34
- }
35
- }
36
- end
37
-
38
- class << self
39
- private :_logical_operator
40
- end
41
-
42
- # A condition builder.
43
- # @param cond1 [Proc, Method, #===]
44
- # @param cond2 [Proc, Method, #===]
45
- # @param conds [Proc, Method, #===]
46
- # @return [lambda]
47
- # this lambda return true if match all conditions
48
- def AND(cond1, cond2, *conds)
49
- _logical_operator(:all?, cond1, cond2, *conds)
50
- end
51
-
52
- # A condition builder.
53
- # @param cond1 [Proc, Method, #===]
54
- # @param cond2 [Proc, Method, #===]
55
- # @param conds [Proc, Method, #===]
56
- # @return [lambda]
57
- def NAND(cond1, cond2, *conds)
58
- NOT(AND(cond1, cond2, *conds))
59
- end
60
-
61
- # A condition builder.
62
- # @param cond1 [Proc, Method, #===]
63
- # @param cond2 [Proc, Method, #===]
64
- # @param conds [Proc, Method, #===]
65
- # @return [lambda]
66
- # this lambda return true if match a any condition
67
- def OR(cond1, cond2, *conds)
68
- _logical_operator(:any?, cond1, cond2, *conds)
69
- end
70
-
71
- # A condition builder.
72
- # @param cond1 [Proc, Method, #===]
73
- # @param cond2 [Proc, Method, #===]
74
- # @param conds [Proc, Method, #===]
75
- # @return [lambda]
76
- def NOR(cond1, cond2, *conds)
77
- NOT(OR(cond1, cond2, *conds))
78
- end
79
-
80
- # A condition builder.
81
- # @param cond1 [Proc, Method, #===]
82
- # @param cond2 [Proc, Method, #===]
83
- # @param conds [Proc, Method, #===]
84
- # @return [lambda]
85
- def XOR(cond1, cond2, *conds)
86
- _logical_operator(:one?, cond1, cond2, *conds)
87
- end
88
-
89
- # A condition builder.
90
- # @param cond1 [Proc, Method, #===]
91
- # @param cond2 [Proc, Method, #===]
92
- # @param conds [Proc, Method, #===]
93
- # @return [lambda]
94
- def XNOR(cond1, cond2, *conds)
95
- NOT(XOR(cond1, cond2, *conds))
96
- end
97
-
98
- # A condition builder.
99
- # @param condition [Proc, Method, #===]
100
- # @return [lambda] A condition invert the original condition.
101
- def NOT(condition)
102
- unless conditionable?(condition)
103
- raise TypeError, 'wrong object for condition'
104
- end
105
-
106
- ->v { !_valid?(condition, v) }
107
- end
108
-
109
- # A condition builder.
110
- # @param obj [#==]
111
- # @return [lambda]
112
- # this lambda return true if a argument match under #== method
113
- def EQ(obj)
114
- ->v { obj == v }
115
- end
116
-
117
- # A condition builder.
118
- # @param obj [#equal?]
119
- # @return [lambda]
120
- # this lambda return true if a argument match under #equal? method
121
- def EQUAL(obj)
122
- ->v { obj.equal?(v) }
123
- end
124
-
125
- alias_method :SAME, :EQUAL
126
- module_function :SAME
127
-
128
- # A condition builder.
129
- # @param messages [Symbol, String]
130
- # @return [lambda]
131
- # this lambda return true if a argument respond to all messages
132
- def CAN(message1, *messages)
133
- messages = [message1, *messages]
134
- unless messages.all? { |s|
135
- [Symbol, String].any? { |klass| s.kind_of?(klass) }
136
- }
137
- raise TypeError, 'only Symbol or String for message'
138
- end
139
-
140
- ->v {
141
- messages.all? { |message| v.respond_to?(message) }
142
- }
143
- end
144
-
145
- # A condition builder.
146
- # @param condition [Proc, Method, #===]
147
- # @param conditions [Proc, Method, #===]
148
- # @return [lambda]
149
- # this lambda return true
150
- # if face no exception when a argument checking under all conditions
151
- def QUIET(condition, *conditions)
152
- conditions = [condition, *conditions]
153
- unless conditions.all? { |c| conditionable?(c) }
154
- raise TypeError, 'wrong object for condition'
155
- end
156
-
157
- ->v {
158
- conditions.all? { |cond|
159
- begin
160
- _valid?(cond, v)
161
- rescue Exception
162
- false
163
- else
164
- true
165
- end
166
- }
167
- }
168
- end
169
-
170
- # A condition builder.
171
- # @param exception [Exception]
172
- # @param exceptions [Exception]
173
- # @return [lambda]
174
- # this lambda return true
175
- # if catch any kindly exceptions when a argument checking in a block parameter
176
- def RESCUE(exception, *exceptions, &condition)
177
- exceptions = [exception, *exceptions]
178
- raise ArgumentError unless conditionable?(condition)
179
- raise TypeError unless exceptions.all? { |e| e.ancestors.include?(Exception) }
180
-
181
- ->v {
182
- begin
183
- _valid?(condition, v)
184
- false
185
- rescue *exceptions
186
- true
187
- rescue Exception
188
- false
189
- end
190
- }
191
- end
192
-
193
- # A condition builder.
194
- # @param exception [Exception]
195
- # @return [lambda]
196
- # this lambda return true
197
- # if catch a specific exception when a argument checking in a block parameter
198
- def CATCH(exception, &condition)
199
- raise ArgumentError unless conditionable?(condition)
200
- raise TypeError, 'not error object' unless exception.ancestors.include?(Exception)
201
-
202
- ->v {
203
- begin
204
- _valid?(condition, v)
205
- rescue Exception => err
206
- err.instance_of?(exception)
207
- else
208
- false
209
- end
210
- }
211
- end
212
-
213
- # A condition builder.
214
- # @param condition [Proc, Method, #===]
215
- # @param conditions [Proc, Method, #===]
216
- # @return [lambda]
217
- # this lambda return true
218
- # if all included objects match all conditions
219
- def ALL(condition, *conditions)
220
- conditions = [condition, *conditions]
221
- unless conditions.all? { |c| conditionable?(c) }
222
- raise TypeError, 'wrong object for condition'
223
- end
224
-
225
- ->list {
226
- enum = (
227
- (list.respond_to?(:each_value) && list.each_value) or
228
- (list.respond_to?(:all?) && list) or
229
- (list.respond_to?(:each) && list.each) or
230
- return false
231
- )
232
-
233
- conditions.all? { |cond|
234
- enum.all? { |v|
235
- _valid?(cond, v)
236
- }
237
- }
238
- }
239
- end
240
-
241
- # A condition builder.
242
- # @param list [#all?]
243
- # @param lists [#all?]
244
- # @return [lambda]
245
- # this lambda return true
246
- # if all lists including the object
247
- def MEMBER_OF(list, *lists)
248
- lists = [list, *lists]
249
- unless lists.all? { |l| l.respond_to?(:all?) }
250
- raise TypeError, 'list must respond #all?'
251
- end
252
-
253
- ->v {
254
- lists.all? { |l| l.include?(v) }
255
- }
256
- end
257
-
258
- # @endgroup
259
-
260
- # @group Useful Conditions
261
- BOOLEAN = ->v { v.equal?(true) || v.equal?(false) }
262
- STRINGABLE = OR(String, CAN(:to_str))
263
-
264
- def anything
265
- # BasicObject.=== always passing
266
- BasicObject
267
- end
268
-
269
- # A getter for a useful condition.
270
- # @return [BOOLEAN] "true or false"
271
- def boolean
272
- BOOLEAN
273
- end
274
-
275
- # A getter for a useful condition.
276
- # @return [STRINGABLE] check "looks string family"
277
- def stringable
278
- STRINGABLE
279
- end
280
-
281
- # @endgroup
282
- end
283
- end