Skip to content

Commit 8e81998

Browse files
glaucocustodiodblock
authored andcommitted
Updated README showing how to reuse formatters (#222) [ci skip]
1 parent eb32624 commit 8e81998

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,41 @@ expose :replies, using: API::Entities::Status, as: :responses
311311
Apply a formatter before exposing a value.
312312

313313
```ruby
314-
format_with(:iso_timestamp) { |dt| dt.iso8601 }
315-
with_options(format_with: :iso_timestamp) do
316-
expose :created_at
317-
expose :updated_at
314+
module Entities
315+
class MyModel < Grape::Entity
316+
format_with(:iso_timestamp) do |date|
317+
date.iso8601
318+
end
319+
320+
with_options(format_with: :iso_timestamp) do
321+
expose :created_at
322+
expose :updated_at
323+
end
324+
end
325+
end
326+
```
327+
328+
Defining a reusable formatter between multiples entities:
329+
330+
```ruby
331+
module ApiHelpers
332+
extend Grape::API::Helpers
333+
334+
Grape::Entity.format_with :utc do |date|
335+
date.utc if date
336+
end
337+
end
338+
```
339+
340+
```ruby
341+
module Entities
342+
class MyModel < Grape::Entity
343+
expose :updated_at, format_with: :utc
344+
end
345+
346+
class AnotherModel < Grape::Entity
347+
expose :created_at, format_with: :utc
348+
end
318349
end
319350
```
320351

0 commit comments

Comments
 (0)