plucker_serializer 0.6.0 → 0.7.1
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 +51 -48
- data/lib/plucker/collection.rb +4 -6
- data/lib/plucker/concerns/caching.rb +10 -8
- data/lib/plucker/descriptor.rb +19 -17
- data/lib/plucker/relationship.rb +23 -28
- 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: 965b22f7307d9bb8930fe6e49e44eed985ad2dabd98970e12e79349855f7d99d
|
4
|
+
data.tar.gz: 1b21385382669c1eb7a6ebb15802a73edaceb8d8f81f9ed0caee8c9f9ade990f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce2ee569cfc9b2fd4157715efa36c823001f0a4aad122270e41d4752048d8e74867869e3dfb51ca8af08ff6d217a6b692855af7dc657cdccdef689cfb331cad2
|
7
|
+
data.tar.gz: 8cd5c5d6228800fc97429853b3b658ffe12222dca1567b3fea357b475948a4ce23c31cb7c91537b81514fe455c8dccc9b5764540c947d9f0737b92e4317e9687
|
data/lib/plucker/attribute.rb
CHANGED
data/lib/plucker/base.rb
CHANGED
@@ -14,98 +14,101 @@ module Plucker
|
|
14
14
|
|
15
15
|
attr_accessor :object
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
self._descriptor ||= Plucker::Descriptor.new(self.class)
|
17
|
+
class << self
|
18
|
+
attr_accessor :_descriptor
|
20
19
|
end
|
21
20
|
|
21
|
+
self._descriptor ||= Descriptor.new(self)
|
22
|
+
|
22
23
|
def self.inherited(base)
|
23
24
|
super
|
24
25
|
base._descriptor = Plucker::Descriptor.new(base)
|
25
26
|
end
|
26
27
|
|
27
28
|
def initialize(object, options = {})
|
28
|
-
|
29
|
+
@object = object
|
29
30
|
end
|
30
31
|
|
31
32
|
def serializable_hash(use_cache: true)
|
32
33
|
if use_cache && self.class.cache_enabled?
|
33
|
-
fetch(adapter: :hash)
|
34
|
-
get_hash
|
35
|
-
end
|
34
|
+
fetch(adapter: :hash) { compute_hash }
|
36
35
|
else
|
37
|
-
|
36
|
+
compute_hash
|
38
37
|
end
|
39
38
|
end
|
40
39
|
alias to_hash serializable_hash
|
41
40
|
alias to_h serializable_hash
|
42
41
|
|
43
|
-
def as_json(
|
42
|
+
def as_json(_options = nil)
|
44
43
|
serializable_hash
|
45
44
|
end
|
46
45
|
|
47
46
|
def to_json(options = {}, use_cache: true)
|
48
47
|
if use_cache && self.class.cache_enabled?
|
49
|
-
fetch(adapter: :json)
|
50
|
-
Oj.dump(get_hash, mode: :rails)
|
51
|
-
end
|
48
|
+
fetch(adapter: :json) { Oj.dump(compute_hash, mode: :rails) }
|
52
49
|
else
|
53
|
-
Oj.dump(
|
50
|
+
Oj.dump(compute_hash, mode: :rails)
|
54
51
|
end
|
55
52
|
end
|
56
53
|
|
57
|
-
|
58
|
-
attributes_hash.merge! associations_hash
|
59
|
-
end
|
60
|
-
|
61
|
-
def associations_hash
|
62
|
-
self.class._descriptor._relationships.each_with_object({}) do |(key, relationship), hash|
|
63
|
-
next unless relationship.should_include?(self)
|
54
|
+
private
|
64
55
|
|
65
|
-
|
66
|
-
|
56
|
+
def compute_hash
|
57
|
+
@compute_hash ||= attributes_hash.merge(associations_hash)
|
67
58
|
end
|
68
59
|
|
69
60
|
def attributes_hash
|
70
61
|
self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
|
71
|
-
|
72
|
-
|
73
|
-
hash[key.to_s] = attr.value(self)
|
62
|
+
hash[key] = attr.value(self) if attr.should_include?(self)
|
74
63
|
end
|
75
64
|
end
|
76
65
|
|
77
|
-
def
|
78
|
-
self._descriptor.
|
66
|
+
def associations_hash
|
67
|
+
self.class._descriptor._relationships.each_with_object({}) do |(key, relationship), hash|
|
68
|
+
hash[key] = relationship.value(self) if relationship.should_include?(self)
|
69
|
+
end
|
79
70
|
end
|
80
71
|
|
81
|
-
|
82
|
-
|
83
|
-
|
72
|
+
class << self
|
73
|
+
def pluckable?
|
74
|
+
_descriptor.pluckable?
|
75
|
+
end
|
84
76
|
|
85
|
-
|
86
|
-
|
87
|
-
attribute(attr)
|
77
|
+
def pluckable_columns
|
78
|
+
_descriptor._pluckable_columns
|
88
79
|
end
|
89
|
-
end
|
90
80
|
|
91
|
-
|
92
|
-
|
93
|
-
|
81
|
+
def attributes(*attrs)
|
82
|
+
attrs.each { |attr| attribute(attr) }
|
83
|
+
end
|
94
84
|
|
95
|
-
|
96
|
-
|
97
|
-
|
85
|
+
def attribute(attr, options = {}, &block)
|
86
|
+
key = options.fetch(:key, attr)
|
87
|
+
attribute = Plucker::Attribute.new(attr, options, block)
|
88
|
+
_descriptor.add_attribute(key.to_sym, attribute)
|
89
|
+
end
|
98
90
|
|
99
|
-
|
100
|
-
|
101
|
-
|
91
|
+
def belongs_to(attr, options = {}, &block)
|
92
|
+
key = options.fetch(:key, attr)
|
93
|
+
relationship = BelongsTo.new(attr, options, block)
|
94
|
+
_descriptor.add_relationship(key.to_sym, relationship)
|
95
|
+
end
|
102
96
|
|
103
|
-
|
104
|
-
|
105
|
-
|
97
|
+
def has_one(attr, options = {}, &block)
|
98
|
+
key = options.fetch(:key, attr)
|
99
|
+
relationship = HasOne.new(attr, options, block)
|
100
|
+
_descriptor.add_relationship(key.to_sym, relationship)
|
101
|
+
end
|
106
102
|
|
107
|
-
|
108
|
-
|
103
|
+
def has_many(attr, options = {}, &block)
|
104
|
+
key = options.fetch(:key, attr)
|
105
|
+
relationship = HasMany.new(attr, options, block)
|
106
|
+
_descriptor.add_relationship(key.to_sym, relationship)
|
107
|
+
end
|
108
|
+
|
109
|
+
def model(attr)
|
110
|
+
_descriptor.set_model(attr)
|
111
|
+
end
|
109
112
|
end
|
110
113
|
end
|
111
114
|
end
|
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,41 +2,43 @@
|
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
@_serialized_model = get_serialized_model(serializer_class)
|
9
|
+
@_attributes = {}
|
10
|
+
@_relationships = {}
|
11
|
+
@_pluckable_columns = Set.new
|
12
|
+
@_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
|
-
_attributes[key] = attr
|
21
|
-
|
22
|
-
|
20
|
+
@_attributes[key] = attr
|
21
|
+
|
22
|
+
if attr.pluckable? && @_serialized_model&.column_names&.include?(attr.name.to_s)
|
23
|
+
@_pluckable_columns << attr.name
|
23
24
|
else
|
24
|
-
|
25
|
+
@_pluckable = false
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
29
|
def add_relationship(key, relationship)
|
29
|
-
_relationships[key] = relationship
|
30
|
-
|
30
|
+
@_relationships[key] = relationship
|
31
|
+
@_pluckable = false
|
31
32
|
end
|
32
33
|
|
33
34
|
def set_model(model)
|
34
|
-
|
35
|
+
@_serialized_model = model
|
35
36
|
end
|
36
37
|
|
37
38
|
def get_serialized_model(serializer_class)
|
38
|
-
serializer_class.name.split(/Serializer/).first.
|
39
|
-
|
39
|
+
model_name = serializer_class.name.split(/Serializer/).first.freeze
|
40
|
+
model_name.constantize
|
41
|
+
rescue NameError, LoadError
|
40
42
|
nil
|
41
43
|
end
|
42
44
|
end
|
data/lib/plucker/relationship.rb
CHANGED
@@ -14,50 +14,45 @@ module Plucker
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def should_include?(serializer)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
when Symbol
|
21
|
-
|
22
|
-
when String
|
23
|
-
serializer.instance_eval(@condition)
|
17
|
+
return true unless condition
|
18
|
+
|
19
|
+
case condition
|
20
|
+
when Symbol then serializer.public_send(condition)
|
21
|
+
when String then serializer.instance_eval(condition)
|
24
22
|
when Proc
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
serializer.instance_exec(serializer, &@condition)
|
29
|
-
end
|
23
|
+
condition.arity.zero? ? serializer.instance_exec(&condition) : serializer.instance_exec(serializer, &condition)
|
24
|
+
else
|
25
|
+
true
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
33
29
|
def associated_object(serializer)
|
34
|
-
if
|
35
|
-
result =
|
36
|
-
|
37
|
-
else
|
38
|
-
@block.call(serializer.object)
|
39
|
-
end
|
40
|
-
return result if result != :nil
|
30
|
+
if block
|
31
|
+
result = block.arity.zero? ? serializer.object.instance_exec(&block) : block.call(serializer.object)
|
32
|
+
return result unless result == :nil
|
41
33
|
end
|
42
|
-
|
34
|
+
|
35
|
+
serializer.object.public_send(name)
|
43
36
|
end
|
44
37
|
|
45
|
-
|
38
|
+
# This method is intended to be overridden by subclasses (such as HasOne, HasMany, etc.)
|
39
|
+
def value(_serializer)
|
46
40
|
nil
|
47
41
|
end
|
48
42
|
|
49
43
|
private
|
50
44
|
|
51
45
|
def relationship_serializer(serializer, relationship_object = nil)
|
52
|
-
if
|
53
|
-
association_class =
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
46
|
+
if options[:serializer].blank?
|
47
|
+
association_class =
|
48
|
+
if relationship_object.present?
|
49
|
+
relationship_object.class.name
|
50
|
+
else
|
51
|
+
serializer.object.class.reflect_on_association(name)&.class_name
|
52
|
+
end
|
58
53
|
"#{association_class.demodulize.camelize}Serializer".constantize
|
59
54
|
else
|
60
|
-
|
55
|
+
options[:serializer]
|
61
56
|
end
|
62
57
|
end
|
63
58
|
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.1
|
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
|