attribeauty 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/lib/attribeauty/params.rb +19 -16
- data/lib/attribeauty/types/boolean.rb +2 -0
- data/lib/attribeauty/types/float.rb +2 -0
- data/lib/attribeauty/types/integer.rb +2 -0
- data/lib/attribeauty/types/string.rb +2 -0
- data/lib/attribeauty/types/time.rb +2 -0
- data/lib/attribeauty/validator.rb +51 -26
- data/lib/attribeauty/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 812191110b6c7f397737c3a9b1cceab66819c07d549c02c31687b8260342f404
|
4
|
+
data.tar.gz: 2ce7d61531ba7dcb7a896994ef94a2fa970bd821eb62d0901cc2b1df5e70947b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbe0d664e43e995eb4caf66f66fe2a242d2f40b9effda38f511775451c666c82e1596a9116197e5cc84848f0f63ab11f2bb0f537f2214e0d38c6f9b3ddba2e1f
|
7
|
+
data.tar.gz: 66fb433c37a8986c4f5cf3bec0dae55246360fb6f65d82e2e25d1f507b3f4526d5476c99fa7f3e5244d6d255cdb9f429c51e7a7c7c84504e61fe2083e864bf0b
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/lib/attribeauty/params.rb
CHANGED
@@ -44,31 +44,18 @@ module Attribeauty
|
|
44
44
|
yield
|
45
45
|
end
|
46
46
|
|
47
|
-
# rubocop:disable Naming::BlockForwarding
|
48
47
|
def attribute(name, type = nil, **args, &block)
|
49
48
|
value = request_params[name]
|
50
49
|
return if value.nil? && args[:required].nil?
|
51
50
|
|
52
51
|
if block_given?
|
53
|
-
@to_h[name] =
|
54
|
-
if value.is_a?(Array)
|
55
|
-
value.map do |val|
|
56
|
-
params = self.class.with(val).accept(&block)
|
57
|
-
@errors.push(*params.errors)
|
58
|
-
params
|
59
|
-
end.reject(&:empty?)
|
60
|
-
else
|
61
|
-
params = self.class.with(value).accept(&block)
|
62
|
-
@errors.push(*params.errors)
|
63
|
-
params
|
64
|
-
end
|
52
|
+
@to_h[name.to_sym] = vals_from_nested(value, &block)
|
65
53
|
else
|
66
|
-
validator = Validator.run(name,
|
67
|
-
@to_h[name.to_sym] = validator.value if validator.valid?
|
54
|
+
validator = Validator.run(name, value, type, **args)
|
68
55
|
@errors.push(*validator.errors)
|
56
|
+
@to_h[name.to_sym] = validator.value if validator.valid?
|
69
57
|
end
|
70
58
|
end
|
71
|
-
# rubocop:enable Naming::BlockForwarding
|
72
59
|
|
73
60
|
def inspect
|
74
61
|
to_h.inspect
|
@@ -81,5 +68,21 @@ module Attribeauty
|
|
81
68
|
def strict?
|
82
69
|
strict
|
83
70
|
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def vals_from_nested(value, &block)
|
75
|
+
if value.is_a?(Array)
|
76
|
+
value.map do |val|
|
77
|
+
params = self.class.with(val).accept(&block)
|
78
|
+
@errors.push(*params.errors)
|
79
|
+
params.to_h
|
80
|
+
end.reject(&:empty?)
|
81
|
+
else
|
82
|
+
params = self.class.with(value).accept(&block)
|
83
|
+
@errors.push(*params.errors)
|
84
|
+
params.to_h
|
85
|
+
end
|
86
|
+
end
|
84
87
|
end
|
85
88
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "forwardable"
|
4
|
-
|
5
3
|
module Attribeauty
|
6
4
|
class Validator
|
7
5
|
ALLOWS_HASH = {
|
@@ -9,55 +7,82 @@ module Attribeauty
|
|
9
7
|
allow_empty: :empty?
|
10
8
|
}.freeze
|
11
9
|
|
12
|
-
def self.run(name, type,
|
13
|
-
new(name, type,
|
10
|
+
def self.run(name, type, original_val, **args)
|
11
|
+
new(name, type, original_val, **args).run
|
14
12
|
end
|
15
13
|
|
16
|
-
attr_reader :
|
14
|
+
attr_reader :original_val, :errors, :name, :type, :required, :default, :allows, :value, :valid
|
17
15
|
|
18
|
-
def initialize(name,
|
16
|
+
def initialize(name, original_val, type = nil, **args)
|
19
17
|
@name = name
|
20
18
|
@type = type
|
21
|
-
@
|
22
|
-
@errors = []
|
19
|
+
@original_val = original_val
|
23
20
|
@default = args[:default]
|
24
|
-
@required = args[:required] if
|
25
|
-
allows = args.slice(*allows_array)
|
26
|
-
return if allows.empty?
|
21
|
+
@required = args[:required] if args[:required] == true
|
22
|
+
@allows = args.slice(*allows_array).delete_if { |_key, value| value == true }
|
27
23
|
|
28
|
-
|
29
|
-
|
30
|
-
@predicate = predicate_array
|
24
|
+
@valid = true
|
25
|
+
@errors = []
|
31
26
|
end
|
32
27
|
|
33
28
|
def run
|
34
|
-
|
35
|
-
|
29
|
+
if type.nil?
|
30
|
+
@value = original_val
|
31
|
+
else
|
32
|
+
set_default
|
33
|
+
cast_value
|
34
|
+
handle_missing_required
|
35
|
+
handle_predicates
|
36
|
+
end
|
36
37
|
|
37
38
|
self
|
38
39
|
end
|
39
40
|
|
40
41
|
def valid?
|
41
|
-
|
42
|
-
|
43
|
-
return false
|
44
|
-
end
|
45
|
-
return true if predicate.nil?
|
42
|
+
valid
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
return true if bool
|
45
|
+
private
|
49
46
|
|
50
|
-
|
47
|
+
def set_default
|
48
|
+
return unless original_val.nil? && !default.nil?
|
49
|
+
|
50
|
+
@original_val = default
|
51
51
|
end
|
52
52
|
|
53
|
-
|
53
|
+
def cast_value
|
54
|
+
@value = TypeCaster.run(original_val, type)
|
55
|
+
end
|
56
|
+
|
57
|
+
# only returning errors if required is missing, not if nil?, or :empty?
|
58
|
+
def handle_missing_required
|
59
|
+
return unless required? && original_val.nil?
|
60
|
+
|
61
|
+
errors << "#{name} required"
|
62
|
+
@valid = false
|
63
|
+
end
|
64
|
+
|
65
|
+
def handle_predicates
|
66
|
+
return if predicate.nil? || !valid?
|
54
67
|
|
55
|
-
|
68
|
+
@valid = !value.public_send(predicate)
|
69
|
+
end
|
56
70
|
|
57
71
|
def allows_array
|
58
72
|
ALLOWS_HASH.keys
|
59
73
|
end
|
60
74
|
|
75
|
+
# convert allow_nil -> :nil? or allow_empty -> :empty?
|
76
|
+
# this will be used to public_send
|
77
|
+
# NOTE: only one will be checked, if you pass both:
|
78
|
+
# allow_nil and allow_empty, one will be ignored
|
79
|
+
def predicate
|
80
|
+
return if allows.empty?
|
81
|
+
|
82
|
+
key = allows.keys.first
|
83
|
+
ALLOWS_HASH[key]
|
84
|
+
end
|
85
|
+
|
61
86
|
def required?
|
62
87
|
required
|
63
88
|
end
|
data/lib/attribeauty/version.rb
CHANGED