panko_serializer 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +6 -0
  3. data/.github/workflows/database_matrix.yml +85 -0
  4. data/.github/workflows/docs.yml +5 -5
  5. data/.github/workflows/lint.yml +10 -16
  6. data/.github/workflows/{ruby.yml → tests.yml} +9 -22
  7. data/.gitignore +2 -0
  8. data/.rubocop.yml +29 -21
  9. data/Appraisals +23 -12
  10. data/Gemfile +12 -3
  11. data/README.md +4 -4
  12. data/Rakefile +3 -1
  13. data/benchmarks/allocs.rb +2 -2
  14. data/benchmarks/benchmarking_support.rb +2 -1
  15. data/benchmarks/bm_ams_0_10.rb +3 -7
  16. data/benchmarks/bm_panko_json.rb +2 -6
  17. data/benchmarks/bm_panko_object.rb +2 -6
  18. data/benchmarks/bm_plain_object.rb +1 -4
  19. data/benchmarks/bm_serialization_descriptor.rb +1 -1
  20. data/benchmarks/bm_to_object.rb +2 -6
  21. data/benchmarks/profile.rb +2 -2
  22. data/benchmarks/sanity.rb +2 -6
  23. data/benchmarks/setup.rb +4 -3
  24. data/benchmarks/type_casts/support.rb +0 -1
  25. data/docs/docs/associations.md +28 -15
  26. data/docs/docs/attributes.md +33 -20
  27. data/docs/docs/design-choices.md +36 -35
  28. data/docs/docs/getting-started.md +13 -7
  29. data/docs/docs/introduction.md +6 -6
  30. data/docs/docs/performance.md +4 -5
  31. data/docs/docs/response-bag.md +13 -6
  32. data/docs/docusaurus.config.js +86 -0
  33. data/docs/package-lock.json +13999 -18188
  34. data/docs/package.json +10 -6
  35. data/docs/src/css/customTheme.css +9 -0
  36. data/docs/static/CNAME +1 -0
  37. data/ext/panko_serializer/attributes_writer/active_record.c +81 -77
  38. data/ext/panko_serializer/attributes_writer/active_record.h +2 -0
  39. data/ext/panko_serializer/attributes_writer/type_cast/type_cast.c +1 -1
  40. data/gemfiles/{6.1.0.gemfile → 7.2.0.gemfile} +17 -6
  41. data/gemfiles/7.2.0.gemfile.lock +221 -0
  42. data/gemfiles/{7.0.0.gemfile → 8.0.0.gemfile} +17 -6
  43. data/gemfiles/8.0.0.gemfile.lock +239 -0
  44. data/gemfiles/{7.1.0.gemfile → 8.1.0.gemfile} +17 -6
  45. data/gemfiles/8.1.0.gemfile.lock +248 -0
  46. data/lib/panko/version.rb +1 -1
  47. data/panko_serializer.gemspec +1 -1
  48. metadata +16 -21
  49. data/.standard.yml +0 -5
  50. data/docs/.DS_Store +0 -0
  51. data/docs/README.md +0 -198
  52. data/docs/core/Footer.js +0 -80
  53. data/docs/i18n/en.json +0 -50
  54. data/docs/siteConfig.js +0 -80
  55. data/gemfiles/6.1.0.gemfile.lock +0 -166
  56. data/gemfiles/7.0.0.gemfile.lock +0 -164
  57. data/gemfiles/7.1.0.gemfile.lock +0 -181
@@ -3,18 +3,19 @@ id: associations
3
3
  title: Associations
4
4
  sidebar_label: Associations
5
5
  ---
6
-
7
- A serializer can define it's own associations - both `has_many` and `has_one` to serializer under the context of the object.
6
+ A serializer can define it's own associations - both `has_many` and `has_one` to serialize under the context of the object.
8
7
 
9
8
  For example:
10
9
 
11
10
  ```ruby
11
+
12
12
  class PostSerializer < Panko::Serializer
13
13
  attributes :title, :body
14
14
 
15
15
  has_one :author, serializer: AuthorSerializer
16
16
  has_many :comments, each_serializer: CommentSerializer
17
17
  end
18
+
18
19
  ```
