attribeauty 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +37 -0
- data/lib/attribeauty/params.rb +13 -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: a594dd2b01526d1b89bea9ceeea3217bf0cb311b236981be2e77bd03dc1b5b2f
|
4
|
+
data.tar.gz: 4f79df9cca230c2c0c29c34746f381818535095383c54902e8a7b90f8ea205de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ab16692f1f34715726b3777889c5ee4f1797eeff0431ce73a116c7e02c5faac34d0453188f2469938b1c98076e172dd37e5b18a5a2c1bff009bb483c067a984
|
7
|
+
data.tar.gz: 78f5e064bc51b85fbb57877ce9bd9a7d3894f7798fdfd7dcfe0dc7989fad2618f3d0358e22467d02351745710b92e81227f6b243a74254e90d377be474b1655a
|
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
|
@@ -47,7 +48,6 @@ module Attribeauty
|
|
47
48
|
#
|
48
49
|
def attribute(name, type = nil, **args, &block)
|
49
50
|
value = request_params[name]
|
50
|
-
return if required?(value, **args)
|
51
51
|
return hash_from_nested(name, value, &block) if block_given?
|
52
52
|
|
53
53
|
value_from_validator(name, value, type, **args)
|
@@ -67,29 +67,28 @@ module Attribeauty
|
|
67
67
|
|
68
68
|
private
|
69
69
|
|
70
|
-
def required?(value, **args)
|
71
|
-
value.nil? && args[:required].nil?
|
72
|
-
end
|
73
|
-
|
74
70
|
def value_from_validator(name, value, type, **args)
|
75
|
-
|
71
|
+
merged_args = args.merge(default_args || {})
|
72
|
+
validator = Validator.run(name, value, type, **merged_args)
|
76
73
|
@errors.push(*validator.errors)
|
77
74
|
@to_h[name.to_sym] = validator.value if validator.valid?
|
78
75
|
end
|
79
76
|
|
80
77
|
def hash_from_nested(name, value, &block)
|
81
|
-
|
78
|
+
result =
|
82
79
|
if value.is_a?(Array)
|
83
80
|
value.map do |val|
|
84
|
-
params = self.class.with(val).accept(&block)
|
81
|
+
params = self.class.with(val).accept(**default_args, &block)
|
85
82
|
@errors.push(*params.errors)
|
86
83
|
params.to_h
|
87
84
|
end.reject(&:empty?)
|
88
85
|
else
|
89
|
-
params = self.class.with(value).accept(&block)
|
86
|
+
params = self.class.with(value).accept(**default_args, &block)
|
90
87
|
@errors.push(*params.errors)
|
91
88
|
params.to_h
|
92
89
|
end
|
90
|
+
|
91
|
+
@to_h[name.to_sym] = result unless result.empty?
|
93
92
|
end
|
94
93
|
end
|
95
94
|
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