attribeauty 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +7 -1
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -1
- data/lib/attribeauty/params.rb +6 -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
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.4.3] - 2024-06-25
|
4
|
+
|
5
|
+
- allow certain values with `allow` option
|
6
|
+
|
7
|
+
## [0.4.2] - 2024-06-25
|
8
|
+
|
9
|
+
- getting and setting vals in params
|
10
|
+
|
3
11
|
## [0.4.1] - 2024-06-25
|
4
12
|
|
5
13
|
- Global default_args in accept, to apply your conditions to all attributes
|
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
@@ -39,13 +39,16 @@ module Attribeauty
|
|
39
39
|
to_h[key]
|
40
40
|
end
|
41
41
|
|
42
|
+
def []=(key, value)
|
43
|
+
to_h[key] = value
|
44
|
+
end
|
45
|
+
|
42
46
|
def container(name)
|
43
47
|
@request_params = request_params[name]
|
44
48
|
|
45
49
|
yield
|
46
50
|
end
|
47
51
|
|
48
|
-
#
|
49
52
|
def attribute(name, type = nil, **args, &block)
|
50
53
|
value = request_params[name]
|
51
54
|
return hash_from_nested(name, value, &block) if block_given?
|
@@ -75,7 +78,7 @@ module Attribeauty
|
|
75
78
|
end
|
76
79
|
|
77
80
|
def hash_from_nested(name, value, &block)
|
78
|
-
result =
|
81
|
+
result =
|
79
82
|
if value.is_a?(Array)
|
80
83
|
value.map do |val|
|
81
84
|
params = self.class.with(val).accept(**default_args, &block)
|
@@ -86,7 +89,7 @@ module Attribeauty
|
|
86
89
|
params = self.class.with(value).accept(**default_args, &block)
|
87
90
|
@errors.push(*params.errors)
|
88
91
|
params.to_h
|
89
|
-
|
92
|
+
end
|
90
93
|
|
91
94
|
@to_h[name.to_sym] = result unless result.empty?
|
92
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:
|