19
20
 
20
21
  ### Associations with aliases
@@ -23,44 +24,52 @@ An association key name can be aliased with the `name` option.
23
24
 
24
25
  For example:
25
26
  the `actual_author` property will be converted to `alias_author`.
27
+
26
28
  ```ruby
29
+
27
30
  class PostSerializer < Panko::Serializer
28
31
  attributes :title, :body
29
32
 
30
33
  has_one :actual_author, serializer: AuthorSerializer, name: :alias_author
31
34
  has_many :comments, each_serializer: CommentSerializer
32
35
  end
36
+
33
37
  ```
38
+
34
39
  ### Inference
35
40
 
36
- Panko can find the type of the serializer by looking at the realtionship name, so instead specifying
37
- the serializer at the above example, we can -
41
+ Panko can find the type of the serializer by looking at the relationship name, so instead of specifying
42
+ the serializer at the above example, we can:
38
43
 
39
44
  ```ruby
45
+
40
46
  class PostSerializer < Panko::Serializer
41
47
  attributes :title, :body
42
48
 
43
49
  has_one :author
44
50
  has_many :comments
45
51
  end
52
+
46
53
  ```
47
54
 
48
- The logic of inferencing is -
49
- - Take the name of the relationship (for example - `:author` / `:comments`) singularize and camelize it
50
- - Look for const defined with the name aboe and "Serializer" suffix (by using `Object.const_get`)
55
+ The logic of inferencing is:
56
+
57
+ - Take the name of the relationship (for example - `:author` / `:comments`) singularize and camelize it.
58
+ - Look for const defined with the name above and "Serializer" suffix (by using `Object.const_get`).
51
59
 
52
- > If Panko can't find the serializer it will throw an error on startup time, for example: `Can't find serializer for PostSerializer.author has_one relationship`
60
+ &gt; If Panko can't find the serializer it will throw an error on startup time, for example: `Can't find serializer for PostSerializer.author has_one relationship`.
53
61
 
54
62
  ## Nested Filters
55
63
 
56
64
  As talked before, Panko allows you to filter the attributes of a serializer.
57
- But Panko let you take that step further, and filters the attributes of you associations so you can re-use your serializers in your application.
65
+ But Panko lets you take that step further, and filters the attributes of you associations so you can re-use your serializers in your application.
58
66
 
59
- For example, let's say one portion of the application needs to serializer list of posts and serializer their - `title`, `body`, author's id and comments id.
67
+ For example, let's say one portion of the application needs to serialize a list of posts but only with their - `title`, `body`, author's id and comments id.
60
68
 
61
69
  We can declare tailored serializer for this, or we can re-use the above defined serializer - `PostSerializer` and use nested filters.
62
70
 
63
71
  ```ruby
72
+
64
73
  posts = Post.all
65
74
 
66
75
  Panko::ArraySerializer.new(posts, each_serializer: PostSerializer, only: {
@@ -68,17 +77,20 @@ Panko::ArraySerializer.new(posts, each_serializer: PostSerializer, only: {
68
77
  author: [:id],
69
78
  comments: [:id],
70
79
  })
80
+
71
81
  ```
72
82
 
73
- Let's dissect `only` option we passed -
74
- * `instance` - list of attributes (and associations) we want to serializer for current instance of the serializer, in this case - `PostSerializer`.
75
- * `author`, `comments` - here we specify the list of attributes we want to serialize for each association.
83
+ Let's dissect the `only` option we passed:
76
84
 
77
- It's important to note that Nested Filters, are recursive, in other words, we can filter the association's associations.
85
+ - `instance` - list of attributes (and associations) we want to serialize for the current instance of the serializer, in this case - `PostSerializer`.
86
+ - `author`, `comments` - here we specify the list of attributes we want to serialize for each association.
78
87
 
79
- For example, `CommentSerializer` have has_one association `Author`, and for each `comments.author` we only it's name.
88
+ It's important to note that Nested Filters are recursive, in other words, we can filter the association's associations.
89
+
90
+ For example, `CommentSerializer` has an `has_one` association `Author`, and for each `comments.author` we can only serialize it's name.
80
91
 
