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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c705d3129b34e6dbc91c53163face634eafbc288a2b36b2768faf97a77a7f8a
4
- data.tar.gz: 1553078b6ae388c7d75c367e9f70b2e1153b59b4999ce7fbe7da7bf078d64725
3
+ metadata.gz: 16789f41e6c0cfea21c44f2f71fc3fd0c9664791ed8d1c97648834be86db6e92
4
+ data.tar.gz: e8c5ac4397043090870ffeacc5f4b1ae0565f0b7e85237a47254a41e31258032
5
5
  SHA512:
6
- metadata.gz: efb29ddb06c0c1391d661efcc43b4b1e122233a7c0a481f0fe5bb96a00fe190e0bd05e87640f8f16872ca32c9bd6aaf7120665a6f931b8d41d60109dc8ac49af
7
- data.tar.gz: 9c10fb519ec0eaaffd5a31d02c5aea808917fd2ce0b3421ba4e4edf1c4b8dba796ac2fddda907e62e79ba9edf8c8c2355dc37a564346b7377979994ef57ea603
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.3.0)
4
+ attribeauty (0.3.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
- ## Usage
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
- See `test/test_params.rb` for what is expected.
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribeauty
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.2"
5
5
  end
data/lib/attribeauty.rb CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  require "date"
4
4
  require "time"
5
+ require "forwardable"
5
6
 
6
7
  # Module
7
8
  module Attribeauty
8
9
  class Error < StandardError; end
10
+ class MissingAttributeError < StandardError; end
9
11
 
10
12
  class << self
11
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.0
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-21 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: