validate-rb 1.0.0.alpha.2 → 1.0.0.alpha.3

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: 6cbdd9ab7995b1b5affe50fce32d2f32fd3d49d12fc03484cbc934d312e4a15f
4
- data.tar.gz: 593628eaacb2eed4d85d991fd0045395c82b43a28801b68801b065b62708b0f4
3
+ metadata.gz: bbb3ff4387fdcdf89831a255462ce6e01c647a3ac443d85b48d7eb4b61337f71
4
+ data.tar.gz: 1d8d0e388222fa64c92483293ff2ac15cc409fdb118bebfb807996e879c43f60
5
5
  SHA512:
6
- metadata.gz: 38f7c973117e67ad6fe86fa0dcd2f2c5edce5263ab884f2153a679f00b453336668167992a16e540c03905fedf2b0710a8dc035bfcd88f26c31c687153d9900f
7
- data.tar.gz: 74fd63f44829727fb8c5360b3a9675c5aefc7e61829d6441addf7430a86c42177974f327ac9468285de84e4b36220594eb54709ac1802b1295ca355078b33cdd
6
+ metadata.gz: 98bc8e416ee5c8e8b5d018cb5fe7a667aead58fc8ff27d818ef9129074f1f1d36f74807a00e6704fdac4311b30b1c06c98b1a61951d099ac75b9b7bd0a9e17e0
7
+ data.tar.gz: b1ce371355794068bb406a1721e4baa9d75d3101b51f915381a8866c8e44a53ed9b25f708a285ac13ee3c7c3ee961be0d9f872664dca95cc8bba6e51d3ee4a16
@@ -3,32 +3,12 @@ module Validate
3
3
  module ClassMethods
4
4
  def method_added(method_name)
5
5
  super
6
- return if @args.empty?
7
-
8
- method = instance_method(method_name)
9
- guard = ArgumentsGuard.new(method, @args.dup)
10
-
11
- @methods_guard.__send__(:define_method, method_name) do |*args, &block|
12
- guard.send(method_name, *args, &block)
13
- super(*args, &block)
14
- end
15
- ensure
16
- @args.clear
6
+ guard_method(instance_method(method_name), @methods_guard)
17
7
  end
18
8
 
19
9
  def singleton_method_added(method_name)
20
10
  super
21
- return if @args.empty?
22
-
23
- method = singleton_method(method_name)
24
- guard = ArgumentsGuard.new(method, @args.dup)
25
-
26
- @methods_guard.__send__(:define_singleton_method, method_name) do |*args, &block|
27
- guard.send(method_name, *args, &block)
28
- super(*args, &block)
29
- end
30
- ensure
31
- @args.clear
11
+ guard_method(singleton_method(method_name), @singleton_methods_guard)
32
12
  end
33
13
 
34
14
  def arg(name, &body)
@@ -39,6 +19,24 @@ module Validate
39
19
  @args[name] = Assertions.create(&body)
40
20
  self
41
21
  end
22
+
23
+ private
24
+
25
+ def guard_method(method, guard_module)
26
+ return if @args.empty?
27
+ guard = ArgumentsGuard.new(method, @args)
28
+ guard_module.__send__(:define_method, method.name) do |*args, **kwargs, &block|
29
+ if kwargs.empty?
30
+ guard.enforce!(*args, &block)
31
+ super(*args, &block)
32
+ else
33
+ guard.enforce!(*args, **kwargs, &block)
34
+ super(*args, **kwargs, &block)
35
+ end
36
+ end
37
+ ensure
38
+ @args = {}
39
+ end
42
40
  end
43
41
 
44
42
  def self.included(base)
@@ -46,6 +44,7 @@ module Validate
46
44
  base.instance_exec do
47
45
  @args = {}
48
46
  prepend(@methods_guard = Module.new)
47
+ singleton_class.prepend(@singleton_methods_guard = Module.new)
49
48
  end
50
49
  end
51
50
 
@@ -73,17 +72,19 @@ module Validate
73
72
  when :block
74
73
  signature << "&#{name}"
75
74
  else
76
- raise Error::ArgumentError, "unsupported parameter type #{kind}"
75
+ raise Error::ArgumentError,
76
+ "unsupported parameter type #{kind}"
77
77
  end
78
78
  next unless rules.include?(name)
79
79
 
80
- assertions << "@rules[:#{name}].assert(#{name}, message: 'invalid argument #{name}')"
80
+ assertions <<
81
+ "@rules[:#{name}].assert(#{name}, message: '#{name}') unless #{name}.eql?(DEFAULT_VALUE)"
81
82
  end
82
83
 
83
- singleton_class.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
84
- def #{method.name}(#{signature.join(', ')})
85
- #{assertions.join("\n ")}
86
- end
84
+ singleton_class.class_eval(<<~RUBY, __FILE__, __LINE__)
85
+ def enforce!(#{signature.join(', ')})
86
+ #{assertions.join("\n ")}
87
+ end
87
88
  RUBY
88
89
 
89
90
  @rules = rules
@@ -66,8 +66,10 @@ module Validate
66
66
  !@violations.empty?
67
67
  end
68
68
 
69
- def to_err
70
- Error::ConstraintViolationError.new(@violations.freeze)
69
+ def to_err(backtrace = [])
70
+ err = Error::ConstraintViolationError.new(@violations.freeze)
71
+ err.set_backtrace(backtrace)
72
+ err
71
73
  end
72
74
 
73
75
  def merge(other)
@@ -104,6 +106,8 @@ module Validate
104
106
  Path.new(@paths.dup << path)
105
107
  when Path
106
108
  Path.new(@paths.dup << path.to_a)
109
+ else
110
+ raise ArgumentError, 'invalid path'
107
111
  end
108
112
  end
109
113
 
@@ -31,9 +31,9 @@ module Validate
31
31
 
32
32
  def message
33
33
  @violations.group_by(&:path)
34
- .transform_values { |violations| violations.map(&:message) }
35
- .map { |path, messages| "#{path}: #{messages.join(' ')}" }
36
- .join("\n")
34
+ .transform_values { |violations| violations.map(&:message) }
35
+ .map { |path, messages| "#{path}: #{messages.join(', ')}" }
36
+ .join("\n")
37
37
  end
38
38
  end
39
39
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Validate
4
- VERSION = '1.0.0.alpha.2'
4
+ VERSION = '1.0.0.alpha.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha.2
4
+ version: 1.0.0.alpha.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bulat Shakirzyanov