Skip to content

Commit

Permalink
Issue #142: Only one generator is documented (#146)
Browse files Browse the repository at this point in the history
* document behavior added to rails resource generator

* update truncatestringdecanter to truncatedstringdecanter in readme

* subheadings for generators

* link to details on custom parsers

* ensure all generators have the generated file(s) documented

* bump version to 4.0.4

* PR feedback

* remove extraneous period
  • Loading branch information
nicoledow authored Jan 11, 2024
1 parent 682539d commit 5234600
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
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

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

0 comments on commit 5234600

Please sign in to comment.