plucker_serializer 0.6.0 → 0.7.0
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/lib/plucker/attribute.rb +1 -1
- data/lib/plucker/base.rb +16 -8
- data/lib/plucker/collection.rb +4 -6
- data/lib/plucker/concerns/caching.rb +10 -8
- data/lib/plucker/descriptor.rb +8 -8
- data/lib/plucker/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: 5944e6c05d1bab433f405984a62980c72501367bcddae6c91164aaf412eab4fc
|
4
|
+
data.tar.gz: c3d20b80b2d9759239d1b35b669a6a1ea09bad571c952b9a3c768330937af866
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d402fd1eebbd1b62394292f11b57a04e02e4d90dd2402272b239da98fe504900d9164dba7dc94c4fc13c8bc5b4db7350b1841021977ebe69c77d187c06b9fc0d
|
7
|
+
data.tar.gz: fdd3db583cb2d0cee54ae0a96b137657d977a21477d8ea13550ca388162f0a5636108690166f34ce88c10ea5764730985858d9b71f9173422ededaaeb289e359
|
data/lib/plucker/attribute.rb
CHANGED
data/lib/plucker/base.rb
CHANGED
@@ -62,7 +62,7 @@ module Plucker
|
|
62
62
|
self.class._descriptor._relationships.each_with_object({}) do |(key, relationship), hash|
|
63
63
|
next unless relationship.should_include?(self)
|
64
64
|
|
65
|
-
hash[key.
|
65
|
+
hash[key.to_sym] = relationship.value(self)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -70,12 +70,12 @@ module Plucker
|
|
70
70
|
self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
|
71
71
|
next unless attr.should_include?(self)
|
72
72
|
|
73
|
-
hash[key.
|
73
|
+
hash[key.to_sym] = attr.value(self)
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
|
-
def self.
|
78
|
-
self._descriptor.
|
77
|
+
def self.pluckable?
|
78
|
+
self._descriptor.pluckable?
|
79
79
|
end
|
80
80
|
|
81
81
|
def self.pluckable_columns
|
@@ -89,19 +89,27 @@ module Plucker
|
|
89
89
|
end
|
90
90
|
|
91
91
|
def self.attribute(attr, options = {}, &block)
|
92
|
-
|
92
|
+
key = options.fetch(:key, attr)
|
93
|
+
attribute = Plucker::Attribute.new(attr, options, block)
|
94
|
+
self._descriptor.add_attribute(key.to_sym, attribute)
|
93
95
|
end
|
94
96
|
|
95
97
|
def self.belongs_to(attr, options = {}, &block)
|
96
|
-
|
98
|
+
key = options.fetch(:key, attr)
|
99
|
+
belongs_to = Plucker::BelongsTo.new(attr, options, block)
|
100
|
+
self._descriptor.add_relationship(key.to_sym, belongs_to)
|
97
101
|
end
|
98
102
|
|
99
103
|
def self.has_one(attr, options = {}, &block)
|
100
|
-
|
104
|
+
key = options.fetch(:key, attr)
|
105
|
+
has_one = Plucker::HasOne.new(attr, options, block)
|
106
|
+
self._descriptor.add_relationship(key.to_sym, has_one)
|
101
107
|
end
|
102
108
|
|
103
109
|
def self.has_many(attr, options = {}, &block)
|
104
|
-
|
110
|
+
key = options.fetch(:key, attr)
|
111
|
+
has_many = Plucker::HasMany.new(attr, options, block)
|
112
|
+
self._descriptor.add_relationship(key.to_sym, has_many)
|
105
113
|
end
|
106
114
|
|
107
115
|
def self.model(attr)
|
data/lib/plucker/collection.rb
CHANGED
@@ -57,7 +57,7 @@ module Plucker
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def get_collection_json(use_cache: false)
|
60
|
-
if serializer_class.
|
60
|
+
if serializer_class.pluckable?
|
61
61
|
associated_hash
|
62
62
|
else
|
63
63
|
objects.map do |object|
|
@@ -67,8 +67,8 @@ module Plucker
|
|
67
67
|
end
|
68
68
|
|
69
69
|
def get_hash(use_cache: false)
|
70
|
-
if serializer_class.
|
71
|
-
associated_hash
|
70
|
+
if serializer_class.pluckable?
|
71
|
+
associated_hash.map(&:symbolize_keys)
|
72
72
|
else
|
73
73
|
objects.map do |object|
|
74
74
|
serializer_class.new(object).serializable_hash(use_cache: use_cache)
|
@@ -77,9 +77,7 @@ module Plucker
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def cache_version
|
80
|
-
|
81
|
-
|
82
|
-
@cache_version = objects.cache_version
|
80
|
+
@cache_version ||= objects.cache_version
|
83
81
|
end
|
84
82
|
|
85
83
|
def cache_key(adapter: :json)
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'active_support/all'
|
3
4
|
|
4
5
|
module Plucker
|
@@ -16,7 +17,8 @@ module Plucker
|
|
16
17
|
module ClassMethods
|
17
18
|
def _cache_digest
|
18
19
|
return @_cache_digest if defined?(@_cache_digest)
|
19
|
-
|
20
|
+
|
21
|
+
@_cache_digest = Digest::SHA1.hexdigest(name)
|
20
22
|
end
|
21
23
|
|
22
24
|
def cache(options = {})
|
@@ -26,15 +28,14 @@ module Plucker
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def cache_enabled?
|
29
|
-
|
31
|
+
_cache_store.present? && _cache.present?
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
|
-
def fetch(adapter: :json)
|
35
|
+
def fetch(adapter: :json, &block)
|
34
36
|
if serializer_class.cache_enabled?
|
35
|
-
serializer_class._cache_store.fetch(cache_key(adapter: adapter), version: cache_version,
|
36
|
-
|
37
|
-
end
|
37
|
+
serializer_class._cache_store.fetch(cache_key(adapter: adapter), version: cache_version,
|
38
|
+
options: serializer_class._cache_options, &block)
|
38
39
|
else
|
39
40
|
yield
|
40
41
|
end
|
@@ -42,15 +43,16 @@ module Plucker
|
|
42
43
|
|
43
44
|
def cache_version
|
44
45
|
return @cache_version if defined?(@cache_version)
|
46
|
+
|
45
47
|
@cache_version = object.cache_version
|
46
48
|
end
|
47
49
|
|
48
50
|
def cache_key(adapter: :json)
|
49
|
-
object.cache_key
|
51
|
+
"#{object.cache_key}/#{serializer_class._cache_digest}/#{adapter}"
|
50
52
|
end
|
51
53
|
|
52
54
|
def serializer_class
|
53
55
|
@serializer_class ||= self.class
|
54
56
|
end
|
55
57
|
end
|
56
|
-
end
|
58
|
+
end
|
data/lib/plucker/descriptor.rb
CHANGED
@@ -2,32 +2,32 @@
|
|
2
2
|
|
3
3
|
module Plucker
|
4
4
|
class Descriptor
|
5
|
-
attr_accessor :_serialized_model, :_attributes, :_relationships, :_pluckable_columns, :
|
5
|
+
attr_accessor :_serialized_model, :_attributes, :_relationships, :_pluckable_columns, :_pluckable
|
6
6
|
|
7
7
|
def initialize(serializer_class)
|
8
8
|
self._serialized_model = get_serialized_model(serializer_class)
|
9
9
|
self._attributes = {}
|
10
10
|
self._relationships = {}
|
11
11
|
self._pluckable_columns = Set.new
|
12
|
-
self.
|
12
|
+
self._pluckable = true
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
|
15
|
+
def pluckable?
|
16
|
+
_pluckable
|
17
17
|
end
|
18
18
|
|
19
19
|
def add_attribute(key, attr)
|
20
20
|
_attributes[key] = attr
|
21
|
-
if attr.
|
21
|
+
if attr.pluckable? && _serialized_model&.column_names&.include?(attr.name.to_s)
|
22
22
|
_pluckable_columns << attr.name
|
23
23
|
else
|
24
|
-
self.
|
24
|
+
self._pluckable = false
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
def add_relationship(key, relationship)
|
29
29
|
_relationships[key] = relationship
|
30
|
-
self.
|
30
|
+
self._pluckable = false
|
31
31
|
end
|
32
32
|
|
33
33
|
def set_model(model)
|
@@ -36,7 +36,7 @@ module Plucker
|
|
36
36
|
|
37
37
|
def get_serialized_model(serializer_class)
|
38
38
|
serializer_class.name.split(/Serializer/).first.constantize
|
39
|
-
rescue NameError, LoadError
|
39
|
+
rescue NameError, LoadError
|
40
40
|
nil
|
41
41
|
end
|
42
42
|
end
|
data/lib/plucker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plucker_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henry Boisgibault
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|