81
92
  ```ruby
93
+
82
94
  posts = Post.all
83
95
 
84
96
  Panko::ArraySerializer.new(posts, only: {
@@ -89,6 +101,7 @@ Panko::ArraySerializer.new(posts, only: {
89
101
  author: [:name]
90
102
  }
91
103
  })
104
+
92
105
  ```
93
106
 
94
107
  As you see now in `comments` the `instance` have different meaning, the `CommentSerializer`.
@@ -3,15 +3,15 @@ id: attributes
3
3
  title: Attributes
4
4
  sidebar_label: Attributes
5
5
  ---
6
+ Attributes allow you to specify which record attributes you want to serialize.
6
7
 
7
- Attributes allow you to specify which record attributes you want to serialize,
8
8
  There are two types of attributes:
9
9
 
10
- * Field - simple columns defined on the record it self.
11
- * Virtual/Method - this allows to include properties beyond simple fields.
12
-
10
+ - Field - simple columns defined on the record it self.
11
+ - Virtual/Method - this allows to include properties beyond simple fields.
13
12
 
14
13
  ```ruby
14
+
15
15
  class UserSerializer < Panko::Serializer
16
16
  attributes :full_name
17
17
 
@@ -19,22 +19,23 @@ class UserSerializer < Panko::Serializer
19
19
  "#{object.first_name} #{object.last_name}"
20
20
  end
21
21
  end
22
+
22
23
  ```
23
24
 
24
25
  ## Field Attributes
25
26
 
26
27
  Using field attributes you can control which columns of the given ActiveRecord object you want to serialize.
27
28
 
28
- Instead of relying ActiveRecord to do it's type casting, Panko does on it's own for performance reasons (read more in [Design Choices](design-choices.md#type-casting)).
29
-
29
+ Instead of relying on ActiveRecord to do it's type casting, Panko does on it's own for performance reasons (read more in [Design Choices](design-choices.md#type-casting)).
30
30
 
31
31
  ## Method Attributes
32
32
 
33
33
  Method attributes are used when your serialized values can be derived from the object you are serializing.
34
34
 
35
- The serializer's attribute methods can access the object being serialized as `object` -
35
+ The serializer's attribute methods can access the object being serialized as `object`:
36
36
 
37
37
  ```ruby
38
+
38
39
  class PostSerializer < Panko::Serializer
39
40
  attributes :author_name
40
41
 
@@ -42,12 +43,15 @@ class PostSerializer < Panko::Serializer
42
43
  "#{object.author.first_name} #{object.author.last_name}"
43
44
  end
44
45
  end
46
+
45
47
  ```
46
48
 
47
- Another useful, thing you can pass your serializer is `context`, a `context` is a bag of data whom your serializer may need.
49
+ Another useful thing you can pass your serializer is `context`, a `context` is a bag of data whom your serializer may need.
48
50
 
49
51
  For example, here we will pass feature flags:
52
+
50
53
  ```ruby
54
+
51
55
  class UserSerializer < Panko::Serializer
52
56
  attributes :id, :email
53
57
 
@@ -61,6 +65,7 @@ serializer = UserSerializer.new(context: {
61
65
  })
62
66
 
63
67
  serializer.serialize(User.first)
68
+
64
69
  ```
65
70
 
66
71
  ## Filters
@@ -68,11 +73,14 @@ serializer.serialize(User.first)
68
73
  Filters allows us to reduce the amount of attributes we can serialize, therefore reduce the data usage & performance of serializing.
69
74
 
70
75
  There are two types of filters:
71
- * only - use those attributes **only** and nothing else
72
- * except - all attributes **except** those attributes
76
+
77
+ - only - use those attributes **only** and nothing else.
78
+ - except - all attributes **except** those attributes.
73
79
 
74
80
  Usage example:
81
+
75
82
  ```ruby
83
+
76
84
  class UserSerializer < Panko::Serializer
77
85
  attributes :id, :name, :email
78
86
  end
@@ -82,20 +90,20 @@ UserSerializer.new(only: [:name]).serialize(User.first)
82
90
 
83
91
  # this line will return { 'id': '..', 'email': ... }
84
92
  UserSerializer.new(except: [:name]).serialize(User.first)
93
+
85
94
  ```
86
95
 
87
- > **Note** that if you want to user filter on an associations, the `:name`
88
- > property is not taken into account.
89
- > If you have a `has_many :state_transitions, name: :history` association
90
- > defined, the key to use in filters is `:state_transitions`
91
- > (e.g. `{ except: [:state_transitions] }`)
96
+ &gt; **Note** that if you want to user filter on an associations, the `:name` property is not taken into account.
97
+ If you have a `has_many :state_transitions, name: :history` association defined, the key to use in filters is
98
+ `:state_transitions` (e.g. `{ except: [:state_transitions] }`).
92
99
 
93
100
  ## Filters For
94
101
 
95
- Sometimes you find yourself have the same filtering logic in actions in order to
102
+ Sometimes you find yourself having the same filtering logic in actions. In order to
96
103
  solve this duplication, Panko allows you to write the filters in the serializer.
97
104
 
98
105
  ```ruby
106
+
99
107
  class UserSerializer < Panko::Serializer
100
108
  attributes :id, :name, :email
101
109
 
@@ -108,15 +116,17 @@ end
108
116
 
109
117
  # this line will return { 'name': '..' }
110
118
  UserSerializer.serialize(User.first)
119
+
111
120
  ```
112
121
 
113
- > See discussion in: https://github.com/yosiat/panko_serializer/issues/16
122
+ &gt; See discussion in: [https:](https://github.com/yosiat/panko_serializer/issues/16)
114
123
 
115
124
  ## Aliases
116
125
 
117
- Let's say we have attribute name that we want to expose to client as different name, the current way of doing so is using method attribute, for example:
126
+ Let's say we have an attribute name that we want to expose to client as different name, the current way of doing so is using method attribute, for example:
118
127
 
119
128
  ```ruby
129
+
120
130
  class PostSerializer < Panko::Serializer
121
131
  attributes :published_at
122
132
 
@@ -124,14 +134,17 @@ class PostSerializer < Panko::Serializer
124
134
  object.created_at
125
135
  end
126
136
  end
137
+
127
138
  ```
128
139
 
129
- The downside of this approach is that `created_at` skips Panko's type casting, therefore we get direct hit on performance.
140
+ The downside of this approach is that `created_at` skips Panko's type casting, therefore we get a direct hit on performance.
130
141
 
131
- To fix this, we can use aliases -
142
+ To fix this, we can use aliases:
132
143
 
133
144
  ```ruby
145
+
134
146
  class PostSerializer < Panko::Serializer
135
147
  aliases created_at: :published_at
136
148
  end
149
+
137
150
  ```
@@ -3,24 +3,23 @@ id: design-choices
3
3
  title: Design Choices
4
4
  sidebar_label: Design Choices
5
5
  ---
6
-
7
- In short, Panko, is a serializer for ActiveRecord objects (it can't serialize any other object), which strives for high performance & simple API (which is inspired by ActiveModelSerializers).
6
+ In short, Panko is a serializer for ActiveRecord objects (it can't serialize any other object), which strives for high performance & simple API (which is inspired by ActiveModelSerializers).
8
7
 
9
8
  Its performance is achieved by:
10
9
 
11
- * `Oj::StringWriter` - I will elaborate later.
12
- * Type casting — instead of relying on ActiveRecord to do its type cast, Panko is doing it by itself.
13
- * Figuring out the metadata, ahead of time — therefore, we ask less questions during the `serialization loop`.
14
-
10
+ - `Oj::StringWriter` - I will elaborate later.
11
+ - Type casting — instead of relying on ActiveRecord to do its type cast, Panko is doing it by itself.
12
+ - Figuring out the metadata, ahead of time — therefore, we ask less questions during the `serialization loop`.
15
13
 
16
14
  ## Serialization overview
17
15
 
18
- First, let's start with overview. Let's say we want to serialize `User` object, which has
16
+ First, let's start with an overview. Let's say we want to serialize an `User` object, which has
19
17
  `first_name`, `last_name`, `age`, and `email` properties.
20
18
 
21
19
  The serializer definition will be something like this:
22
20
 
23
21
  ```ruby
22
+
24
23
  class UserSerializer < Panko::Serializer
25
24
  attributes :name, :age, :email
26
25
 
@@ -28,11 +27,13 @@ class UserSerializer < Panko::Serializer
28
27
  "#{object.first_name} #{object.last_name}"
29
28
  end
30
29
  end
30
+
31
31
  ```
32
32
 
33
33
  And the usage of this serializer will be:
34
34
 
35
35
  ```ruby
36
+
36
37
  # fetch user from database
37
38
  user = User.first
38
39
 
@@ -41,29 +42,30 @@ serializer = UserSerializer.new
41
42
 
42
43
  # serialize to JSON
43
44
  serializer.serialize_to_json(user)
45
+
44
46
  ```
45
47
 
46
48
  Let's go over the steps that Panko will execute behind the scenes for this flow.
47
- _I will skip the serializer definition part, because it's fairly simple and straightforward (see `lib/panko/serializer.rb`)_
49
+ _I will skip the serializer definition part, because it's fairly simple and straightforward (see `lib/panko/serializer.rb`)._
48
50
 
49
51
  First step, while initializing the UserSerializer, we will create a **Serialization Descriptor** for this class.
50
52
  Serialization Descriptor's goal is to answer those questions:
51
53
 
52
- * Which fields do we have? In our case, `:age`, `:email`
53
- * Which method fields do we have? In our case `:name`
54
- * Which associations do we have (and their serialization descriptors)
54
+ - Which fields do we have? In our case, `:age`, `:email`.
55
+ - Which method fields do we have? In our case `:name`.
56
+ - Which associations do we have (and their serialization descriptors)?
55
57
 
56
- The serialization description is also responsible for filtering the attributes (`only` \ `except`).
58
+ The serialization description is also responsible for filtering the attributes (`only` \\ `except`).
57
59
 
58
- Now, that we have the serialization descriptor, we are finished with the Ruby part of Panko, and all we did here is done in *initialization time* and now we move to C code.
60
+ Now, that we have the serialization descriptor, we are finished with the Ruby part of Panko, and all we did here is done in _initialization time_ and now we move to C code.
59
61
 
60
62
  In C land, we take the `user` object and the serialization descriptor, and start the serialization process which is separated to 4 parts:
61
63
 
62
- * Serializing Fields - looping through serialization descriptor's `fields` and read them from the ActiveRecord object (see `Type Casting`) and write them to the writer.
63
- * Serializing Method Fields - creating (a cached) serializer instance, setting its `@object` and `@context`, calling all the method fields and writing them to the writer.
64
- * Serializing associations — this is simple, once we have fields + method fields, we just repeat the process.
64
+ - Serializing Fields - looping through serialization descriptor's `fields` and read them from the ActiveRecord object (see `Type Casting`) and write them to the writer.
65
+ - Serializing Method Fields - creating (a cached) serializer instance, setting its `@object` and `@context`, calling all the method fields and writing them to the writer.
66
+ - Serializing associations — this is simple, once we have fields + method fields, we just repeat the process.
65
67
 
66
- Once this is finished, we have nice JSON string.
68
+ Once this is finished, we have a nice JSON string.
67
69
  Now let's dig deeper.
68
70
 
69
71
  ## Interesting parts
@@ -72,37 +74,36 @@ Now let's dig deeper.
72
74
 
73
75
  If you read the code of ActiveRecord serialization code in Ruby, you will observe this flow:
74
76
 
75
- 1. Get an array of ActiveRecord objects (`User.all` for example)
76
- 2. Build new array of hashes where each hash is `User` with the attributes we selected
77
- 3. The JSON serializer, takes this array of hashes and loop them, and converts it to JSON string
77
+ 1. Get an array of ActiveRecord objects (`User.all` for example).
78
+ 2. Build a new array of hashes where each hash is an `User` with the attributes we selected.
79
+ 3. The JSON serializer, takes this array of hashes and loop them, and converts it to a JSON string.
78
80
 
79
81
  This entire process is expensive in terms of Memory & CPU, and this where the combination of Panko and Oj::StringWriter really shines.
80
82
 
81
83
  In Panko, the serialization process of the above is:
82
84
 
83
- 1. Get an array of ActiveRecord objects (`User.all` for example)
84
- 2. Create `Oj::StringWriter` and feed the values to it, via `push_value` / `push_object` / `push_object` and behind the scene, `Oj::StringWriter` will serialize the objects incrementally into a string.
85
- 3. Get from `Oj::StringWriter` the completed JSON string — which is a no-op, since `Oj::StringWriter` already built the string.
85
+ 1. Get an array of ActiveRecord objects (`User.all` for example).
86
+ 2. Create `Oj::StringWriter` and feed the values to it, via `push_value` / `push_object` / `push_object` and behind the scene, `Oj::StringWriter` will serialize the objects incrementally into a string.
87
+ 3. Get from `Oj::StringWriter` the completed JSON string — which is a no-op, since `Oj::StringWriter` already built the string.
86
88
 
87
89
  ### Figuring out the metadata, ahead of time.
88
90
 
89
- Another observation I noticed in the ruby serializers is that they ask and do a lot in a serialization loop:
91
+ Another observation I noticed in the Ruby serializers is that they ask and do a lot in a serialization loop:
90
92
 
91
- * Is this field a method? is it a property?
92
- * Which fields and associations do I need for the serializer to consider the `only` and `except` options
93
- * What is the serializer of this has_one association?
93
+ - Is this field a method? is it a property?
94
+ - Which fields and associations do I need for the serializer to consider the `only` and `except` options?
95
+ - What is the serializer of this has_one association?
94
96
 
95
97
  Panko tries to ask the bare minimum in serialization by building `Serialization Descriptor` for each serialization and caching it.
96
98
 
97
- The Serialization Descriptor will do the filtering of `only` and `except` and will check if a field is a method or not (therefore Panko doesn't have list of `attributes`)
98
-
99
+ The Serialization Descriptor will do the filtering of `only` and `except` and will check if a field is a method or not (therefore Panko doesn't have list of `attributes`).
99
100
 
100
101
  ### Type Casting
101
102
 
102
103
  This is the final part, which helped yield most of the performance improvements.
103
- In ActiveRecord, when we read a value of attribute, it does type casting of the DB value to its real ruby type.
104
+ In ActiveRecord, when we read the value of an attribute, it does type casting of the DB value to its real Ruby type.
104
105
 
105
- For example, time strings are converted to Time objects, Strings are duplicated, and Integers are converts from their values to Number.
106
+ For example, time strings are converted to Time objects, Strings are duplicated, and Integers are converted from their values to Number.
106
107
 
107
108
  This type casting is really expensive, as it's responsible for most of the allocations in the serialization flow and most of them can be "relaxed".
108
109
 
@@ -114,13 +115,13 @@ If we have an integer string value, we will convert it to an integer, and the sa
114
115
  All of these conversions are done in C, which of course yields a big performance improvement.
115
116
 
116
117
  #### Time type casting
118
+
117
119
  While you read Panko source code, you will encounter the time type casting and immediately you will have a "WTF?" moment.
118
120
 
119
121
  The idea behind the time type casting code relies on the end result of JSON type casting — what we need in order to serialize Time to JSON? UTC ISO8601 time format representation.
120
122
 
121
123
  The time type casting works as follows:
122
124
 
123
- * If it's a string that ends with `Z`, and the strings matches the UTC ISO8601 regex, then we just return the string.
124
- * If it's a string and it doesn't follow the rules above, we check if it's a timestamp in database format and convert it via regex + string concat to UTC ISO8601 - Yes, there is huge assumption here, that the database returns UTC timestamps — this will be configureable (before Panko official release).
125
- * If it's none of the above, I will let ActiveRecord type casting do it's magic.
126
-
125
+ - If it's a string that ends with `Z`, and the strings matches the UTC ISO8601 regex, then we just return the string.
126
+ - If it's a string and it doesn't follow the rules above, we check if it's a timestamp in database format and convert it via regex + string concat to UTC ISO8601 - Yes, there is huge assumption here, that the database returns UTC timestamps — this will be configurable (before Panko official release).
127
+ - If it's none of the above, I will let ActiveRecord type casting do it's magic.
@@ -3,25 +3,27 @@ id: getting-started
3
3
  title: Getting Started
4
4
  sidebar_label: Getting Started
5
5
  ---
6
-
7
6
  ## Installation
8
7
 
9
8
  To install Panko, all you need is to add it to your Gemfile:
10
9
 
11
10
  ```ruby
11
+
12
12
  gem "panko_serializer"
13
+
13
14
  ```
14
15
 
15
16
  Then, install it on the command line:
16
17
 
17
18
  ```
18
- > bundle install
19
- ```
20
19
 
20
+ bundle install
21
+
22
+ ```
21
23
 
22
24
  ## Creating your first serializer
23
25
 
24
- Let's create serializer and use it inside Rails controller.
26
+ Let's create a serializer and use it inside of a Rails controller:
25
27
 
26
28
  ```ruby
27
29
  class PostSerializer < Panko::Serializer
@@ -37,28 +39,32 @@ end
37
39
 
38
40
  ### Serializing an object
39
41
 
40
- And now serialize a single object
42
+ And now serialize a single object:
41
43
 
42
44
  ```ruby
45
+
43
46
  # Using Oj serializer
44
47
  PostSerializer.new.serialize_to_json(Post.first)
45
48
 
46
49
  # or, similar to #serializable_hash
47
50
  PostSerializer.new.serialize(Post.first).to_json
51
+
48
52
  ```
49
53
 
50
54
  ### Using the serializers in a controller
51
55
 
52
- As you can see, defining serializers is simple and resembles ActiveModelSerializers 0.9,
56
+ As you can see, defining serializers is simple and resembles ActiveModelSerializers 0.9.
53
57
  To utilize the `UserSerializer` inside a Rails controller and serialize some users, all we need to do is:
54
58
 
55
59
  ```ruby
60
+
56
61
  class UsersController < ApplicationController
57
62
  def index
58
63
  users = User.includes(:posts).all
59
64
  render json: Panko::ArraySerializer.new(users, each_serializer: UserSerializer).to_json
60
65
  end
61
66
  end
67
+
62
68
  ```
63
69
 
64
- And voila, we have endpoint which serialize users using Panko!
70
+ And voila, we have an endpoint which serializes users using Panko!
@@ -2,12 +2,12 @@
2
2
  id: index
3
3
  title: Introduction
4
4
  sidebar_label: Introduction
5
+ slug: /
5
6
  ---
7
+ Panko is a library which is inspired by ActiveModelSerializers 0.9 for serializing ActiveRecord/Ruby objects to JSON strings, fast.
6
8
 
7
- Panko is library which is inspired by ActiveModelSerializers 0.9 for serializing ActiveRecord/Ruby objects to JSON strings, fast.
9
+ To achieve it's [performance](https://panko.dev/performance/):
8
10
 
9
- To achieve it's [performance](https://panko.dev/docs/performance.html):
10
-
11
- * Oj - Panko relies Oj since it's fast and allow to serialize incrementally using `Oj::StringWriter`
12
- * Serialization Descriptor - Panko computes most of the metadata ahead of time, to save time later in serialization.
13
- * Type casting — Panko does type casting by it's self, instead of relying ActiveRecord.
11
+ - Oj - Panko relies on Oj since it's fast and allow to serialize incrementally using `Oj::StringWriter`.
12
+ - Serialization Descriptor - Panko computes most of the metadata ahead of time, to save time later in serialization.
13
+ - Type casting Panko does type casting by itself, instead of relying on ActiveRecord.
@@ -3,13 +3,12 @@ id: performance
3
3
  title: Performance
4
4
  sidebar_label: Performance
5
5
  ---
6
-
7
6
  The performance of Panko is measured using microbenchmarks and load testing.
8
7
 
9
8
  ## Microbenchmarks
10
9
 
11
10
  The following microbenchmarks are run on MacBook Pro (16-inch, 2021, M1 Max), Ruby 3.2.0 with Rails 7.0.5
12
- demonstrating the performance of ActiveModelSerializers 0.10.13 and Panko 0.8.0
11
+ demonstrating the performance of ActiveModelSerializers 0.10.13 and Panko 0.8.0.
13
12
 
14
13
  | Benchmark | AMS ip/s | Panko ip/s |
15
14
  | ----------------- | -------- | ---------- |
@@ -20,8 +19,8 @@ demonstrating the performance of ActiveModelSerializers 0.10.13 and Panko 0.8.0
20
19
 
21
20
  ## Real-world benchmark
22
21
 
23
- The real-world benchmark here is endpoint which serializes 7,884 entries with 48 attributes and no associations.
24
- The benchmark took place in environment that simulates production environment and run using `wrk` from machine on the same cluster.
22
+ The real-world benchmark here is an endpoint which serializes 7,884 entries with 48 attributes and no associations.
23
+ The benchmark took place in an environment that simulates production environment and run using `wrk` from machine on the same cluster.
25
24
 
26
25
  | Metric | AMS | Panko |
27
26
  | ------------------ | ----- | ----- |
@@ -30,4 +29,4 @@ The benchmark took place in environment that simulates production environment an
30
29
  | 99th Response Time | 5.42s | 1.74s |
31
30
  | Total Requests | 61 | 202 |
32
31
 
33
- _Thanks to [Bringg](https://www.bringg.com) for providing the infrastructure for the benchmarks_
32
+ _Thanks to [Bringg](https://www.bringg.com) for providing the infrastructure for the benchmarks._
@@ -3,11 +3,11 @@ id: response-bag
3
3
  title: Response
4
4
  sidebar_label: Response
5
5
  ---
6
-
7
- Let's say you have some JSON payload which can is constructed using Panko serialization result,
6
+ Let's say you have some JSON payload which is constructed using Panko serialization result,
8
7
  like this:
9
8
 
10
9
  ```ruby
10
+
11
11
  class PostsController < ApplicationController
12
12
  def index
13
13
  posts = Post.all
@@ -18,11 +18,13 @@ class PostsController < ApplicationController
18
18
  }
19
19
  end
20
20
  end
21
+
21
22
  ```
22
23
 
23
- The output of the above will be json string (for `posts`) inside json string and this were `Panko::Response` shines.
24
+ The output of the above will be a JSON string (for `posts`) inside a JSON string and this were `Panko::Response` shines.
24
25
 
25
26
  ```ruby
27
+
26
28
  class PostsController < ApplicationController
27
29
  def index
28
30
  posts = Post.all
@@ -33,13 +35,15 @@ class PostsController < ApplicationController
33
35
  )
34
36
  end
35
37
  end
38
+
36
39
  ```
37
40
 
38
41
  And everything will work as expected!
39
42
 
40
- For a single object serialization, we need to use a different API (since `Panko::Serializer` don't accept an object in it's constructor):
43
+ For a single object serialization, we need to use a different API (since `Panko::Serializer` doesn't accept an object in it's constructor):
41
44
 
42
45
  ```ruby
46
+
43
47
  class PostsController < ApplicationController
44
48
  def show
45
49
  post = Post.find(params[:id])
@@ -54,14 +58,16 @@ class PostsController < ApplicationController
54
58
  )
55
59
  end
56
60
  end
61
+
57
62
  ```
58
63
 
59
64
  ## JsonValue
60
65
 
61
- Let's take the above example further, we serialized the posts and cached it as JSON string in our Cache.
62
- Now, you can wrap the cached value with `Panko::JsonValue`, like here -
66
+ Let's take the above example further, we will serialize the posts and cache it as JSON string in our Cache.
67
+ Now, you can wrap the cached value with `Panko::JsonValue`, like here:
63
68
 
64
69
  ```ruby
70
+
65
71
  class PostsController < ApplicationController
66
72
  def index
67
73
  posts = Cache.get("/posts")
@@ -73,4 +79,5 @@ class PostsController < ApplicationController
73
79
  )
74
80
  end
75
81
  end
82
+
76
83
  ```