tchae 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3223e8b9f142d6df07e3140a7469647fb6b285d4324d8720957ebee438dfdab7
4
- data.tar.gz: f0602b5cb2d128f42d097744299e226c504835d231f099f8cace33566033778c
3
+ metadata.gz: d2ef331e75a9929d29f3348d5fb23316964a25773623eb2d703e802d623e0d92
4
+ data.tar.gz: 34c124e0e86a1400e895370f89cd94cdf200ad9b9a54d75b386fb841ff2e4675
5
5
  SHA512:
6
- metadata.gz: 6354b81ae20b79d584d6c6b34e0cdb293f11f6cfebf8e39c2a7dd90f72401af33c8355acba4945ee33c190322c9e4769c7284145a0103eaec57ad070306b4c21
7
- data.tar.gz: 5fa9f4da97ca1063e73814dd5c54d4848d1ee08eeea055ea2fe0ca16c6752107c92cf913f3f5ef05ffefca4158c901e84e00387b3fa4aedb556cb8b1a6cc945e
6
+ metadata.gz: 23ba34c2410bce14ed5bd1e334057a00bfff4376b6c005a84c56056ad7177c7a2f342db3d71e5dee266ae552ef20c833d37f36e69a77af5f1f19ab00d3b69077
7
+ data.tar.gz: 94512da6a9a16bbafb6ac0be7855e32be3d3ce3bb6eebdcaf345cce820e26f39dd6a828f4ea57a8e4b0dd4d96244401b4b5a8e3bcc16deb50e74016cf001dcb7
@@ -5,7 +5,6 @@ class Object
5
5
  end
6
6
 
7
7
  module Tchae
8
- EMPTY_ARRAY = [].freeze
9
8
 
10
9
  def self.Wrapper(handling = ::Tchae::Handling::RAISE)
11
10
  MethodValidator.new(handling)
@@ -14,8 +13,8 @@ module Tchae
14
13
  # wrap method with validation, and on invalid,
15
14
  # exit with control flow and retun a wrapped error object
16
15
  # on success return a wrapped return object
17
- def self.do_method_wrapping_return(obj, wrapper, tea_methodsymb,
18
- proclmbda, params, kwparms)
16
+ def self.do_validation_return_wrapper(obj, wrapper, tea_methodsymb,
17
+ proclmbda, params, kwparms)
19
18
  unless params.empty?
20
19
  if (err = wrapper.args.positional.valid_or_return_error(params))
21
20
  return Return(nil, err)
@@ -27,6 +26,35 @@ module Tchae
27
26
  end
28
27
  end
29
28
 
29
+ unchecked_result = if proclmbda.arity.zero?
30
+ obj.__send__(tea_methodsymb)
31
+ elsif kwparms.empty?
32
+ obj.__send__(tea_methodsymb, *params)
33
+ elsif params.empty?
34
+ obj.__send__(tea_methodsymb, **kwparms)
35
+ else
36
+ obj.__send__(tea_methodsymb, *params, **kwparms)
37
+ end
38
+
39
+ wrapper.result.return_wrapper unchecked_result
40
+ end
41
+
42
+ # wrap method with validation, and on invalid,
43
+ # exit with control flow and retun a error object
44
+ # on success return the valid result
45
+ def self.do_validation_result_or_error(obj, wrapper, tea_methodsymb,
46
+ proclmbda, params, kwparms)
47
+ unless params.empty?
48
+ if (err = wrapper.args.positional.valid_or_return_error(params))
49
+ return err
50
+ end
51
+ end
52
+ unless kwparms.empty?
53
+ if (err = wrapper.args.keyword.valid_or_return_error(kwparms))
54
+ return err
55
+ end
56
+ end
57
+
30
58
  unchecked_result = if proclmbda.arity.zero?
31
59
  obj.__send__(tea_methodsymb)
32
60
  elsif kwparms.empty?
@@ -43,7 +71,7 @@ module Tchae
43
71
  # wrap method with validation, and on invalid,
44
72
  # exit by raising exception
45
73
 
