Skip to content

Commit da73720

Browse files
author
Amy Sutedja
committed
Pluralization information in readme
1 parent 4f67b68 commit da73720

File tree

1 file changed

+80
-45
lines changed

1 file changed

+80
-45
lines changed

README.md

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,31 @@ Fast JSON API serialized 250 records in 3.01 ms
1818

1919
# Table of Contents
2020

21-
* [Features](#features)
22-
* [Installation](#installation)
23-
* [Usage](#usage)
24-
* [Rails Generator](#rails-generator)
25-
* [Model Definition](#model-definition)
26-
* [Serializer Definition](#serializer-definition)
27-
* [Object Serialization](#object-serialization)
28-
* [Compound Document](#compound-document)
29-
* [Key Transforms](#key-transforms)
30-
* [Collection Serialization](#collection-serialization)
31-
* [Caching](#caching)
32-
* [Params](#params)
33-
* [Conditional Attributes](#conditional-attributes)
34-
* [Conditional Relationships](#conditional-relationships)
35-
* [Sparse Fieldsets](#sparse-fieldsets)
36-
* [Contributing](#contributing)
37-
21+
- [Features](#features)
22+
- [Installation](#installation)
23+
- [Usage](#usage)
24+
- [Rails Generator](#rails-generator)
25+
- [Model Definition](#model-definition)
26+
- [Serializer Definition](#serializer-definition)
27+
- [Object Serialization](#object-serialization)
28+
- [Compound Document](#compound-document)
29+
- [Key Transforms](#key-transforms)
30+
- [Pluralize Type](#pluralize-type)
31+
- [Collection Serialization](#collection-serialization)
32+
- [Caching](#caching)
33+
- [Params](#params)
34+
- [Conditional Attributes](#conditional-attributes)
35+
- [Conditional Relationships](#conditional-relationships)
36+
- [Sparse Fieldsets](#sparse-fieldsets)
37+
- [Contributing](#contributing)
3838

3939
## Features
4040

41-
* Declaration syntax similar to Active Model Serializer
42-
* Support for `belongs_to`, `has_many` and `has_one`
43-
* Support for compound documents (included)
44-
* Optimized serialization of compound documents
45-
* Caching
41+
- Declaration syntax similar to Active Model Serializer
42+
- Support for `belongs_to`, `has_many` and `has_one`
43+
- Support for compound documents (included)
44+
- Optimized serialization of compound documents
45+
- Caching
4646

4747
## Installation
4848

@@ -61,6 +61,7 @@ $ bundle install
6161
## Usage
6262

6363
### Rails Generator
64+
6465
You can use the bundled generator if you are using the library inside of
6566
a Rails project:
6667

@@ -105,11 +106,13 @@ movie
105106
### Object Serialization
106107

107108
#### Return a hash
109+
108110
```ruby
109111
hash = MovieSerializer.new(movie).serializable_hash
110112
```
111113

112114
#### Return Serialized JSON
115+
113116
```ruby
114117
json_string = MovieSerializer.new(movie).serialized_json
115118
```
@@ -147,11 +150,11 @@ json_string = MovieSerializer.new(movie).serialized_json
147150
}
148151
}
149152
}
150-
151153
```
152154

153155
### Key Transforms
154-
By default fast_jsonapi underscores the key names. It supports the same key transforms that are supported by AMS. Here is the syntax of specifying a key transform
156+
157+
By default fast_jsonapi underscores the key names. It supports the same key transforms that are supported by AMS. Here is the syntax of specifying a key transform:
155158

156159
```ruby
157160
class MovieSerializer
@@ -160,7 +163,8 @@ class MovieSerializer
160163
set_key_transform :camel
161164
end
162165
```
163-
Here are examples of how these options transform the keys
166+
167+
Here are examples of how these options transform the keys.
164168

165169
```ruby
166170
set_key_transform :camel # "some_key" => "SomeKey"
@@ -169,10 +173,34 @@ set_key_transform :dash # "some_key" => "some-key"
169173
set_key_transform :underscore # "some_key" => "some_key"
170174
```
171175

176+
### Pluralize Type
177+
178+
By default fast_jsonapi does not pluralize type names. You can turn pluralization on using this syntax:
179+
180+
```ruby
181+
class AwardSerializer
182+
include FastJsonapi::ObjectSerializer
183+
belongs_to :actor
184+
pluralize_type true # "award" => "awards"
185+
end
186+
```
187+
188+
Relationship types are not automatically pluralized, even when their base types have `pluralize_type` set. Pluralization can be enabled in the relationship definition.
189+
190+
```ruby
191+
class ActorSerializer
192+
include FastJsonapi::ObjectSerializer
193+
has_many :awards, pluralize_type: true # "award" => "awards"
194+
end
195+
```
196+
197+
The most common use case for this feature is to easily migrate from serialization engines that pluralize by default, such as AMS.
198+
172199
### Attributes
173-
Attributes are defined in FastJsonapi using the `attributes` method. This method is also aliased as `attribute`, which is useful when defining a single attribute.
174200

175-
By default, attributes are read directly from the model property of the same name. In this example, `name` is expected to be a property of the object being serialized:
201+
Attributes are defined in FastJsonapi using the `attributes` method. This method is also aliased as `attribute`, which is useful when defining a single attribute.
202+
203+
By default, attributes are read directly from the model property of the same name. In this example, `name` is expected to be a property of the object being serialized:
176204

177205
```ruby
178206
class MovieSerializer
@@ -221,6 +249,7 @@ end
221249
```
222250

223251
### Links Per Object
252+
224253
Links are defined in FastJsonapi using the `link` method. By default, links are read directly from the model property of the same name. In this example, `public_url` is expected to be a property of the object being serialized.
225254

226255
You can configure the method to use on the object for example a link with key `self` will get set to the value returned by a method called `url` on the movie object.
@@ -276,6 +305,7 @@ This will create a `self` reference for the relationship, and a `related` link f
276305
### Meta Per Resource
277306

278307
For every resource in the collection, you can include a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.
308+
279309
```ruby
280310
meta do |movie|
281311
{
@@ -287,7 +317,7 @@ end
287317

288318
### Compound Document
289319

290-
Support for top-level and nested included associations through ` options[:include] `.
320+
Support for top-level and nested included associations through `options[:include]`.
291321

292322
```ruby
293323
options = {}
@@ -331,10 +361,11 @@ options[:is_collection]
331361
was introduced to be able to have precise control this behavior
332362

333363
- `nil` or not provided: will try to autodetect single vs collection (please, see notes above)
334-
- `true` will always treat input resource as *collection*
335-
- `false` will always treat input resource as *single object*
364+
- `true` will always treat input resource as _collection_
365+
- `false` will always treat input resource as _single object_
336366

337367
### Caching
368+
338369
Requires a `cache_key` method be defined on model:
339370

340371
```ruby
@@ -446,18 +477,18 @@ serializer.serializable_hash
446477

447478
### Customizable Options
448479

449-
Option | Purpose | Example
450-
------------ | ------------- | -------------
451-
set_type | Type name of Object | ```set_type :movie ```
452-
key | Key of Object | ```belongs_to :owner, key: :user ```
453-
set_id | ID of Object | ```set_id :owner_id ``` or ```set_id { |record| "#{record.name.downcase}-#{record.id}" }```
454-
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true, cache_length: 12.hours, race_condition_ttl: 10.seconds```
455-
id_method_name | Set custom method name to get ID of an object (If block is provided for the relationship, `id_method_name` is invoked on the return value of the block instead of the resource object) | ```has_many :locations, id_method_name: :place_ids ```
456-
object_method_name | Set custom method name to get related objects | ```has_many :locations, object_method_name: :places ```
457-
record_type | Set custom Object Type for a relationship | ```belongs_to :owner, record_type: :user```
458-
serializer | Set custom Serializer for a relationship | ```has_many :actors, serializer: :custom_actor``` or ```has_many :actors, serializer: MyApp::Api::V1::ActorSerializer```
459-
polymorphic | Allows different record types for a polymorphic association | ```has_many :targets, polymorphic: true```
460-
polymorphic | Sets custom record types for each object class in a polymorphic association | ```has_many :targets, polymorphic: { Person => :person, Group => :group }```
480+
| Option | Purpose | Example |
481+
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
482+
| set_type | Type name of Object | `set_type :movie` |
483+
| key | Key of Object | `belongs_to :owner, key: :user` |
484+
| set_id | ID of Object | `set_id :owner_id` or `set_id { |record| "#{record.name.downcase}-#{record.id}" }` |
485+
| cache_options | Hash to enable caching and set cache length | `cache_options enabled: true, cache_length: 12.hours, race_condition_ttl: 10.seconds` |
486+
| id_method_name | Set custom method name to get ID of an object (If block is provided for the relationship, `id_method_name` is invoked on the return value of the block instead of the resource object) | `has_many :locations, id_method_name: :place_ids` |
487+
| object_method_name | Set custom method name to get related objects | `has_many :locations, object_method_name: :places` |
488+
| record_type | Set custom Object Type for a relationship | `belongs_to :owner, record_type: :user` |
489+
| serializer | Set custom Serializer for a relationship | `has_many :actors, serializer: :custom_actor` or `has_many :actors, serializer: MyApp::Api::V1::ActorSerializer` |
490+
| polymorphic | Allows different record types for a polymorphic association | `has_many :targets, polymorphic: true` |
491+
| polymorphic | Sets custom record types for each object class in a polymorphic association | `has_many :targets, polymorphic: { Person => :person, Group => :group }` |
461492

462493
### Instrumentation
463494

@@ -474,8 +505,9 @@ require 'fast_jsonapi/instrumentation'
474505
```
475506

476507
The two instrumented notifcations are supplied by these two constants:
477-
* `FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION`
478-
* `FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION`
508+
509+
- `FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION`
510+
- `FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION`
479511

480512
It is also possible to instrument one method without the other by using one of the following require statements:
481513

@@ -485,15 +517,18 @@ require 'fast_jsonapi/instrumentation/serialized_json'
485517
```
486518

487519
Same goes for the Skylight integration:
520+
488521
```ruby
489522
require 'fast_jsonapi/instrumentation/skylight/normalizers/serializable_hash'
490523
require 'fast_jsonapi/instrumentation/skylight/normalizers/serialized_json'
491524
```
492525

493526
## Contributing
527+
494528
Please see [contribution check](https://github.com/Netflix/fast_jsonapi/blob/master/CONTRIBUTING.md) for more details on contributing
495529

496530
### Running Tests
531+
497532
We use [RSpec](http://rspec.info/) for testing. We have unit tests, functional tests and performance tests. To run tests use the following command:
498533

499534
```bash
@@ -516,4 +551,4 @@ rspec spec --tag performance:true
516551

517552
Join the Netflix Studio Engineering team and help us build gems like this!
518553

519-
* [Senior Ruby Engineer](https://jobs.netflix.com/jobs/864893)
554+
- [Senior Ruby Engineer](https://jobs.netflix.com/jobs/864893)

0 commit comments

Comments
 (0)