attribeauty 0.4.2 → 0.4.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 +7 -1
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -1
- data/lib/attribeauty/params.rb +2 -3
- data/lib/attribeauty/validator.rb +29 -5
- data/lib/attribeauty/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6815673fc0f4d65774d12f0dff065883a9243bb8495c3379f2adc293cbc2f988
|
4
|
+
data.tar.gz: 5201b83d32ada2367130f5c7de336c012b8c157a4aa9331db1acca883407d4df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd30d11344376e152a20d8723c977b1434db36111be25ccfda3bbad20206da75d28d4c7c02cf71c05f15a0912ebbcf3102c5a07bc8ae19031629e281d850ee21
|
7
|
+
data.tar.gz: f063c90c743d67436000eb357b6b3868a92f790bff1c80c8638e7ba849b9d7a452416970053329106d06b648efe8143c51dc56a5e2953ae960b67217662cde19
|
data/.rubocop.yml
CHANGED
@@ -24,11 +24,17 @@ Metrics/AbcSize:
|
|
24
24
|
Metrics/ClassLength:
|
25
25
|
Enabled: false
|
26
26
|
|
27
|
+
Metrics/MethodLength:
|
28
|
+
Enabled: false
|
29
|
+
|
27
30
|
Metrics/PerceivedComplexity:
|
28
31
|
Enabled: false
|
29
32
|
|
30
33
|
Metrics/CyclomaticComplexity:
|
31
34
|
Enabled: false
|
32
35
|
|
33
|
-
Naming
|
36
|
+
Naming/BlockForwarding:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Naming/MemoizedInstanceVariableName:
|
34
40
|
Enabled: false
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -192,6 +192,7 @@ end
|
|
192
192
|
```
|
193
193
|
|
194
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
|
+
You can also exclude a value from this by useing the `allows` option.
|
195
196
|
|
196
197
|
```
|
197
198
|
class MyController
|
@@ -219,7 +220,7 @@ class MyController
|
|
219
220
|
attribute :email do
|
220
221
|
attribute :address, :string
|
221
222
|
attribute :valid, :boolean
|
222
|
-
attribute :ip_address, :string
|
223
|
+
attribute :ip_address, :string, allows: :nil?
|
223
224
|
end
|
224
225
|
end
|
225
226
|
end
|
data/lib/attribeauty/params.rb
CHANGED
@@ -49,7 +49,6 @@ module Attribeauty
|
|
49
49
|
yield
|
50
50
|
end
|
51
51
|
|
52
|
-
#
|
53
52
|
def attribute(name, type = nil, **args, &block)
|
54
53
|
value = request_params[name]
|
55
54
|
return hash_from_nested(name, value, &block) if block_given?
|
@@ -79,7 +78,7 @@ module Attribeauty
|
|
79
78
|
end
|
80
79
|
|
81
80
|
def hash_from_nested(name, value, &block)
|
82
|
-
result =
|
81
|
+
result =
|
83
82
|
if value.is_a?(Array)
|
84
83
|
value.map do |val|
|
85
84
|
params = self.class.with(val).accept(**default_args, &block)
|
@@ -90,7 +89,7 @@ module Attribeauty
|
|
90
89
|
params = self.class.with(value).accept(**default_args, &block)
|
91
90
|
@errors.push(*params.errors)
|
92
91
|
params.to_h
|
93
|
-
|
92
|
+
end
|
94
93
|
|
95
94
|
@to_h[name.to_sym] = result unless result.empty?
|
96
95
|
end
|
@@ -6,7 +6,8 @@ module Attribeauty
|
|
6
6
|
new(name, type, original_val, **args).run
|
7
7
|
end
|
8
8
|
|
9
|
-
attr_reader :original_val, :errors, :name, :type, :required,
|
9
|
+
attr_reader :original_val, :errors, :name, :type, :required,
|
10
|
+
:default, :excludes, :value, :valid, :allows
|
10
11
|
|
11
12
|
def initialize(name, original_val, type = nil, **args)
|
12
13
|
@name = name
|
@@ -15,6 +16,7 @@ module Attribeauty
|
|
15
16
|
@default = args[:default]
|
16
17
|
args.delete_if { |key, value| key == :required && value == false }
|
17
18
|
@required = args[:required]
|
19
|
+
@allows = args[:allow]
|
18
20
|
@excludes = args[:exclude_if]
|
19
21
|
|
20
22
|
@valid = true
|
@@ -22,10 +24,11 @@ module Attribeauty
|
|
22
24
|
end
|
23
25
|
|
24
26
|
def run
|
27
|
+
handle_allows
|
25
28
|
handle_missing_original_val!
|
26
29
|
handle_missing_required!
|
27
|
-
handle_missing_type
|
28
30
|
|
31
|
+
handle_missing_type
|
29
32
|
set_default
|
30
33
|
cast_value
|
31
34
|
handle_excludes
|
@@ -45,10 +48,17 @@ module Attribeauty
|
|
45
48
|
|
46
49
|
private
|
47
50
|
|
51
|
+
def handle_allows
|
52
|
+
return if allows.nil?
|
53
|
+
|
54
|
+
@required = false if allows_array.include?(:nil?)
|
55
|
+
@excludes = excludes_array - allows_array
|
56
|
+
end
|
57
|
+
|
48
58
|
def handle_missing_original_val!
|
49
|
-
return unless
|
59
|
+
return unless exclude_nil?
|
50
60
|
|
51
|
-
@valid = false
|
61
|
+
@valid = false
|
52
62
|
raise ValueInvalidError
|
53
63
|
end
|
54
64
|
|
@@ -78,7 +88,21 @@ module Attribeauty
|
|
78
88
|
def handle_excludes
|
79
89
|
return if excludes.nil? || !valid?
|
80
90
|
|
81
|
-
@valid =
|
91
|
+
@valid = excludes_array.none? { |exclude| value.public_send(exclude) }
|
92
|
+
end
|
93
|
+
|
94
|
+
def exclude_nil?
|
95
|
+
return false if allows_array.include?(:nil?)
|
96
|
+
|
97
|
+
!required? && original_val.nil?
|
98
|
+
end
|
99
|
+
|
100
|
+
def allows_array
|
101
|
+
[*allows].flatten.compact
|
102
|
+
end
|
103
|
+
|
104
|
+
def excludes_array
|
105
|
+
[*excludes].flatten
|
82
106
|
end
|
83
107
|
end
|
84
108
|
end
|
data/lib/attribeauty/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribeauty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toby
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: There are so many of these, I just needed this one.
|
14
14
|
email:
|