decanter 0.6.2 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +43 -7
- data/lib/decanter/extensions.rb +5 -1
- data/lib/decanter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db2d47fb35516ad171707804d74d599dfc9f4275
|
4
|
+
data.tar.gz: 9de4216e15cef03ab046d1dc68372ce3de6f834d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5a4887ef4cebe7358b2a57a25ef558647ebc4760369636c91dd65aa266649e0a85df014f02b746849d3fd301f603cb865154349aedba8a8232684d0ff1da127
|
7
|
+
data.tar.gz: 3efb3ee49fc50aaa223c2205fe4e2ff9bde46fc482529510d86735bc16d31db7243f7d1e58bc2433b852f46ec74358312fb748544f268e37176c3c9512f35eb8
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,9 @@ Decanter
|
|
8
8
|
What is Decanter?
|
9
9
|
---
|
10
10
|
|
11
|
-
Decanter is a Rails gem that makes it easy to
|
11
|
+
Decanter is a Rails gem that makes it easy to transform incoming data before it hits the model. The basic idea is that form data entered by a user often needs to be processed before it is stored into the database. A typical example of this is a datepicker. A user selects January 15th, 2015 as the date, but this is going to come into our controller as a string like "01/15/2015", so we need to convert this string to a Ruby Date object before it is stored in our database. Many developers perform this conversion right in the controller, which results in errors and unnecessary complexity, especially as the application grows.
|
12
|
+
|
13
|
+
You can think of Decanter as the opposite of Active Model Serializer. Whereas AMS transforms your outbound data into a format that your frontend consumes, Decanter transforms your incoming data into a format that your backend consumes.
|
12
14
|
|
13
15
|
Installation
|
14
16
|
---
|
@@ -73,6 +75,17 @@ In your controller:
|
|
73
75
|
end
|
74
76
|
```
|
75
77
|
|
78
|
+
Or, if you would prefer to get the parsed hash and then do your own logic, you can do the following:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
def create
|
82
|
+
parsed_params = Trip.decant(params[:trip])
|
83
|
+
@trip = Trip.new(parsed_params)
|
84
|
+
|
85
|
+
# save logic here
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
76
89
|
Basic Example
|
77
90
|
---
|
78
91
|
|
@@ -134,7 +147,7 @@ class TripDecanter < Decanter::Base
|
|
134
147
|
end
|
135
148
|
```
|
136
149
|
|
137
|
-
You'll also notice that instead of ```@trip = Trip.new(params[:trip])``` we do ```@trip = Trip.decant_new(params[:trip])```. ```decant_new
|
150
|
+
You'll also notice that instead of ```@trip = Trip.new(params[:trip])``` we do ```@trip = Trip.decant_new(params[:trip])```. ```decant_new``` is where the magic happens. It is converting the params from this:
|
138
151
|
|
139
152
|
```ruby
|
140
153
|
{
|
@@ -271,7 +284,7 @@ Each of the destinations in our params[:trip] are automatically parsed according
|
|
271
284
|
Non Database-Backed Objects
|
272
285
|
---
|
273
286
|
|
274
|
-
Decanter will work for
|
287
|
+
Decanter will work for your non database-backed objects as well. We just need to call ```decant``` to parse our params according to our decanter logic.
|
275
288
|
|
276
289
|
Let's say we have a search filtering object called ```SearchFilter```. We start by generating our decanter:
|
277
290
|
|
@@ -336,11 +349,34 @@ rails g parser SquashDate
|
|
336
349
|
# app/decanter/squashers/date_squasher.rb
|
337
350
|
|
338
351
|
class SquashDateParser < Decanter::Parser::Base
|
339
|
-
parser do |name, values, options|
|
340
|
-
day = values
|
341
|
-
month = values[1]
|
342
|
-
year = values[2]
|
352
|
+
parser do |name, values, options|
|
353
|
+
day, month, year = values.map(&:to_i)
|
343
354
|
Date.new(year, month, day)
|
344
355
|
end
|
345
356
|
end
|
346
357
|
```
|
358
|
+
|
359
|
+
No Need for Strong Params
|
360
|
+
---
|
361
|
+
|
362
|
+
Since you are already defining your expected inputs in Decanter, you really don't need strong_params anymore.
|
363
|
+
|
364
|
+
In order to tell Decanter to ignore the params not defined in your Decanter, just add the ```strict``` flag to your Decanters:
|
365
|
+
|
366
|
+
```ruby
|
367
|
+
class TripDecanter < Decanter::Base
|
368
|
+
strict true
|
369
|
+
|
370
|
+
input :name
|
371
|
+
end
|
372
|
+
```
|
373
|
+
|
374
|
+
Or to raise exceptions when parameters arrive in your Decanter that you didn't expect:
|
375
|
+
|
376
|
+
```ruby
|
377
|
+
class TripDecanter < Decanter::Base
|
378
|
+
strict :with_exception
|
379
|
+
|
380
|
+
input :name
|
381
|
+
end
|
382
|
+
```
|
data/lib/decanter/extensions.rb
CHANGED
@@ -15,6 +15,10 @@ module Decanter
|
|
15
15
|
self.save!(context: options[:context])
|
16
16
|
end
|
17
17
|
|
18
|
+
def decant(args, **options)
|
19
|
+
self.class.decant(args, options)
|
20
|
+
end
|
21
|
+
|
18
22
|
module ClassMethods
|
19
23
|
|
20
24
|
def decant_create(args, **options)
|
@@ -31,7 +35,7 @@ module Decanter
|
|
31
35
|
.save!(context: options[:context])
|
32
36
|
end
|
33
37
|
|
34
|
-
def decant(args, options)
|
38
|
+
def decant(args, options={})
|
35
39
|
options.fetch(:decanter, Decanter.decanter_for(self))
|
36
40
|
.decant(args)
|
37
41
|
end
|
data/lib/decanter/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decanter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Francis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-02-
|
12
|
+
date: 2016-02-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|