cached_serializer 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf526cbee1a252e2821fab724ba9a1edd00aa3c85beeb52846b9cc25dc2648de
4
- data.tar.gz: 263aad0b673d71ff631f6d0cbe0ae27db278009df9ae8b3835943847d523bd9a
3
+ metadata.gz: f63d5286fa23eef6901c0699183d6afc16f3d7133ccf9cc510aa663a6e20d2af
4
+ data.tar.gz: 6617de397b071edd94246cf968872f3a633cad40f07f986bc980f5ce4a778ff6
5
5
  SHA512:
6
- metadata.gz: '059c0ebf3794f4de80a3a3b6c66a54617e96350002951730fd867f7668ac4a27f17e0a91368c8e34beb80a0afdddf1bda71f3d698bc1a7785e27e9bfb0c3f6b3'
7
- data.tar.gz: 38f0b76d94df50ce19f56a326c34fe3ad757ac0f80d1f39ebb68bd881622ead577690cc6da32fea21e8f2473551c5ce1924653ce3360adcfb27820bfb6afe9ec
6
+ metadata.gz: 60ede69361a0f192248bf914b6e2f24b9145674bc5c4c328143dc7131386520a3118c6a4c8848ab7ca4624956eedded633f26a703a5a83617fac7793ea9ba9f5
7
+ data.tar.gz: 866f839d237847c1ca0feaff3d3cc51fe27d7756cc284fc5a673d0c0fe2928aa90005e036debc5a2a5310747723d6b1b5707f8de826da0a063d4934a627b6f05
data/README.md CHANGED
@@ -12,7 +12,21 @@ every attribute. This can be desirable when some of the serialized data involves
12
12
  long-running queries, relationship-heavy calculations, etc.
13
13
 
14
14
  ```rb
15
- class UserSerializer < CachedSerializer::Base
15
+ class Admin::UserSerializer < CachedSerializer::Base
16
+ # Use the `::subject_class` macro (optional) to specify the class of the model
17
+ # this serializer will be operating on. By default, it will serialize (and
18
+ # cache for) the model class named (and nested) the same as the serializer,
19
+ # short "`Serializer`" at the end of the name. For example, `UserSerializer`
20
+ # will serialize `User` models by default. `Admin::UserSerializer` will
21
+ # serialize `Admin::User` models by default.
22
+ #
23
+ # If you want to name the serializer something other than "`TheModelName` +
24
+ # `Serializer`" (e.g., `AuthorSerializer` to serialize `User` records), or to
25
+ # put it in a module (e.g., `Admin::UserSerializer` to differentiate it from
26
+ # an existing `UserSerializer`) use `::subject_class` to specify the class of
27
+ # the subject you will be serializing.
28
+ subject_class User
29
+
16
30
  # Properties specified by `::columns` are called as-is on the model, and
17
31
  # invalidated automatically when the model is saved with new values for these
18
32
  # properties, by checking Rails' built-in `#email_changed?`/`#phone_changed?`
@@ -3,10 +3,48 @@ require_relative "./attr_serializer"
3
3
  require_relative "./attr_serializer_collection"
4
4
 
5
5
  module CachedSerializer
6
+ class Error < StandardError; end
7
+
6
8
  class Base
7
9
  attr_accessor :subject
8
10
 
9
11
  class << self
12
+ # Examples:
13
+ #
14
+ # module Admin
15
+ # class UserSerializer < CachedSerializer::Base
16
+ # subject_class User
17
+ #
18
+ # # ...
19
+ # end
20
+ # end
21
+ #
22
+ # class AuthorSerializer < CachedSerializer::Base
23
+ # subject_class User
24
+ #
25
+ # # ...
26
+ # end
27
+ #
28
+ # If you want to serialize a model and name the serializer something other
29
+ # than "`ModelName` + `Serializer`" (e.g., `AuthorSerializer` to serialize
30
+ # `User` records), or to put it in a module (e.g., `Admin::UserSerializer`
31
+ # to differentiate it from an existing `UserSerializer`) use
32
+ # `::subject_class` to specify the class of the model you will be
33
+ # serializing.
34
+ def subject_class(subject_class = nil)
35
+ return @subject_class if @subject_class && !subject_class
36
+
37
+ subject_class_name = case subject_class.class.to_s
38
+ when 'Class' then subject_class.to_s
39
+ when 'String' then subject_class.classify
40
+ else self.to_s.gsub(/[Ss]erializer\z/, '')
41
+ end
42
+
43
+ @subject_class = subject_class_name.constantize
44
+ rescue NameError
45
+ raise CachedSerializer::Error, "Cannot find a #{subject_class_name} model class for serialization (use the `subject_class TheModelName` in #{self.to_s} to specify which model to serialize)"
46
+ end
47
+
10
48
  def serializers
11
49
  @serializers ||= AttrSerializerCollection.new
12
50
  end
@@ -118,10 +156,6 @@ module CachedSerializer
118
156
 
119
157
  private
120
158
 
121
- def subject_class
122
- @subject_class ||= self.to_s.gsub(/[Ss]erializer\z/, '').constantize
123
- end
124
-
125
159
  def add_column_changed_cache_invalidator_callback(attr_name, dependent_attr_name)
126
160
  @already_added_callback ||= {}
127
161
  @already_added_callback[attr_name.to_sym] ||= {}
@@ -140,6 +174,10 @@ module CachedSerializer
140
174
  end
141
175
 
142
176
  def initialize(subject)
177
+ unless subject.is_a?(self.class.subject_class)
178
+ raise CachedSerializer::Error, "Subject is not a #{self.class.subject_class} (use `subject_class #{subject.class}` in your serializer to serialize #{subject.class} records)"
179
+ end
180
+
143
181
  self.subject = subject
144
182
  end
145
183
 
@@ -1,3 +1,3 @@
1
1
  module CachedSerializer
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cached_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keegan Leitz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-18 00:00:00.000000000 Z
11
+ date: 2019-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails