attribeauty 0.3.0 → 0.3.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 +82 -3
- data/lib/attribeauty/params.rb +19 -3
- data/lib/attribeauty/version.rb +1 -1
- data/lib/attribeauty.rb +2 -0
- 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: 16789f41e6c0cfea21c44f2f71fc3fd0c9664791ed8d1c97648834be86db6e92
|
4
|
+
data.tar.gz: e8c5ac4397043090870ffeacc5f4b1ae0565f0b7e85237a47254a41e31258032
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06c7ca8c0f104cc00ac9854fd4d5c31c043daf65c4d993c32da3dc0472ce4be296ebc2640e2b8b759fb6d99ba07d54e4a8b3c251c183da2048b22eb7da23af78
|
7
|
+
data.tar.gz: 13eb48b958c5cfd1da319a33326449e86683b90f9c89631964c4397d9447334a025b7b3647c87f40ad8c11465c51af7f73e32319ad0c9e344757d823f58e19a8
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.3.2] - 2024-06-24
|
4
|
+
|
5
|
+
- added accept! method to raise error if "required's" are missing
|
6
|
+
|
7
|
+
## [0.3.1] - 2024-06-22
|
8
|
+
|
9
|
+
- added container to exclude the "head" param
|
10
|
+
|
3
11
|
## [0.3.0] - 2024-06-21
|
4
12
|
|
5
13
|
- Experimental params coercion
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Attribeauty
|
2
2
|
|
3
3
|
I just wanted a quick, simple way to initialize mutable objects. This is it.
|
4
|
-
There are so many of these, but none were what I wanted.
|
4
|
+
There are so many of these (notably rails Attributes api), but none were what I wanted.
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
@@ -13,7 +13,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
13
13
|
|
14
14
|
$ gem install attribeauty
|
15
15
|
|
16
|
-
##
|
16
|
+
## Base
|
17
17
|
|
18
18
|
Inherit from `Attribeauty::Base` and then add attribute with the type you want.
|
19
19
|
Initialize with these and they will be cast to that attribute.
|
@@ -84,6 +84,8 @@ end
|
|
84
84
|
|
85
85
|
Experimental params sanitizer is now available. This will cast your params, and remove elements you want to exclude if `nil` or `empty`
|
86
86
|
|
87
|
+
Why is this needed? Params arrive into the controller in a messy state. Booleans are not ready for caparison, integers are often strings, empty strings, and nils abound. Rails does the casting of params at the model, which is simple and elegant, but in many cases, these params are used for a multitude of things before hitting the database. I truly believe we need to cast them before they do anything.
|
88
|
+
|
87
89
|
```
|
88
90
|
# app/controllers/my_controller.rb
|
89
91
|
class MyController
|
@@ -111,7 +113,84 @@ class MyController
|
|
111
113
|
end
|
112
114
|
```
|
113
115
|
|
114
|
-
|
116
|
+
If you have a "head" param, like in rails, you can exclude it like this:
|
117
|
+
|
118
|
+
```
|
119
|
+
class MyController
|
120
|
+
def update
|
121
|
+
MyRecord.update(update_params)
|
122
|
+
|
123
|
+
redirect_to index_path
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
# your params look like this:
|
129
|
+
# { user: { title: "woo", email: { address: "hmm@yep.com" } } }
|
130
|
+
#
|
131
|
+
def params_filter
|
132
|
+
Attribeauty::Params.with(request.params)
|
133
|
+
end
|
134
|
+
|
135
|
+
# using container will exclude the value and yield:
|
136
|
+
# { title: "woo", email: { address: "hmm@yep.com" } }
|
137
|
+
#
|
138
|
+
def update_params
|
139
|
+
params_filter.accept do
|
140
|
+
container :user do
|
141
|
+
attribute :title, :string, allow_nil: false, required: true
|
142
|
+
attribute :email do
|
143
|
+
attribute :address, :string, allow_empty: false
|
144
|
+
attribute :valid, :boolean, allow_nil: false
|
145
|
+
attribute :ip_address, :string, allow_blank: true
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
```
|
153
|
+
|
154
|
+
If you want to raise an error, rather than just return the errors in an array, use the `accept!` method. Will raise `Attribeauty::MissingAttributeError` with the required elements:
|
155
|
+
|
156
|
+
|
157
|
+
```
|
158
|
+
class MyController
|
159
|
+
def update
|
160
|
+
MyRecord.update(update_params)
|
161
|
+
|
162
|
+
redirect_to index_path
|
163
|
+
end
|
164
|
+
|
165
|
+
private
|
166
|
+
|
167
|
+
# your params look like this:
|
168
|
+
# { user: { profile: [{ address: { street_name: "Main St" } }] } }
|
169
|
+
#
|
170
|
+
def params_filter
|
171
|
+
Attribeauty::Params.with(request.params)
|
172
|
+
end
|
173
|
+
|
174
|
+
# This following with the accept! method
|
175
|
+
# will raise: Attribeauty::MissingAttributeError, "title required, email required"
|
176
|
+
#
|
177
|
+
def update_params
|
178
|
+
params_filter.accept! do
|
179
|
+
container :user do
|
180
|
+
attribute :title, :string, allow_nil: false, required: true
|
181
|
+
attribute :email do
|
182
|
+
attribute :address, :string, allow_empty: false
|
183
|
+
attribute :valid, :boolean, allow_nil: false
|
184
|
+
attribute :ip_address, :string, allow_blank: true
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
```
|
192
|
+
|
193
|
+
See `test/test_params.rb` for more examples
|
115
194
|
|
116
195
|
|
117
196
|
## Development
|
data/lib/attribeauty/params.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "forwardable"
|
4
|
-
|
5
3
|
module Attribeauty
|
6
4
|
class Params
|
7
5
|
extend Forwardable
|
@@ -12,7 +10,7 @@ module Attribeauty
|
|
12
10
|
new(request_params)
|
13
11
|
end
|
14
12
|
|
15
|
-
attr_reader :allow_nil, :prefix, :request_params, :acceptables, :to_h, :errors
|
13
|
+
attr_reader :allow_nil, :prefix, :request_params, :acceptables, :to_h, :errors, :strict
|
16
14
|
|
17
15
|
def initialize(request_params)
|
18
16
|
@request_params = request_params.transform_keys(&:to_sym)
|
@@ -23,15 +21,29 @@ module Attribeauty
|
|
23
21
|
def accept(&)
|
24
22
|
instance_eval(&)
|
25
23
|
|
24
|
+
raise MissingAttributeError, errors.join(", ") if errors.any? && strict?
|
25
|
+
|
26
26
|
self
|
27
27
|
end
|
28
28
|
|
29
|
+
def accept!(&)
|
30
|
+
@strict = true
|
31
|
+
|
32
|
+
accept(&)
|
33
|
+
end
|
34
|
+
|
29
35
|
def to_hash = to_h
|
30
36
|
|
31
37
|
def [](key)
|
32
38
|
to_h[key]
|
33
39
|
end
|
34
40
|
|
41
|
+
def container(name)
|
42
|
+
@request_params = request_params[name]
|
43
|
+
|
44
|
+
yield
|
45
|
+
end
|
46
|
+
|
35
47
|
# rubocop:disable Naming::BlockForwarding
|
36
48
|
def attribute(name, type = nil, **args, &block)
|
37
49
|
value = request_params[name]
|
@@ -65,5 +77,9 @@ module Attribeauty
|
|
65
77
|
def valid?
|
66
78
|
errors.empty?
|
67
79
|
end
|
80
|
+
|
81
|
+
def strict?
|
82
|
+
strict
|
83
|
+
end
|
68
84
|
end
|
69
85
|
end
|
data/lib/attribeauty/version.rb
CHANGED
data/lib/attribeauty.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.3.
|
4
|
+
version: 0.3.2
|
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-24 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:
|