Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #142: Only one generator is documented #146

Merged
merged 9 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
decanter (4.0.3)
decanter (4.0.4)
actionpack (>= 4.2.10)
activesupport
rails-html-sanitizer (>= 1.0.4)
Expand Down
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,48 @@ Then, transform incoming params in your controller using `Decanter#decant`:

### Generators

Decanter comes with generators for creating `Decanter` and `Parser` files:
Decanter comes with custom generators for creating `Decanter` and `Parser` files:

#### Decanters

```
rails g decanter Trip name:string start_date:date end_date:date

# Creates app/decanters/trip_decanter.rb:
class TripDecanter < Decanter::Base
input :name, :string
input :start_date, :date
input :end_date, :date
end
```

#### Parsers
```
rails g parser TruncatedString

# Creates lib/decanter/parsers/truncated_string_parser.rb:
class TruncatedStringParser < Decanter::Parser::ValueParser
parser do |value, options|
value
end
end
```

[Learn more about using custom parsers](#custom-parsers)

#### Resources

When using the Rails resource generator in a project that includes Decanter, a decanter will be automatically created for the new resource:

```
rails g resource Trip name:string start_date:date end_date:date

# Creates app/decanters/trip_decanter.rb:
class TripDecanter < Decanter::Base
input :name, :string
input :start_date, :date
input :end_date, :date
end
```

### Decanting Collections
Expand Down Expand Up @@ -184,8 +218,8 @@ You can also disable strict mode globally using a [global configuration](#global
To add a custom parser, first create a parser class:

```rb
# app/parsers/truncate_string_parser.rb
class TruncateStringParser < Decanter::Parser::ValueParser
# app/parsers/truncated_string_parser.rb
class TruncatedStringParser < Decanter::Parser::ValueParser
chawes13 marked this conversation as resolved.
Show resolved Hide resolved

parser do |value, options|
length = options.fetch(:length, 100)
Expand All @@ -197,7 +231,7 @@ end
Then, use the appropriate key to look up the parser:

```ruby
input :name, :truncate_string #=> TruncateStringParser
input :name, :truncated_string #=> TruncatedStringParser
```

#### Custom parser methods
Expand Down
2 changes: 1 addition & 1 deletion lib/decanter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Decanter
VERSION = '4.0.3'.freeze
VERSION = '4.0.4'.freeze
end