attribeauty 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0635b380b4cb80f46aa693faef308dd93acf646950c8a592efe9ee56f3f019d1
4
- data.tar.gz: f47043d7784f3068f3f82575bc71a5cdf7006cc73e8a8b4f481381a55091cb84
3
+ metadata.gz: 16789f41e6c0cfea21c44f2f71fc3fd0c9664791ed8d1c97648834be86db6e92
4
+ data.tar.gz: e8c5ac4397043090870ffeacc5f4b1ae0565f0b7e85237a47254a41e31258032
5
5
  SHA512:
6
- metadata.gz: 90040026bba413f4390d1c8141140dc90d7547b47e1af3ec737d23a6ec79c7a9ca9677086318b4d44107acdaf39b8587711e41231074ad80854c619eb3a02883
7
- data.tar.gz: 747703281202de16748413ecee1703ad15a6353a31caa289bf77b68e41d6ca820cf2d0bce5087af54ab7c39a237b398492701fa964a02af707dc00c4131fd487
6
+ metadata.gz: 06c7ca8c0f104cc00ac9854fd4d5c31c043daf65c4d993c32da3dc0472ce4be296ebc2640e2b8b759fb6d99ba07d54e4a8b3c251c183da2048b22eb7da23af78
7
+ data.tar.gz: 13eb48b958c5cfd1da319a33326449e86683b90f9c89631964c4397d9447334a025b7b3647c87f40ad8c11465c51af7f73e32319ad0c9e344757d823f58e19a8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
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
+
3
7
  ## [0.3.1] - 2024-06-22
4
8
 
5
9
  - added container to exclude the "head" param
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.3.1)
4
+ attribeauty (0.3.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -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
@@ -149,7 +151,46 @@ end
149
151
 
150
152
  ```
151
153
 
152
- See `test/test_params.rb` for what is expected.
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
153
194
 
154
195
 
155
196
  ## Development
@@ -10,7 +10,7 @@ module Attribeauty
10
10
  new(request_params)
11
11
  end
12
12
 
13
- attr_reader :allow_nil, :prefix, :request_params, :acceptables, :to_h, :errors
13
+ attr_reader :allow_nil, :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)
@@ -21,9 +21,17 @@ module Attribeauty
21
21
  def accept(&)
22
22
  instance_eval(&)
23
23
 
24
+ raise MissingAttributeError, errors.join(", ") if errors.any? && strict?
25
+
24
26
  self
25
27
  end
26
28
 
29
+ def accept!(&)
30
+ @strict = true
31
+
32
+ accept(&)
33
+ end
34
+
27
35
  def to_hash = to_h
28
36
 
29
37
  def [](key)
@@ -69,5 +77,9 @@ module Attribeauty
69
77
  def valid?
70
78
  errors.empty?
71
79
  end
80
+
81
+ def strict?
82
+ strict
83
+ end
72
84
  end
73
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribeauty
4
- VERSION = "0.3.1"
4
+ VERSION = "0.3.2"
5
5
  end
data/lib/attribeauty.rb CHANGED
@@ -7,6 +7,7 @@ require "forwardable"
7
7
  # Module
8
8
  module Attribeauty
9
9
  class Error < StandardError; end
10
+ class MissingAttributeError < StandardError; end
10
11
 
11
12
  class << self
12
13
  def configuration
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.1
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-22 00:00:00.000000000 Z
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: