serializr 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d0515d6eae5a0ac66b6a3b99d8c13dc4c678c57
4
- data.tar.gz: 41ddc35c1451b748395a7e030a9b506d4b8b6d1b
3
+ metadata.gz: 401cb01b0f27931f2a1a2c28c938e4e3990ec529
4
+ data.tar.gz: 9849952b0cb668f9e5fcbf9d0c1b3fd652bdf65a
5
5
  SHA512:
6
- metadata.gz: 4ae557c0706a93fe3b8b99a5eb94170d5d57e6b308742d9289b8890aa635995986374ddffbda2fb11d164912099ef5d5fcc53f96ec2045ed5f2dc3c40eed5145
7
- data.tar.gz: c1a16cb9d3b53a5954d3e77f0c5c01760d5b532ccce1e9e73dee2998d1ba401eb438d6d7cfc65b2b08dcf3c202b35a6ac7e5d77f5e0a95264b61513299613da0
6
+ metadata.gz: 3998530017184a7906c166f3dc786c9191c8402edd587e15f279f6317732a30533fbac00d1f5d85a7552f4459fa5643e43d53c9a4a4cb05b195742b6ae2a25b2
7
+ data.tar.gz: fb27cf00e7093a2cf73d5683e220f775d7a34f567c84ee02caa8021460a9604ee1936111169ce555d5cedfab4d001519fb7918e7f0e2ce91494d5722c41ef11d
data/README.md CHANGED
@@ -93,7 +93,7 @@ You can also render collections of objects:
93
93
  ```ruby
94
94
  class FriendsController < ApplicationController
95
95
  def index
96
- user = User.friends_of(params[:id])
96
+ friends = User.friends_of(params[:id])
97
97
 
98
98
  render json: friends
99
99
  end
@@ -108,7 +108,7 @@ cool, you know. 😎
108
108
  ```ruby
109
109
  class FriendsController < ApplicationController
110
110
  def index
111
- user = User.friends_of(params[:id])
111
+ friends = User.friends_of(params[:id])
112
112
 
113
113
  render json: friends, serializer: UserSerializer[]
114
114
  end
@@ -120,7 +120,7 @@ And this is how you drop `Action View` off your API's, kids!
120
120
  (_Not really, but anyway, I have the stage now, I do the typing. Fuck off and
121
121
  call my therapist!_)
122
122
 
123
- ### 1️⃣ Last Thing
123
+ ### 👆 Last Thing
124
124
 
125
125
  To fill the API cliché, we need to go over one last file:
126
126
  `app/serializers/application_serializer.rb`. At first, it looks like this:
@@ -144,17 +144,45 @@ class ApplicationSerializer < Serializr
144
144
  # JSON responses.
145
145
  inlcude Rails.application.routes.url_helpers
146
146
 
147
- EPOCH_TIME = Time.at(0).in_time_zone('UTC')
147
+ cattr_reader :serializer_class_cache do
148
+ Hash.new do |hash, cls|
149
+ hash[cls] = "#{cls}Serializer".constantize
150
+ end
151
+ end
152
+
153
+ # Because I'm sure you gonna ask: how do I render associations. Where are the
154
+ # `has_one` and `has_many` class macros?
155
+ #
156
+ # The answers is: you don't need those macros. You can use similar methods
157
+ # like the ones below to render the associations with plain old boring Ruby
158
+ # methods.
159
+ #
160
+ # class UserSerializer < ApplicationSerializer
161
+ # attributes :card
162
+ #
163
+ # def card
164
+ # render_one(object.credit_card)
165
+ # end
166
+ # end
167
+ #
168
+ # No extra DSL, but still: clean, consise and flexible view code.
169
+ def render_one(object, serializer: nil)
170
+ return unless object
171
+
172
+ serializer ||= serializer_class_cache[object.class]
173
+ serializer.new(object)
174
+ end
175
+
176
+ def render_many(objects, serializer: nil)
177
+ return [] if objects.blank?
148
178
 
149
- # You may wanna render this timestamp instead of `null` for unset timestamps.
150
- # Or do whatever, really. I'm not your parents.
151
- def render_timestamp(timestamp)
152
- timestamp || EPOCH_TIME
179
+ serializer ||= serializer_class_cache[objects.first.class][]
180
+ serializer.new(objects)
153
181
  end
154
182
  end
155
183
  ```
156
184
 
157
- ### 1️⃣ Last Thing, For Real This Time
185
+ ### ✌️ Last Thing
158
186
 
159
187
  Serializr? Really? I know. It's fine.
160
188
 
@@ -28,7 +28,7 @@ class SerializrGenerator < Rails::Generators::NamedBase
28
28
  end
29
29
 
30
30
  def generate_application_serilzer
31
- if self.behavior == :invoke && !application_serilzer_exist?
31
+ if behavior == :invoke && !application_serilzer_exist?
32
32
  template 'application_serializr.rb', application_serilzer_file_name
33
33
  end
34
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serializr
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/serializr.rb CHANGED
@@ -31,8 +31,12 @@ class Serializr
31
31
  @@collection_class_cache ||= Hash.new do |hash, cls|
32
32
  hash[cls] = Class.new(cls) do
33
33
  def as_json
34
- serializer = self.class.superclass
35
- object.map { |obj| serializer.new(obj, options).as_json }
34
+ serializer = self.class.superclass.new(nil, options)
35
+
36
+ object.map do |obj|
37
+ serializer.object = obj
38
+ serializer.as_json
39
+ end
36
40
  end
37
41
  end
38
42
  end
@@ -56,9 +60,9 @@ class Serializr
56
60
  end]
57
61
  end
58
62
 
59
- private
63
+ protected
60
64
 
61
- attr_reader :object, :options
65
+ attr_accessor :object, :options
62
66
  end
63
67
 
64
68
  # Let's get the constant, even if we don't explicitly `require 'serializer'`.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serializr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genadi Samokovarov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-01 00:00:00.000000000 Z
11
+ date: 2016-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler