decanter 2.1.1 → 3.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +38 -0
- data/.github/ISSUE_TEMPLATE/BUG_REPORT.md +33 -0
- data/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md +18 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +8 -0
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +10 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/CONTRIBUTING.md +36 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +102 -0
- data/LICENSE.txt +21 -0
- data/README.md +241 -0
- data/Rakefile +1 -0
- data/bin/console +3 -4
- data/decanter.gemspec +39 -0
- data/lib/decanter.rb +18 -10
- data/lib/decanter/base.rb +0 -2
- data/lib/decanter/configuration.rb +2 -3
- data/lib/decanter/core.rb +136 -126
- data/lib/decanter/exceptions.rb +4 -7
- data/lib/decanter/extensions.rb +11 -11
- data/lib/decanter/parser.rb +13 -5
- data/lib/decanter/parser/array_parser.rb +28 -0
- data/lib/decanter/parser/base.rb +1 -2
- data/lib/decanter/parser/boolean_parser.rb +4 -3
- data/lib/decanter/parser/compose_parser.rb +27 -0
- data/lib/decanter/parser/core.rb +8 -16
- data/lib/decanter/parser/date_parser.rb +6 -7
- data/lib/decanter/parser/datetime_parser.rb +15 -0
- data/lib/decanter/parser/float_parser.rb +7 -5
- data/lib/decanter/parser/hash_parser.rb +6 -9
- data/lib/decanter/parser/integer_parser.rb +8 -4
- data/lib/decanter/parser/pass_parser.rb +5 -3
- data/lib/decanter/parser/phone_parser.rb +3 -3
- data/lib/decanter/parser/string_parser.rb +4 -5
- data/lib/decanter/parser/utils.rb +1 -3
- data/lib/decanter/parser/value_parser.rb +3 -4
- data/lib/decanter/railtie.rb +15 -11
- data/lib/decanter/version.rb +1 -3
- data/lib/generators/decanter/install_generator.rb +3 -5
- data/lib/generators/decanter/templates/initializer.rb +1 -4
- data/lib/generators/rails/decanter_generator.rb +5 -7
- data/lib/generators/rails/parser_generator.rb +3 -5
- data/lib/generators/rails/resource_override.rb +0 -2
- data/migration-guides/v3.0.0.md +21 -0
- metadata +47 -20
- data/lib/decanter/decant.rb +0 -11
- data/lib/decanter/parser/date_time_parser.rb +0 -21
- data/lib/decanter/parser/join_parser.rb +0 -14
- data/lib/decanter/parser/key_value_splitter_parser.rb +0 -18
- data/lib/decanter/parser/time_parser.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41a34deb5031de28979d99cb09d64538b2d718435d5d7786626c5d62c81021d1
|
4
|
+
data.tar.gz: a897bf8bccc8f3d282299a6d1f0f68a70dcdbaabd6b9b8ab933b52e619cd0e26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4409ea087acc2122ca50cdd57daf6edbf1fd08b2c34f3386c9ba8d1989e14ef60c648f45d0d3eaffdc14813b26d4c277d9abedbcb6b8424f404a8f1096eb161
|
7
|
+
data.tar.gz: 1fe1e1b5939338bca63153421810649afdadc2b169d45f5823bcd437f7229aa56bb8e4baceddf7c31e7c7fbe7624d49e3bf94820587e11f455727bbb49a87aee
|
data/.codeclimate.yml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
engines:
|
2
|
+
rubocop:
|
3
|
+
enabled: true
|
4
|
+
checks:
|
5
|
+
Rubocop/Style/Documentation:
|
6
|
+
enabled: false
|
7
|
+
Rubocop/Metrics/LineLength:
|
8
|
+
enabled: false
|
9
|
+
Rubocop/Rails/Validation:
|
10
|
+
enabled: false
|
11
|
+
Rubocop/Style/IndentationConsistency:
|
12
|
+
enabled: false
|
13
|
+
Rubocop/Style/EmptyLines:
|
14
|
+
enabled: false
|
15
|
+
Rubocop/Style/ClassAndModuleChildren:
|
16
|
+
enabled: false
|
17
|
+
Rubocop/Style/AccessorMethodName:
|
18
|
+
enabled: false
|
19
|
+
golint:
|
20
|
+
enabled: true
|
21
|
+
gofmt:
|
22
|
+
enabled: true
|
23
|
+
eslint:
|
24
|
+
enabled: true
|
25
|
+
csslint:
|
26
|
+
enabled: true
|
27
|
+
brakeman:
|
28
|
+
enabled: false
|
29
|
+
bundler-audit:
|
30
|
+
enabled: true
|
31
|
+
ratings:
|
32
|
+
paths:
|
33
|
+
- lib/**
|
34
|
+
- "**.rb"
|
35
|
+
exclude_paths:
|
36
|
+
- bin/**/*
|
37
|
+
- spec/**/*
|
38
|
+
- coverage/**/*
|
@@ -0,0 +1,33 @@
|
|
1
|
+
---
|
2
|
+
name: Bug Report
|
3
|
+
about: Report a bug with Decanter
|
4
|
+
labels: bug
|
5
|
+
---
|
6
|
+
|
7
|
+
<!--
|
8
|
+
Please fill out this template with all the relevant information so we can
|
9
|
+
understand what's going on and fix the issue. We encourage you to submit a PR
|
10
|
+
to fix the reported issue!
|
11
|
+
-->
|
12
|
+
|
13
|
+
## Environment
|
14
|
+
|
15
|
+
- `decanter` version:
|
16
|
+
- `ruby` version:
|
17
|
+
- `rails` version:
|
18
|
+
|
19
|
+
## Expected Behavior
|
20
|
+
|
21
|
+
## Current Behavior
|
22
|
+
|
23
|
+
## Steps to Reproduce
|
24
|
+
<!--
|
25
|
+
If possible, please create a repository that reproduces the issue with the
|
26
|
+
minimal amount of code possible.
|
27
|
+
-->
|
28
|
+
|
29
|
+
## Suggested Solution
|
30
|
+
<!--
|
31
|
+
It's ok if you don't have a suggested solution, but please let us know if you
|
32
|
+
have any ideas!
|
33
|
+
-->
|
@@ -0,0 +1,18 @@
|
|
1
|
+
---
|
2
|
+
name: Feature Request
|
3
|
+
about: Suggest a feature for Decanter
|
4
|
+
labels: enhancement
|
5
|
+
---
|
6
|
+
|
7
|
+
## Feature Description
|
8
|
+
|
9
|
+
## Suggested Solution
|
10
|
+
<!--
|
11
|
+
It's ok if you don't have a suggested solution, but please let us know if you
|
12
|
+
have any ideas!
|
13
|
+
-->
|
14
|
+
|
15
|
+
## Alternatives Considered / Existing Workarounds
|
16
|
+
|
17
|
+
## Additional Context
|
18
|
+
<!-- Add other information, screenshots, etc. about the request here -->
|
@@ -0,0 +1,8 @@
|
|
1
|
+
## Items Addressed
|
2
|
+
<!-- Include description and link to resolved issue(s) here. -->
|
3
|
+
|
4
|
+
## Author Checklist
|
5
|
+
- [ ] Add unit test(s)
|
6
|
+
- [ ] Update documentation (if necessary)
|
7
|
+
- [ ] Update version in `version.rb` following [versioning guidelines](https://github.com/LaunchPadLab/opex-public/blob/master/gists/gem-guidelines.md#pull-requests-and-deployments)
|
8
|
+
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.5
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to make participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
9
|
+
level of experience, education, socio-economic status, nationality, personal
|
10
|
+
appearance, race, religion, or sexual identity and orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies within all project spaces, and it also applies when
|
49
|
+
an individual is representing the project or its community in public spaces.
|
50
|
+
Examples of representing a project or community include using an official
|
51
|
+
project e-mail address, posting via an official social media account, or acting
|
52
|
+
as an appointed representative at an online or offline event. Representation of
|
53
|
+
a project may be further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at [conor@launchpadlab.com](mailto:conor@launchpadlab.com). All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
72
|
+
|
73
|
+
[homepage]: https://www.contributor-covenant.org
|
74
|
+
|
75
|
+
For answers to common questions about this code of conduct, see
|
76
|
+
https://www.contributor-covenant.org/faq
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
Thanks for your interest in contributing! This project follows our [Gem guidelines](https://github.com/LaunchPadLab/opex-public/blob/master/gists/gem-guidelines.md).
|
4
|
+
|
5
|
+
## Code of Conduct
|
6
|
+
|
7
|
+
We expect all participants to adhere to our [Code of Conduct](CODE_OF_CONDUCT.md).
|
8
|
+
|
9
|
+
## How Can I Contribute?
|
10
|
+
|
11
|
+
There are several ways to contribute to this project, including (but not limited to):
|
12
|
+
|
13
|
+
- Reporting bugs
|
14
|
+
- Requesting features
|
15
|
+
- Responding to bug reports / feature requests
|
16
|
+
- Submitting PR(s) that address issues labeled with "help wanted"
|
17
|
+
- Updating documentation
|
18
|
+
|
19
|
+
Before making a contribution, search through the existing [issues](https://github.com/LaunchPadLab/decanter/issues) and [pull requests](https://github.com/LaunchPadLab/decanter/pulls) to see if your item has already been raised or addressed. If there is an existing **open** issue that impacts you, it is best practice to upvote (👍) the initial comment as opposed to adding an additional comment voicing your support. The latter creates noise for the LaunchPad Lab team and makes it difficult for us to quickly and effectively rank issues according to the community's priority.
|
20
|
+
|
21
|
+
## Getting Started Locally
|
22
|
+
1. Fork and clone the repo
|
23
|
+
1. Run `bundle` to install dependencies
|
24
|
+
1. Run `rspec` to run unit tests
|
25
|
+
1. Load development changes in a local application in your Gemfile
|
26
|
+
```ruby
|
27
|
+
gem "decanter", path: "/path/to/repo"
|
28
|
+
```
|
29
|
+
|
30
|
+
## Proposing a Change
|
31
|
+
For any non-trivial change, we prefer an issue to be created first. This helps us (and you!) save time and keep track of conversations and decisions made regarding the project.
|
32
|
+
|
33
|
+
### Sending a Pull Request
|
34
|
+
If this is your first Pull Request, we recommend learning how to navigate GitHub via this free video tutorial: [How to Contribute to an Open Source Project on GitHub](https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github).
|
35
|
+
|
36
|
+
The LaunchPad Lab team monitors this repository for pull requests. Once a pull request has been created from a forked repository that **meets all the guidelines** outlined in the [Pull Request Checklist](.github/PULL_REQUEST_TEMPLATE.md), we will review the request and either merge it, request changes, or close it with an explanation. We aim to respond to pull requests within 48 hours.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
decanter (3.1.1)
|
5
|
+
actionpack (>= 4.2.10)
|
6
|
+
activesupport
|
7
|
+
rails-html-sanitizer (>= 1.0.4)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actionpack (5.1.4)
|
13
|
+
actionview (= 5.1.4)
|
14
|
+
activesupport (= 5.1.4)
|
15
|
+
rack (~> 2.0)
|
16
|
+
rack-test (>= 0.6.3)
|
17
|
+
rails-dom-testing (~> 2.0)
|
18
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
19
|
+
actionview (5.1.4)
|
20
|
+
activesupport (= 5.1.4)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubi (~> 1.4)
|
23
|
+
rails-dom-testing (~> 2.0)
|
24
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
25
|
+
activesupport (5.1.4)
|
26
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
27
|
+
i18n (~> 0.7)
|
28
|
+
minitest (~> 5.1)
|
29
|
+
tzinfo (~> 1.1)
|
30
|
+
builder (3.2.3)
|
31
|
+
concurrent-ruby (1.0.5)
|
32
|
+
crass (1.0.4)
|
33
|
+
diff-lcs (1.3)
|
34
|
+
docile (1.1.5)
|
35
|
+
dotenv (2.2.1)
|
36
|
+
erubi (1.7.0)
|
37
|
+
i18n (0.9.3)
|
38
|
+
concurrent-ruby (~> 1.0)
|
39
|
+
json (2.1.0)
|
40
|
+
loofah (2.2.2)
|
41
|
+
crass (~> 1.0.2)
|
42
|
+
nokogiri (>= 1.5.9)
|
43
|
+
method_source (0.9.0)
|
44
|
+
mini_portile2 (2.3.0)
|
45
|
+
minitest (5.11.3)
|
46
|
+
nokogiri (1.8.5)
|
47
|
+
mini_portile2 (~> 2.3.0)
|
48
|
+
rack (2.0.4)
|
49
|
+
rack-test (0.8.2)
|
50
|
+
rack (>= 1.0, < 3)
|
51
|
+
rails-dom-testing (2.0.3)
|
52
|
+
activesupport (>= 4.2.0)
|
53
|
+
nokogiri (>= 1.6)
|
54
|
+
rails-html-sanitizer (1.0.4)
|
55
|
+
loofah (~> 2.2, >= 2.2.2)
|
56
|
+
railties (5.1.4)
|
57
|
+
actionpack (= 5.1.4)
|
58
|
+
activesupport (= 5.1.4)
|
59
|
+
method_source
|
60
|
+
rake (>= 0.8.7)
|
61
|
+
thor (>= 0.18.1, < 2.0)
|
62
|
+
rake (10.5.0)
|
63
|
+
rspec-core (3.7.1)
|
64
|
+
rspec-support (~> 3.7.0)
|
65
|
+
rspec-expectations (3.7.0)
|
66
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
67
|
+
rspec-support (~> 3.7.0)
|
68
|
+
rspec-mocks (3.7.0)
|
69
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
70
|
+
rspec-support (~> 3.7.0)
|
71
|
+
rspec-rails (3.7.2)
|
72
|
+
actionpack (>= 3.0)
|
73
|
+
activesupport (>= 3.0)
|
74
|
+
railties (>= 3.0)
|
75
|
+
rspec-core (~> 3.7.0)
|
76
|
+
rspec-expectations (~> 3.7.0)
|
77
|
+
rspec-mocks (~> 3.7.0)
|
78
|
+
rspec-support (~> 3.7.0)
|
79
|
+
rspec-support (3.7.1)
|
80
|
+
simplecov (0.15.1)
|
81
|
+
docile (~> 1.1.0)
|
82
|
+
json (>= 1.8, < 3)
|
83
|
+
simplecov-html (~> 0.10.0)
|
84
|
+
simplecov-html (0.10.2)
|
85
|
+
thor (0.20.0)
|
86
|
+
thread_safe (0.3.6)
|
87
|
+
tzinfo (1.2.5)
|
88
|
+
thread_safe (~> 0.1)
|
89
|
+
|
90
|
+
PLATFORMS
|
91
|
+
ruby
|
92
|
+
|
93
|
+
DEPENDENCIES
|
94
|
+
bundler (~> 1.9)
|
95
|
+
decanter!
|
96
|
+
dotenv
|
97
|
+
rake (~> 10.0)
|
98
|
+
rspec-rails
|
99
|
+
simplecov (~> 0.15.1)
|
100
|
+
|
101
|
+
BUNDLED WITH
|
102
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,241 @@
|
|
1
|
+
# Decanter
|
2
|
+
|
3
|
+
Decanter is a Ruby gem that makes it easy to transform incoming data before it hits the model. You can think of Decanter as the opposite of Active Model Serializers (AMS). While AMS transforms your outbound data into a format that your frontend consumes, Decanter transforms your incoming data into a format that your backend consumes.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
gem 'decanter', '~> 3.0'
|
7
|
+
```
|
8
|
+
|
9
|
+
## Migration Guides
|
10
|
+
|
11
|
+
- [v3.0.0](migration-guides/v3.0.0.md)
|
12
|
+
|
13
|
+
## Contents
|
14
|
+
|
15
|
+
- [Basic Usage](#basic-usage)
|
16
|
+
- [Decanters](#decanters)
|
17
|
+
- [Generators](#generators)
|
18
|
+
- [Nested resources](#nested-resources)
|
19
|
+
- [Default parsers](#default-parsers)
|
20
|
+
- [Parser options](#parser-options)
|
21
|
+
- [Exceptions](#exceptions)
|
22
|
+
- [Advanced usage](#advanced-usage)
|
23
|
+
- [Custom parsers](#custom-parsers)
|
24
|
+
- [Squashing inputs](#squashing-inputs)
|
25
|
+
- [Chaining parsers](#chaining-parsers)
|
26
|
+
- [Requiring params](#requiring-params)
|
27
|
+
- [Global configuration](#global-configuration)
|
28
|
+
- [Contributing](#contributing)
|
29
|
+
|
30
|
+
## Basic Usage
|
31
|
+
|
32
|
+
### Decanters
|
33
|
+
|
34
|
+
Declare a `Decanter` for a model:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# app/decanters/trip_decanter.rb
|
38
|
+
|
39
|
+
class TripDecanter < Decanter::Base
|
40
|
+
input :name, :string
|
41
|
+
input :start_date, :date
|
42
|
+
input :end_date, :date
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Then, transform incoming params in your controller using `Decanter#decant`:
|
47
|
+
|
48
|
+
```rb
|
49
|
+
# app/controllers/trips_controller.rb
|
50
|
+
|
51
|
+
def create
|
52
|
+
trip_params = params.require(:trip) # or params[:trip] if you are not using Strong Parameters
|
53
|
+
decanted_trip_params = TripDecanter.decant(trip_params)
|
54
|
+
@trip = Trip.new(decanted_trip_params)
|
55
|
+
|
56
|
+
# ...any response logic
|
57
|
+
end
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
### Generators
|
62
|
+
|
63
|
+
Decanter comes with generators for creating `Decanter` and `Parser` files:
|
64
|
+
|
65
|
+
```
|
66
|
+
rails g decanter Trip name:string start_date:date end_date:date
|
67
|
+
```
|
68
|
+
|
69
|
+
```
|
70
|
+
rails g parser TruncatedString
|
71
|
+
```
|
72
|
+
|
73
|
+
### Nested resources
|
74
|
+
|
75
|
+
Decanters can declare relationships using `ActiveRecord`-style declarators:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class TripDecanter < Decanter::Base
|
79
|
+
has_many :destinations
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
This decanter will look up and apply the corresponding `DestinationDecanter` whenever necessary to transform nested resources.
|
84
|
+
|
85
|
+
### Default parsers
|
86
|
+
|
87
|
+
Decanter comes with the following parsers out of the box:
|
88
|
+
|
89
|
+
- `:boolean`
|
90
|
+
- `:date`
|
91
|
+
- `:date_time`
|
92
|
+
- `:float`
|
93
|
+
- `:integer`
|
94
|
+
- `:pass`
|
95
|
+
- `:phone`
|
96
|
+
- `:string`
|
97
|
+
- `:array`
|
98
|
+
|
99
|
+
Note: these parsers are designed to operate on a single value, except for `:array`. This parser expects an array, and will use the `parse_each` option to call a given parser on each of its elements:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
input :ids, :array, parse_each: :integer
|
103
|
+
```
|
104
|
+
|
105
|
+
### Parser options
|
106
|
+
|
107
|
+
Parsers can receive options that modify their behavior. These options are passed in as named arguments to `input`:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
input :start_date, :date, parse_format: '%Y-%m-%d'
|
111
|
+
```
|
112
|
+
|
113
|
+
### Exceptions
|
114
|
+
|
115
|
+
By default, `Decanter#decant` will raise an exception when unexpected parameters are passed. To override this behavior, you can disable strict mode:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
class TripDecanter < Decanter::Base
|
119
|
+
strict false
|
120
|
+
# ...
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
124
|
+
Or explicitly ignore a key:
|
125
|
+
|
126
|
+
```rb
|
127
|
+
class TripDecanter < Decanter::Base
|
128
|
+
ignore :created_at, :updated_at
|
129
|
+
# ...
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
You can also disable strict mode globally using a [global configuration](#global-configuration) setting.
|
134
|
+
|
135
|
+
## Advanced Usage
|
136
|
+
|
137
|
+
### Custom Parsers
|
138
|
+
|
139
|
+
To add a custom parser, first create a parser class:
|
140
|
+
|
141
|
+
```rb
|
142
|
+
# app/parsers/truncate_string_parser.rb
|
143
|
+
class TruncateStringParser < Decanter::Parser::ValueParser
|
144
|
+
|
145
|
+
parser do |value, options|
|
146
|
+
length = options.fetch(:length, 100)
|
147
|
+
value.truncate(length)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
Then, use the appropriate key to look up the parser:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
input :name, :truncate_string #=> TruncateStringParser
|
156
|
+
```
|
157
|
+
|
158
|
+
#### Custom parser methods
|
159
|
+
|
160
|
+
- `#parse <block>`: (required) recieves a block for parsing a value. Block parameters are `|value, options|` for `ValueParser` and `|name, value, options|` for `HashParser`.
|
161
|
+
- `#allow [<class>]`: skips parse step if the incoming value `is_a?` instance of class(es).
|
162
|
+
- `#pre [<parser>]`: applies the given parser(s) before parsing the value.
|
163
|
+
|
164
|
+
#### Custom parser base classes
|
165
|
+
|
166
|
+
- `Decanter::Parser::ValueParser`: subclasses are expected to return a single value.
|
167
|
+
- `Decanter::Parser::HashParser`: subclasses are expected to return a hash of keys and values.
|
168
|
+
|
169
|
+
### Squashing inputs
|
170
|
+
|
171
|
+
Sometimes, you may want to take several inputs and combine them into one finished input prior to sending to your model. You can achieve this with a custom parser:
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
class TripDecanter < Decanter::Base
|
175
|
+
input [:day, :month, :year], :squash_date, key: :start_date
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
class SquashDateParser < Decanter::Parser::ValueParser
|
181
|
+
parser do |values, options|
|
182
|
+
day, month, year = values.map(&:to_i)
|
183
|
+
Date.new(year, month, day)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
```
|
187
|
+
|
188
|
+
### Chaining parsers
|
189
|
+
|
190
|
+
You can compose multiple parsers by using the `#pre` method:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
class FloatPercentParser < Decanter::Parser::ValueParser
|
194
|
+
|
195
|
+
pre :float
|
196
|
+
|
197
|
+
parser do |val, options|
|
198
|
+
val / 100
|
199
|
+
end
|
200
|
+
end
|
201
|
+
```
|
202
|
+
|
203
|
+
Or by declaring multiple parsers for a single input:
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
class SomeDecanter < Decanter::Base
|
207
|
+
input :some_percent, [:float, :percent]
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
211
|
+
### Requiring params
|
212
|
+
|
213
|
+
If you provide the option `:required` for an input in your decanter, an exception will be thrown if the parameter is `nil` or an empty string.
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
class TripDecanter < Decanter::Base
|
217
|
+
input :name, :string, required: true
|
218
|
+
end
|
219
|
+
```
|
220
|
+
|
221
|
+
_Note: we recommend using [Active Record validations](https://guides.rubyonrails.org/active_record_validations.html) to check for presence of an attribute, rather than using the `required` option. This method is intended for use in non-RESTful routes or cases where Active Record validations are not available._
|
222
|
+
|
223
|
+
### Global configuration
|
224
|
+
|
225
|
+
You can generate a local copy of the default configuration with `rails generate decanter:install`. This will create an initializer where you can do global configuration:
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
# ./config/initializers/decanter.rb
|
229
|
+
|
230
|
+
Decanter.config do |config|
|
231
|
+
config.strict = false
|
232
|
+
end
|
233
|
+
```
|
234
|
+
|
235
|
+
## Contributing
|
236
|
+
|
237
|
+
This project is maintained by developers at [LaunchPad Lab](https://launchpadlab.com/). Contributions of any kind are welcome!
|
238
|
+
|
239
|
+
We aim to provide a response to incoming issues within 48 hours. However, please note that we are an active dev shop and these responses may be as simple as _"we do not have time to respond to this right now, but can address it at {x} time"_.
|
240
|
+
|
241
|
+
For detailed information specific to contributing to this project, reference our [Contribution guide](CONTRIBUTING.md).
|