attribeauty 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +15 -14
- data/lib/attribeauty/params.rb +1 -1
- data/lib/attribeauty/validator.rb +6 -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: d41ac9aebcbb1c28b939ef7c8da19280dac7135655bc9b8fdab6758aad4a76e8
|
4
|
+
data.tar.gz: eaabafa87e2315bb7ea25debb7b915dfcc7e200c07b556274ee1aa73c4b4fbc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 844f5020c04d41dc2d9ce9fc67e678b66a2cfb1722e98ac9dbee57a01d0bfd7269ac4f939194661e6fa87646cc363358f4e791efedfc04b40036bc447837a804
|
7
|
+
data.tar.gz: fcaf05b3f5f450862d664d5419e4375b3419d6d2e6b9ce625aa71517bc314f466eee3b713a7f931d93f5c5dbbb18998351059acd03ba90138f4d71161d1910cf
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -103,17 +103,18 @@ class MyController
|
|
103
103
|
|
104
104
|
def update_params
|
105
105
|
params_filter.accept do
|
106
|
-
attribute :title, :string,
|
106
|
+
attribute :title, :string, required: true
|
107
107
|
attribute :email do
|
108
|
-
attribute :address, :string
|
109
|
-
attribute :valid, :boolean
|
110
|
-
attribute :ip_address, :string,
|
108
|
+
attribute :address, :string
|
109
|
+
attribute :valid, :boolean
|
110
|
+
attribute :ip_address, :string, exclude_if: :empty?
|
111
111
|
end
|
112
112
|
end
|
113
113
|
end
|
114
114
|
```
|
115
115
|
|
116
|
-
If you have a "head" param, like in rails, you can exclude it
|
116
|
+
If you have a "head" param, like in rails, you can exclude it, also note the `exclude_if` option, this will exclude the value completely, if it evaluates to true.
|
117
|
+
`exclude_if` will accept a single method call (`:nil?`) or an array (`[:nil?, :empty?]`)
|
117
118
|
|
118
119
|
```
|
119
120
|
class MyController
|
@@ -126,7 +127,7 @@ class MyController
|
|
126
127
|
private
|
127
128
|
|
128
129
|
# your params look like this:
|
129
|
-
# { user: { title: "woo", email: { address: "hmm@yep.com" } } }
|
130
|
+
# { user: { title: "woo", email: { address: "hmm@yep.com", ip_address: "" } } }
|
130
131
|
#
|
131
132
|
def params_filter
|
132
133
|
Attribeauty::Params.with(request.params)
|
@@ -138,11 +139,11 @@ class MyController
|
|
138
139
|
def update_params
|
139
140
|
params_filter.accept do
|
140
141
|
container :user do
|
141
|
-
attribute :title, :string,
|
142
|
+
attribute :title, :string, required: true
|
142
143
|
attribute :email do
|
143
|
-
attribute :address, :string,
|
144
|
-
attribute :valid, :boolean
|
145
|
-
attribute :ip_address, :string,
|
144
|
+
attribute :address, :string, exclude_if: [:empty?, :nil?]
|
145
|
+
attribute :valid, :boolean
|
146
|
+
attribute :ip_address, :string, exclude_if: :empty?
|
146
147
|
end
|
147
148
|
end
|
148
149
|
end
|
@@ -177,11 +178,11 @@ class MyController
|
|
177
178
|
def update_params
|
178
179
|
params_filter.accept! do
|
179
180
|
container :user do
|
180
|
-
attribute :title, :string,
|
181
|
+
attribute :title, :string, exclude_if: :nil?, required: true
|
181
182
|
attribute :email do
|
182
|
-
attribute :address, :string
|
183
|
-
attribute :valid, :boolean
|
184
|
-
attribute :ip_address, :string
|
183
|
+
attribute :address, :string
|
184
|
+
attribute :valid, :boolean
|
185
|
+
attribute :ip_address, :string
|
185
186
|
end
|
186
187
|
end
|
187
188
|
end
|
data/lib/attribeauty/params.rb
CHANGED
@@ -10,7 +10,7 @@ module Attribeauty
|
|
10
10
|
new(request_params)
|
11
11
|
end
|
12
12
|
|
13
|
-
attr_reader :
|
13
|
+
attr_reader :prefix, :request_params, :acceptables, :to_h, :errors, :strict
|
14
14
|
|
15
15
|
def initialize(request_params)
|
16
16
|
@request_params = request_params.transform_keys(&:to_sym)
|
@@ -2,16 +2,11 @@
|
|
2
2
|
|
3
3
|
module Attribeauty
|
4
4
|
class Validator
|
5
|
-
ALLOWS_HASH = {
|
6
|
-
allow_nil: :nil?,
|
7
|
-
allow_empty: :empty?
|
8
|
-
}.freeze
|
9
|
-
|
10
5
|
def self.run(name, type, original_val, **args)
|
11
6
|
new(name, type, original_val, **args).run
|
12
7
|
end
|
13
8
|
|
14
|
-
attr_reader :original_val, :errors, :name, :type, :required, :default, :
|
9
|
+
attr_reader :original_val, :errors, :name, :type, :required, :default, :excludes, :value, :valid
|
15
10
|
|
16
11
|
def initialize(name, original_val, type = nil, **args)
|
17
12
|
@name = name
|
@@ -19,7 +14,7 @@ module Attribeauty
|
|
19
14
|
@original_val = original_val
|
20
15
|
@default = args[:default]
|
21
16
|
@required = args[:required] if args[:required] == true
|
22
|
-
@
|
17
|
+
@excludes = args[:exclude_if]
|
23
18
|
|
24
19
|
@valid = true
|
25
20
|
@errors = []
|
@@ -32,7 +27,7 @@ module Attribeauty
|
|
32
27
|
set_default
|
33
28
|
cast_value
|
34
29
|
handle_missing_required
|
35
|
-
|
30
|
+
handle_excludes
|
36
31
|
end
|
37
32
|
|
38
33
|
self
|
@@ -62,25 +57,10 @@ module Attribeauty
|
|
62
57
|
@valid = false
|
63
58
|
end
|
64
59
|
|
65
|
-
def
|
66
|
-
return if
|
67
|
-
|
68
|
-
@valid = !value.public_send(predicate)
|
69
|
-
end
|
70
|
-
|
71
|
-
def allows_array
|
72
|
-
ALLOWS_HASH.keys
|
73
|
-
end
|
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?
|
60
|
+
def handle_excludes
|
61
|
+
return if excludes.nil? || !valid?
|
81
62
|
|
82
|
-
|
83
|
-
ALLOWS_HASH[key]
|
63
|
+
@valid = ![*excludes].flatten.any? { |exclude| value.public_send(exclude) }
|
84
64
|
end
|
85
65
|
|
86
66
|
def required?
|
data/lib/attribeauty/version.rb
CHANGED