46
- def self.do_method_wrapping_raise(obj, wrapper, tea_methodsymb,
74
+ def self.do_validation_raise(obj, wrapper, tea_methodsymb,
47
75
  proclmbda, params, kwparms)
48
76
 
49
77
  wrapper.args.positional.valid_or_raise_exception(params)
@@ -153,11 +181,17 @@ module Tchae
153
181
  @valtor.valid_or_raise_exception(result, ::Tchae::ReturnTypeException)
154
182
  end
155
183
 
156
- def valid_or_return_error(result)
184
+ def return_wrapper(result)
157
185
  return Return(result) unless @valtor
158
186
 
159
187
  @valtor.wrapped_result_or_error(result, ::Tchae::ReturnTypeError)
160
188
  end
189
+
190
+ def valid_or_return_error(result)
191
+ return result unless @valtor
192
+ @valtor.valid_or_return_error(result, ::Tchae::ReturnTypeError)
193
+ end
194
+
161
195
  end
162
196
 
163
197
  class PositionalArgs < Array
@@ -171,7 +205,8 @@ module Tchae
171
205
  @wrapper
172
206
  end
173
207
 
174
- def expect(constraints = EMPTY_ARRAY, &proc)
208
+ # def expect(constraints = EMPTY_ARRAY, &proc)
209
+ def expect(*constraints, &proc)
175
210
  constraints.each { |inp| push Tchae(inp) }
176
211
  instance_exec(&proc) if block_given?
177
212
  @wrapper
@@ -247,8 +282,9 @@ module Tchae
247
282
  end
248
283
  module Handling
249
284
  RAISE = :raise_exception
250
- RETURN = :return_wrapper
251
- ALL = [RAISE, RETURN].freeze
285
+ RETURN_WRAPPER = :return_wrapper
286
+ RETURN_RESULT_OR_ERROR = :return_result_or_error
287
+ ALL = [RAISE, RETURN_WRAPPER, RETURN_RESULT_OR_ERROR].freeze
252
288
  end
253
289
  end
254
290
 
@@ -297,8 +333,11 @@ module Tchae
297
333
  lambda
298
334
  end
299
335
 
300
- if wrapper.handling == ::Tchae::Handling::RETURN
301
- create_valid_or_return_method(methodname, wrapper, &proclmbda)
336
+ case wrapper.handling
337
+ when ::Tchae::Handling::RETURN_WRAPPER
338
+ create_return_wrapper_method(methodname, wrapper, &proclmbda)
339
+ when ::Tchae::Handling::RETURN_RESULT_OR_ERROR
340
+ create_result_or_error_method(methodname, wrapper, &proclmbda)
302
341
  else # RAISE
303
342
  create_valid_or_raise_method(methodname, wrapper, &proclmbda)
304
343
  end
@@ -310,7 +349,7 @@ module Tchae
310
349
  tea_methodsymb = "__tea_#{methodname}".to_sym
311
350
  define_method "__tea_#{methodname}", proc
312
351
  define_method(methodname) do |*params, **kwparms|
313
- Tchae.do_method_wrapping_raise(self, wrapper,
352
+ Tchae.do_validation_raise(self, wrapper,
314
353
  tea_methodsymb, proc,
315
354
  params, kwparms)
316
355
  end
@@ -319,14 +358,30 @@ module Tchae
319
358
  end
320
359
  end
321
360
 
322
- def create_valid_or_return_method(methodname,
361
+ def create_result_or_error_method(methodname,
362
+ wrapper= Tchae::NilWrapper,
363
+ &proc)
364
+ if block_given?
365
+ tea_methodsymb = "__tea_#{methodname}".to_sym
366
+ define_method "__tea_#{methodname}", proc
367
+ define_method(methodname) do |*params, **kwparms|
368
+ Tchae.do_validation_result_or_error(self, wrapper,
369
+ tea_methodsymb, proc,
370
+ params, kwparms)
371
+ end
372
+ else
373
+ define_method(methodname) {}
374
+ end
375
+ end
376
+
377
+ def create_return_wrapper_method(methodname,
323
378
  wrapper = Tchae::NilWrapper,
324
379
  &proc)
325
380
  if block_given?
326
381
  tea_methodsymb = "__tea_#{methodname}".to_sym
327
382
  define_method "__tea_#{methodname}", proc
328
383
  define_method(methodname) do |*params, **kwparms|
329
- Tchae.do_method_wrapping_return(self,
384
+ Tchae.do_validation_return_wrapper(self,
330
385
  wrapper,
331
386
  tea_methodsymb,
332
387
  proc,
@@ -353,8 +408,11 @@ module Tchae
353
408
  lambda
354
409
  end
355
410
 
356
- if wrapper.handling == ::Tchae::Handling::RETURN
357
- create_valid_or_return_singleton_method(methodname, wrapper, &proclmbda)
411
+ case wrapper.handling
412
+ when ::Tchae::Handling::RETURN_WRAPPER
413
+ create_return_wrapper_singleton_method(methodname, wrapper, &proclmbda)
414
+ when ::Tchae::Handling::RETURN_RESULT_OR_ERROR
415
+ create_result_or_error_singleton_method(methodname, wrapper, &proclmbda)
358
416
  else # RAISE
359
417
  create_valid_or_raise_singleton_method(methodname, wrapper, &proclmbda)
360
418
  end
@@ -366,7 +424,7 @@ module Tchae
366
424
  tea_methodsymb = "__tea_#{methodname}".to_sym
367
425
  define_singleton_method "__tea_#{methodname}", proc
368
426
  define_singleton_method(methodname) do |*params, **kwparms|
369
- Tchae.do_method_wrapping_raise(self, wrapper,
427
+ Tchae.do_validation_raise(self, wrapper,
370
428
  tea_methodsymb, proc,
371
429
  params, kwparms)
372
430
  end
@@ -374,14 +432,30 @@ module Tchae
374
432
  define_singleton_method(methodname) {}
375
433
  end
376
434
  end
377
-
378
- def create_valid_or_return_singleton_method(methodname,
435
+
436
+ def create_result_or_error_singleton_method(methodname,
437
+ wrapper= Tchae::NilWrapper,
438
+ &proc)
439
+ if block_given?
440
+ tea_methodsymb = "__tea_#{methodname}".to_sym
441
+ define_singleton_method "__tea_#{methodname}", proc
442
+ define_singleton_method(methodname) do |*params, **kwparms|
443
+ Tchae.do_validation_result_or_error(self, wrapper,
444
+ tea_methodsymb, proc,
445
+ params, kwparms)
446
+ end
447
+ else
448
+ define_singleton_method(methodname) {}
449
+ end
450
+ end
451
+
452
+ def create_return_wrapper_singleton_method(methodname,
379
453
  wrapper = Tchae::NilWrapper, &proc)
380
454
  if block_given?
381
455
  tea_methodsymb = "__tea_#{methodname}".to_sym
382
456
  define_singleton_method "__tea_#{methodname}", proc
383
457
  define_singleton_method(methodname) do |*params, **kwparms|
384
- Tchae.do_method_wrapping_return(self, wrapper,
458
+ Tchae.do_validation_return_wrapper(self, wrapper,
385
459
  tea_methodsymb, proc,
386
460
  params, kwparms)
387
461
  end
@@ -396,6 +470,7 @@ end
396
470
  def Return(result, error = nil)
397
471
  Tchae::ResultWrapper.new(result, error)
398
472
  end
473
+
399
474
  class Object
400
475
  extend Tchae::PI
401
476
  include Tchae::PISingleton
@@ -1,3 +1,3 @@
1
1
  module Tchae
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tchae
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - D.M.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-30 00:00:00.000000000 Z
11
+ date: 2020-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,7 +38,9 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.51'
41
- description: Tchae is a lightweight ruby validation library
41
+ description: |-
42
+ Having to validate method parameters and/or the result of method is a
43
+ quite common and repetitive task. Tchae is an attempt to capture this task in a small yet generic reuseable library. Tchae provides validated variants of the standard define_method ruby methods , that will create the same method as the standard would create wrapped into validation code.
42
44
  email: dev@aithscel.eu
43
45
  executables: []
44
46
  extensions: []