decanter 4.0.2 → 4.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +47 -5
- data/lib/decanter/version.rb +1 -1
- 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: 855c0c38fda4543f1542a7519ff4234544d696a910f39fe195cf99f50ad6a780
|
4
|
+
data.tar.gz: 28b8bb87ee97e7a0c371124cbb8a04319b35df6c2ee642f75290d822a2015186
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 523f731eb6580679bc32699b6ae473220dabf8840124657bd0bfbfbbfd2c892affdcf44ba33c9435f51366c83bb8d86ebe467f7b2a98ae0da32211b525d062ca
|
7
|
+
data.tar.gz: 9923cf90108899c59990687ace8b0e23bad06dfb01b52d8c28594ba1f657a687007f33c4aacad4981dd2391106114df3098121a74e845ee967cccb52a94405c2
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -61,14 +61,48 @@ Then, transform incoming params in your controller using `Decanter#decant`:
|
|
61
61
|
|
62
62
|
### Generators
|
63
63
|
|
64
|
-
Decanter comes with generators for creating `Decanter` and `Parser` files:
|
64
|
+
Decanter comes with custom generators for creating `Decanter` and `Parser` files:
|
65
|
+
|
66
|
+
#### Decanters
|
65
67
|
|
66
68
|
```
|
67
69
|
rails g decanter Trip name:string start_date:date end_date:date
|
70
|
+
|
71
|
+
# Creates app/decanters/trip_decanter.rb:
|
72
|
+
class TripDecanter < Decanter::Base
|
73
|
+
input :name, :string
|
74
|
+
input :start_date, :date
|
75
|
+
input :end_date, :date
|
76
|
+
end
|
68
77
|
```
|
69
78
|
|
79
|
+
#### Parsers
|
70
80
|
```
|
71
81
|
rails g parser TruncatedString
|
82
|
+
|
83
|
+
# Creates lib/decanter/parsers/truncated_string_parser.rb:
|
84
|
+
class TruncatedStringParser < Decanter::Parser::ValueParser
|
85
|
+
parser do |value, options|
|
86
|
+
value
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
[Learn more about using custom parsers](#custom-parsers)
|
92
|
+
|
93
|
+
#### Resources
|
94
|
+
|
95
|
+
When using the Rails resource generator in a project that includes Decanter, a decanter will be automatically created for the new resource:
|
96
|
+
|
97
|
+
```
|
98
|
+
rails g resource Trip name:string start_date:date end_date:date
|
99
|
+
|
100
|
+
# Creates app/decanters/trip_decanter.rb:
|
101
|
+
class TripDecanter < Decanter::Base
|
102
|
+
input :name, :string
|
103
|
+
input :start_date, :date
|
104
|
+
input :end_date, :date
|
105
|
+
end
|
72
106
|
```
|
73
107
|
|
74
108
|
### Decanting Collections
|
@@ -137,11 +171,19 @@ input :ids, :array, parse_each: :integer
|
|
137
171
|
|
138
172
|
### Parser options
|
139
173
|
|
140
|
-
|
174
|
+
Some parsers can receive options that modify their behavior. These options are passed in as named arguments to `input`:
|
175
|
+
|
176
|
+
**Example:**
|
141
177
|
|
142
178
|
```ruby
|
143
179
|
input :start_date, :date, parse_format: '%Y-%m-%d'
|
144
180
|
```
|
181
|
+
**Available Options:**
|
182
|
+
| Parser | Option | Default | Notes
|
183
|
+
| ----------- | ----------- | -----------| -----------
|
184
|
+
| `ArrayParser` | `parse_each`| N/A | Accepts a parser type, then uses that parser to parse each element in the array. If this option is not defined, each element is simply returned.
|
185
|
+
| `DateParser`| `parse_format` | `'%m/%d/%Y'`| Accepts any format string accepted by Ruby's `strftime` method
|
186
|
+
| `DateTimeParser` | `parse_format` | `'%m/%d/%Y %I:%M:%S %p'` | Accepts any format string accepted by Ruby's `strftime` method
|
145
187
|
|
146
188
|
### Exceptions
|
147
189
|
|
@@ -176,8 +218,8 @@ You can also disable strict mode globally using a [global configuration](#global
|
|
176
218
|
To add a custom parser, first create a parser class:
|
177
219
|
|
178
220
|
```rb
|
179
|
-
# app/parsers/
|
180
|
-
class
|
221
|
+
# app/parsers/truncated_string_parser.rb
|
222
|
+
class TruncatedStringParser < Decanter::Parser::ValueParser
|
181
223
|
|
182
224
|
parser do |value, options|
|
183
225
|
length = options.fetch(:length, 100)
|
@@ -189,7 +231,7 @@ end
|
|
189
231
|
Then, use the appropriate key to look up the parser:
|
190
232
|
|
191
233
|
```ruby
|
192
|
-
input :name, :
|
234
|
+
input :name, :truncated_string #=> TruncatedStringParser
|
193
235
|
```
|
194
236
|
|
195
237
|
#### Custom parser methods
|
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: 4.0.
|
4
|
+
version: 4.0.4
|
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:
|
12
|
+
date: 2024-01-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|