attribeauty 0.4.0 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +37 -0
- data/lib/attribeauty/params.rb +17 -14
- data/lib/attribeauty/validator.rb +35 -21
- data/lib/attribeauty/version.rb +1 -1
- data/lib/attribeauty.rb +1 -0
- 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: b78499787a41322da17eca60e3a8af5c5d55b081aaefc6eb8bef788e60368c78
|
4
|
+
data.tar.gz: e660efa7ce1508a9d322256b7a3e598bce5e1e8a778dc7da2a39fa58166cfe20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06471cfb5f5f3f76dcc9bf09545a7c976577b893c08562aa2ecea98f42a3a13c0be5df26ca00e187f81eccaf33728ae41cf5b90226df9c29f113faa550865b21
|
7
|
+
data.tar.gz: 3ec534f72b7977af852f649489a7b8265d5a055171f0130c25e8887e796368bd1998a40c747bfdd7bff413c5acc184c46fc331f14b4b091ded2fc9785503f141
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -191,6 +191,43 @@ end
|
|
191
191
|
|
192
192
|
```
|
193
193
|
|
194
|
+
what if you want to require all attributes? If you pass the `required: true` or `exclude_if: :nil?` with the `accept`, it will be applied to all attributes.
|
195
|
+
|
196
|
+
```
|
197
|
+
class MyController
|
198
|
+
def update
|
199
|
+
MyRecord.update(update_params)
|
200
|
+
|
201
|
+
redirect_to index_path
|
202
|
+
end
|
203
|
+
|
204
|
+
private
|
205
|
+
|
206
|
+
# your params look like this:
|
207
|
+
# { user: { profile: [{ address: { street_name: "Main St" } }] } }
|
208
|
+
#
|
209
|
+
def params_filter
|
210
|
+
Attribeauty::Params.with(request.params)
|
211
|
+
end
|
212
|
+
|
213
|
+
# exclude_if and required will be passed onto all attributes
|
214
|
+
#
|
215
|
+
def update_params
|
216
|
+
params_filter.accept exclude_if: :nil?, required: true do
|
217
|
+
container :user do
|
218
|
+
attribute :title, :string,
|
219
|
+
attribute :email do
|
220
|
+
attribute :address, :string
|
221
|
+
attribute :valid, :boolean
|
222
|
+
attribute :ip_address, :string
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
```
|
230
|
+
|
194
231
|
See `test/test_params.rb` for more examples
|
195
232
|
|
196
233
|
|
data/lib/attribeauty/params.rb
CHANGED
@@ -10,15 +10,16 @@ module Attribeauty
|
|
10
10
|
new(request_params)
|
11
11
|
end
|
12
12
|
|
13
|
-
attr_reader :prefix, :request_params, :acceptables, :to_h, :errors, :strict
|
13
|
+
attr_reader :prefix, :request_params, :acceptables, :to_h, :errors, :strict, :default_args
|
14
14
|
|
15
15
|
def initialize(request_params)
|
16
|
-
@request_params = request_params.transform_keys(&:to_sym)
|
16
|
+
@request_params = (request_params || {}).transform_keys(&:to_sym)
|
17
17
|
@to_h = {}
|
18
18
|
@errors = []
|
19
19
|
end
|
20
20
|
|
21
|
-
def accept(&)
|
21
|
+
def accept(**args, &)
|
22
|
+
@default_args = args
|
22
23
|
instance_eval(&)
|
23
24
|
|
24
25
|
raise MissingAttributeError, errors.join(", ") if errors.any? && strict?
|
@@ -26,10 +27,10 @@ module Attribeauty
|
|
26
27
|
self
|
27
28
|
end
|
28
29
|
|
29
|
-
def accept!(&)
|
30
|
+
def accept!(**args, &)
|
30
31
|
@strict = true
|
31
32
|
|
32
|
-
accept(&)
|
33
|
+
accept(**args, &)
|
33
34
|
end
|
34
35
|
|
35
36
|
def to_hash = to_h
|
@@ -38,6 +39,10 @@ module Attribeauty
|
|
38
39
|
to_h[key]
|
39
40
|
end
|
40
41
|
|
42
|
+
def []=(key, value)
|
43
|
+
to_h[key] = value
|
44
|
+
end
|
45
|
+
|
41
46
|
def container(name)
|
42
47
|
@request_params = request_params[name]
|
43
48
|
|
@@ -47,7 +52,6 @@ module Attribeauty
|
|
47
52
|
#
|
48
53
|
def attribute(name, type = nil, **args, &block)
|
49
54
|
value = request_params[name]
|
50
|
-
return if required?(value, **args)
|
51
55
|
return hash_from_nested(name, value, &block) if block_given?
|
52
56
|
|
53
57
|
value_from_validator(name, value, type, **args)
|
@@ -67,29 +71,28 @@ module Attribeauty
|
|
67
71
|
|
68
72
|
private
|
69
73
|
|
70
|
-
def required?(value, **args)
|
71
|
-
value.nil? && args[:required].nil?
|
72
|
-
end
|
73
|
-
|
74
74
|
def value_from_validator(name, value, type, **args)
|
75
|
-
|
75
|
+
merged_args = args.merge(default_args || {})
|
76
|
+
validator = Validator.run(name, value, type, **merged_args)
|
76
77
|
@errors.push(*validator.errors)
|
77
78
|
@to_h[name.to_sym] = validator.value if validator.valid?
|
78
79
|
end
|
79
80
|
|
80
81
|
def hash_from_nested(name, value, &block)
|
81
|
-
|
82
|
+
result =
|
82
83
|
if value.is_a?(Array)
|
83
84
|
value.map do |val|
|
84
|
-
params = self.class.with(val).accept(&block)
|
85
|
+
params = self.class.with(val).accept(**default_args, &block)
|
85
86
|
@errors.push(*params.errors)
|
86
87
|
params.to_h
|
87
88
|
end.reject(&:empty?)
|
88
89
|
else
|
89
|
-
params = self.class.with(value).accept(&block)
|
90
|
+
params = self.class.with(value).accept(**default_args, &block)
|
90
91
|
@errors.push(*params.errors)
|
91
92
|
params.to_h
|
92
93
|
end
|
94
|
+
|
95
|
+
@to_h[name.to_sym] = result unless result.empty?
|
93
96
|
end
|
94
97
|
end
|
95
98
|
end
|
@@ -13,7 +13,8 @@ module Attribeauty
|
|
13
13
|
@type = type
|
14
14
|
@original_val = original_val
|
15
15
|
@default = args[:default]
|
16
|
-
|
16
|
+
args.delete_if { |key, value| key == :required && value == false }
|
17
|
+
@required = args[:required]
|
17
18
|
@excludes = args[:exclude_if]
|
18
19
|
|
19
20
|
@valid = true
|
@@ -21,15 +22,16 @@ module Attribeauty
|
|
21
22
|
end
|
22
23
|
|
23
24
|
def run
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
set_default
|
28
|
-
cast_value
|
29
|
-
handle_missing_required
|
30
|
-
handle_excludes
|
31
|
-
end
|
25
|
+
handle_missing_original_val!
|
26
|
+
handle_missing_required!
|
27
|
+
handle_missing_type
|
32
28
|
|
29
|
+
set_default
|
30
|
+
cast_value
|
31
|
+
handle_excludes
|
32
|
+
|
33
|
+
self
|
34
|
+
rescue ValueInvalidError
|
33
35
|
self
|
34
36
|
end
|
35
37
|
|
@@ -37,24 +39,40 @@ module Attribeauty
|
|
37
39
|
valid
|
38
40
|
end
|
39
41
|
|
40
|
-
|
42
|
+
def required?
|
43
|
+
required
|
44
|
+
end
|
41
45
|
|
42
|
-
|
43
|
-
return unless original_val.nil? && !default.nil?
|
46
|
+
private
|
44
47
|
|
45
|
-
|
46
|
-
|
48
|
+
def handle_missing_original_val!
|
49
|
+
return unless !required? && original_val.nil?
|
47
50
|
|
48
|
-
|
49
|
-
|
51
|
+
@valid = false
|
52
|
+
raise ValueInvalidError
|
50
53
|
end
|
51
54
|
|
52
55
|
# only returning errors if required is missing, not if nil?, or :empty?
|
53
|
-
def handle_missing_required
|
56
|
+
def handle_missing_required!
|
54
57
|
return unless required? && original_val.nil?
|
55
58
|
|
56
59
|
errors << "#{name} required"
|
57
60
|
@valid = false
|
61
|
+
raise ValueInvalidError
|
62
|
+
end
|
63
|
+
|
64
|
+
def handle_missing_type
|
65
|
+
@value = original_val if type.nil?
|
66
|
+
end
|
67
|
+
|
68
|
+
def set_default
|
69
|
+
return unless original_val.nil? && !default.nil?
|
70
|
+
|
71
|
+
@original_val = default
|
72
|
+
end
|
73
|
+
|
74
|
+
def cast_value
|
75
|
+
@value ||= TypeCaster.run(original_val, type)
|
58
76
|
end
|
59
77
|
|
60
78
|
def handle_excludes
|
@@ -62,9 +80,5 @@ module Attribeauty
|
|
62
80
|
|
63
81
|
@valid = ![*excludes].flatten.any? { |exclude| value.public_send(exclude) }
|
64
82
|
end
|
65
|
-
|
66
|
-
def required?
|
67
|
-
required
|
68
|
-
end
|
69
83
|
end
|
70
84
|
end
|
data/lib/attribeauty/version.rb
CHANGED
data/lib/attribeauty.rb
CHANGED