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 +4 -4
- data/README.md +15 -1
- data/lib/cached_serializer/base.rb +42 -4
- data/lib/cached_serializer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f63d5286fa23eef6901c0699183d6afc16f3d7133ccf9cc510aa663a6e20d2af
|
4
|
+
data.tar.gz: 6617de397b071edd94246cf968872f3a633cad40f07f986bc980f5ce4a778ff6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
|
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.
|
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-
|
11
|
+
date: 2019-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|