README.md in decanter-4.0.3 vs README.md in decanter-4.0.4

- old
+ new

@@ -59,20 +59,54 @@ ``` ### 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 Decanter can decant a collection of a resource, applying the patterns used in the [fast JSON API gem](https://github.com/Netflix/fast_jsonapi#collection-serialization): ```rb @@ -182,12 +216,12 @@ ### Custom Parsers 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) value.truncate(length) end @@ -195,10 +229,10 @@ ``` Then, use the appropriate key to look up the parser: ```ruby - input :name, :truncate_string #=> TruncateStringParser + input :name, :truncated_string #=> TruncatedStringParser ``` #### Custom parser methods - `#parse <block>`: (required) recieves a block for parsing a value. Block parameters are `|value, options|` for `ValueParser` and `|name, value, options|` for `HashParser`.