plucker_serializer 0.5.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 +44 -41
- data/lib/plucker/base.rb +89 -78
- data/lib/plucker/belongs_to.rb +10 -8
- data/lib/plucker/collection.rb +20 -21
- data/lib/plucker/concerns/caching.rb +10 -8
- data/lib/plucker/configuration.rb +7 -7
- data/lib/plucker/descriptor.rb +34 -35
- data/lib/plucker/has_many.rb +9 -7
- data/lib/plucker/has_one.rb +10 -8
- data/lib/plucker/relationship.rb +54 -51
- data/lib/plucker/version.rb +3 -2
- data/lib/plucker_serializer.rb +20 -20
- metadata +3 -3
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
@@ -1,48 +1,51 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Plucker
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def initialize(name, options = {}, block)
|
7
|
-
@name = name
|
8
|
-
@block = block
|
9
|
-
@condition = options[:if]
|
10
|
-
end
|
4
|
+
class Attribute
|
5
|
+
attr_reader :name, :block, :condition
|
11
6
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
if serializer.respond_to?(@name)
|
18
|
-
serializer.send(@name)
|
19
|
-
else
|
20
|
-
serializer.object.send(@name)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
7
|
+
def initialize(name, options = {}, block)
|
8
|
+
@name = name.to_sym
|
9
|
+
@block = block&.freeze
|
10
|
+
@condition = options[:if]&.freeze
|
11
|
+
end
|
24
12
|
|
25
|
-
|
26
|
-
|
27
|
-
|
13
|
+
def value(serializer)
|
14
|
+
if @block
|
15
|
+
result = if @block.arity.zero?
|
16
|
+
serializer.object.instance_exec(&@block)
|
17
|
+
else
|
18
|
+
@block.call(serializer.object)
|
19
|
+
end
|
20
|
+
return result unless result == :nil
|
21
|
+
end
|
22
|
+
|
23
|
+
if serializer.respond_to?(@name)
|
24
|
+
serializer.public_send(@name)
|
25
|
+
else
|
26
|
+
serializer.object.public_send(@name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def pluckable?
|
31
|
+
@block.blank?
|
32
|
+
end
|
28
33
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
43
|
-
else
|
44
|
-
nil
|
45
|
-
end
|
34
|
+
def should_include?(serializer)
|
35
|
+
case @condition
|
36
|
+
when nil
|
37
|
+
true
|
38
|
+
when Symbol
|
39
|
+
serializer.public_send(@condition)
|
40
|
+
when String
|
41
|
+
serializer.instance_eval(@condition)
|
42
|
+
when Proc
|
43
|
+
if @condition.arity.zero?
|
44
|
+
serializer.instance_exec(&@condition)
|
45
|
+
else
|
46
|
+
serializer.instance_exec(serializer, &@condition)
|
46
47
|
end
|
48
|
+
end
|
47
49
|
end
|
48
|
-
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/plucker/base.rb
CHANGED
@@ -1,108 +1,119 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require_relative 'concerns/caching'
|
3
4
|
require_relative 'descriptor'
|
4
5
|
require_relative 'has_many'
|
5
6
|
require_relative 'belongs_to'
|
6
7
|
require_relative 'has_one'
|
7
|
-
require
|
8
|
+
require 'oj'
|
8
9
|
require 'active_support/all'
|
9
10
|
|
10
11
|
module Plucker
|
11
|
-
|
12
|
-
|
12
|
+
class Base
|
13
|
+
include Caching
|
13
14
|
|
14
|
-
|
15
|
+
attr_accessor :object
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
with_options instance_writer: false, instance_reader: false do |serializer|
|
18
|
+
serializer.class_attribute :_descriptor
|
19
|
+
self._descriptor ||= Plucker::Descriptor.new(self.class)
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def self.inherited(base)
|
23
|
+
super
|
24
|
+
base._descriptor = Plucker::Descriptor.new(base)
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def initialize(object, options = {})
|
28
|
+
self.object = object
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
35
|
-
else
|
36
|
-
get_hash
|
37
|
-
end
|
31
|
+
def serializable_hash(use_cache: true)
|
32
|
+
if use_cache && self.class.cache_enabled?
|
33
|
+
fetch(adapter: :hash) do
|
34
|
+
get_hash
|
38
35
|
end
|
39
|
-
|
40
|
-
|
36
|
+
else
|
37
|
+
get_hash
|
38
|
+
end
|
39
|
+
end
|
40
|
+
alias to_hash serializable_hash
|
41
|
+
alias to_h serializable_hash
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
def as_json(options = nil)
|
44
|
+
serializable_hash
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
51
|
-
else
|
52
|
-
Oj.dump(get_hash, mode: :rails)
|
53
|
-
end
|
47
|
+
def to_json(options = {}, use_cache: true)
|
48
|
+
if use_cache && self.class.cache_enabled?
|
49
|
+
fetch(adapter: :json) do
|
50
|
+
Oj.dump(get_hash, mode: :rails)
|
54
51
|
end
|
52
|
+
else
|
53
|
+
Oj.dump(get_hash, mode: :rails)
|
54
|
+
end
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
def get_hash
|
58
|
+
attributes_hash.merge! associations_hash
|
59
|
+
end
|
59
60
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
hash[key.to_s] = relationship.value(self)
|
64
|
-
end
|
65
|
-
end
|
61
|
+
def associations_hash
|
62
|
+
self.class._descriptor._relationships.each_with_object({}) do |(key, relationship), hash|
|
63
|
+
next unless relationship.should_include?(self)
|
66
64
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
hash[key.to_s] = attr.value(self)
|
71
|
-
end
|
72
|
-
end
|
65
|
+
hash[key.to_sym] = relationship.value(self)
|
66
|
+
end
|
67
|
+
end
|
73
68
|
|
74
|
-
|
75
|
-
|
76
|
-
|
69
|
+
def attributes_hash
|
70
|
+
self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
|
71
|
+
next unless attr.should_include?(self)
|
77
72
|
|
78
|
-
|
79
|
-
|
80
|
-
|
73
|
+
hash[key.to_sym] = attr.value(self)
|
74
|
+
end
|
75
|
+
end
|
81
76
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
end
|
86
|
-
end
|
77
|
+
def self.pluckable?
|
78
|
+
self._descriptor.pluckable?
|
79
|
+
end
|
87
80
|
|
88
|
-
|
89
|
-
|
90
|
-
|
81
|
+
def self.pluckable_columns
|
82
|
+
self._descriptor._pluckable_columns
|
83
|
+
end
|
91
84
|
|
92
|
-
|
93
|
-
|
94
|
-
|
85
|
+
def self.attributes(*attrs)
|
86
|
+
attrs.each do |attr|
|
87
|
+
attribute(attr)
|
88
|
+
end
|
89
|
+
end
|
95
90
|
|
96
|
-
|
97
|
-
|
98
|
-
|
91
|
+
def self.attribute(attr, options = {}, &block)
|
92
|
+
key = options.fetch(:key, attr)
|
93
|
+
attribute = Plucker::Attribute.new(attr, options, block)
|
94
|
+
self._descriptor.add_attribute(key.to_sym, attribute)
|
95
|
+
end
|
99
96
|
|
100
|
-
|
101
|
-
|
102
|
-
|
97
|
+
def self.belongs_to(attr, options = {}, &block)
|
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)
|
101
|
+
end
|
103
102
|
|
104
|
-
|
105
|
-
|
106
|
-
|
103
|
+
def self.has_one(attr, options = {}, &block)
|
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)
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.has_many(attr, options = {}, &block)
|
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)
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.model(attr)
|
116
|
+
self._descriptor.set_model(attr)
|
107
117
|
end
|
108
|
-
end
|
118
|
+
end
|
119
|
+
end
|
data/lib/plucker/belongs_to.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
|
3
|
+
require_relative 'relationship'
|
3
4
|
|
4
5
|
module Plucker
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
class BelongsTo < Plucker::Relationship
|
7
|
+
def value(serializer)
|
8
|
+
relationship_object = associated_object(serializer)
|
9
|
+
return nil if relationship_object.blank?
|
10
|
+
|
11
|
+
relationship_serializer(serializer, relationship_object).new(relationship_object).serializable_hash
|
11
12
|
end
|
12
|
-
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/plucker/collection.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require_relative 'concerns/caching'
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'oj'
|
5
|
+
require 'pluck_all'
|
5
6
|
|
6
7
|
module Plucker
|
7
8
|
class Collection
|
@@ -18,22 +19,20 @@ module Plucker
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def serializable_hash
|
21
|
-
if
|
22
|
+
if !objects.is_a?(ActiveRecord::Relation)
|
22
23
|
objects.map do |object|
|
23
24
|
serializer_class.new(object).serializable_hash
|
24
25
|
end.compact
|
25
|
-
|
26
|
-
if
|
27
|
-
|
28
|
-
|
29
|
-
get_hash(use_cache: false)
|
30
|
-
end
|
31
|
-
elsif @cache_type == :multi
|
32
|
-
get_hash(use_cache: true)
|
26
|
+
elsif serializer_class.cache_enabled?
|
27
|
+
if @cache_type == :collection
|
28
|
+
fetch(adapter: :hash) do
|
29
|
+
get_hash(use_cache: false)
|
33
30
|
end
|
34
|
-
|
35
|
-
get_hash(use_cache:
|
31
|
+
elsif @cache_type == :multi
|
32
|
+
get_hash(use_cache: true)
|
36
33
|
end
|
34
|
+
else
|
35
|
+
get_hash(use_cache: false)
|
37
36
|
end
|
38
37
|
end
|
39
38
|
alias to_hash serializable_hash
|
@@ -58,7 +57,7 @@ module Plucker
|
|
58
57
|
end
|
59
58
|
|
60
59
|
def get_collection_json(use_cache: false)
|
61
|
-
if serializer_class.
|
60
|
+
if serializer_class.pluckable?
|
62
61
|
associated_hash
|
63
62
|
else
|
64
63
|
objects.map do |object|
|
@@ -68,8 +67,8 @@ module Plucker
|
|
68
67
|
end
|
69
68
|
|
70
69
|
def get_hash(use_cache: false)
|
71
|
-
if serializer_class.
|
72
|
-
associated_hash
|
70
|
+
if serializer_class.pluckable?
|
71
|
+
associated_hash.map(&:symbolize_keys)
|
73
72
|
else
|
74
73
|
objects.map do |object|
|
75
74
|
serializer_class.new(object).serializable_hash(use_cache: use_cache)
|
@@ -78,21 +77,21 @@ module Plucker
|
|
78
77
|
end
|
79
78
|
|
80
79
|
def cache_version
|
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)
|
86
|
-
objects.cache_key
|
84
|
+
"#{objects.cache_key}/#{serializer_class._cache_digest}/#{adapter}"
|
87
85
|
end
|
88
86
|
|
89
87
|
private
|
88
|
+
|
90
89
|
def associated_hash
|
91
90
|
pluck_to_hash(objects, serializer_class.pluckable_columns.to_a)
|
92
91
|
end
|
93
92
|
|
94
93
|
def pluck_to_hash(objects, attrs)
|
95
|
-
namespaced_attrs = attrs.map { |attr| objects.model.table_name
|
94
|
+
namespaced_attrs = attrs.map { |attr| "#{objects.model.table_name}.#{attr}" }
|
96
95
|
objects.pluck_all(namespaced_attrs.join(','))
|
97
96
|
end
|
98
97
|
|
@@ -104,4 +103,4 @@ module Plucker
|
|
104
103
|
end
|
105
104
|
end
|
106
105
|
end
|
107
|
-
end
|
106
|
+
end
|
@@ -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
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Plucker
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :cache_store
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@cache_store = nil
|
10
9
|
end
|
11
|
-
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/plucker/descriptor.rb
CHANGED
@@ -1,44 +1,43 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Plucker
|
3
|
-
|
4
|
-
|
4
|
+
class Descriptor
|
5
|
+
attr_accessor :_serialized_model, :_attributes, :_relationships, :_pluckable_columns, :_pluckable
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
def initialize(serializer_class)
|
8
|
+
self._serialized_model = get_serialized_model(serializer_class)
|
9
|
+
self._attributes = {}
|
10
|
+
self._relationships = {}
|
11
|
+
self._pluckable_columns = Set.new
|
12
|
+
self._pluckable = true
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
def pluckable?
|
16
|
+
_pluckable
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
def add_attribute(key, attr)
|
20
|
+
_attributes[key] = attr
|
21
|
+
if attr.pluckable? && _serialized_model&.column_names&.include?(attr.name.to_s)
|
22
|
+
_pluckable_columns << attr.name
|
23
|
+
else
|
24
|
+
self._pluckable = false
|
25
|
+
end
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
def add_relationship(key, relationship)
|
29
|
+
_relationships[key] = relationship
|
30
|
+
self._pluckable = false
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
def set_model(model)
|
34
|
+
self._serialized_model = model
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
nil
|
41
|
-
end
|
42
|
-
end
|
37
|
+
def get_serialized_model(serializer_class)
|
38
|
+
serializer_class.name.split(/Serializer/).first.constantize
|
39
|
+
rescue NameError, LoadError
|
40
|
+
nil
|
43
41
|
end
|
44
|
-
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/plucker/has_many.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative
|
2
|
+
|
3
|
+
require_relative 'relationship'
|
4
|
+
require_relative 'collection'
|
4
5
|
|
5
6
|
module Plucker
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
class HasMany < Plucker::Relationship
|
8
|
+
def value(serializer)
|
9
|
+
Plucker::Collection.new(associated_object(serializer),
|
10
|
+
serializer: relationship_serializer(serializer)).serializable_hash
|
10
11
|
end
|
11
|
-
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/plucker/has_one.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
|
3
|
+
require_relative 'relationship'
|
3
4
|
|
4
5
|
module Plucker
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
class HasOne < Plucker::Relationship
|
7
|
+
def value(serializer)
|
8
|
+
relationship_object = associated_object(serializer)
|
9
|
+
return nil if relationship_object.blank?
|
10
|
+
|
11
|
+
relationship_serializer(serializer, relationship_object).new(relationship_object).serializable_hash
|
11
12
|
end
|
12
|
-
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/plucker/relationship.rb
CHANGED
@@ -1,61 +1,64 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require_relative 'collection'
|
3
4
|
|
4
5
|
module Plucker
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def initialize(name, options = {}, block)
|
9
|
-
@name = name
|
10
|
-
@block = block
|
11
|
-
@options = options
|
12
|
-
@condition = options[:if]
|
13
|
-
end
|
6
|
+
class Relationship
|
7
|
+
attr_reader :name, :block, :options, :condition
|
14
8
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
when String
|
22
|
-
serializer.instance_eval(@condition)
|
23
|
-
when Proc
|
24
|
-
if @condition.arity.zero?
|
25
|
-
serializer.instance_exec(&@condition)
|
26
|
-
else
|
27
|
-
serializer.instance_exec(serializer, &@condition)
|
28
|
-
end
|
29
|
-
else
|
30
|
-
nil
|
31
|
-
end
|
32
|
-
end
|
9
|
+
def initialize(name, options = {}, block)
|
10
|
+
@name = name.to_sym
|
11
|
+
@block = block&.freeze
|
12
|
+
@options = options.freeze
|
13
|
+
@condition = options[:if]&.freeze
|
14
|
+
end
|
33
15
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
16
|
+
def should_include?(serializer)
|
17
|
+
case @condition
|
18
|
+
when nil
|
19
|
+
true
|
20
|
+
when Symbol
|
21
|
+
serializer.public_send(@condition)
|
22
|
+
when String
|
23
|
+
serializer.instance_eval(@condition)
|
24
|
+
when Proc
|
25
|
+
if @condition.arity.zero?
|
26
|
+
serializer.instance_exec(&@condition)
|
27
|
+
else
|
28
|
+
serializer.instance_exec(serializer, &@condition)
|
41
29
|
end
|
30
|
+
end
|
31
|
+
end
|
42
32
|
|
43
|
-
|
44
|
-
|
45
|
-
|
33
|
+
def associated_object(serializer)
|
34
|
+
if @block
|
35
|
+
result = if @block.arity.zero?
|
36
|
+
serializer.object.instance_exec(&@block)
|
37
|
+
else
|
38
|
+
@block.call(serializer.object)
|
39
|
+
end
|
40
|
+
return result if result != :nil
|
41
|
+
end
|
42
|
+
serializer.object.public_send(@name)
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
45
|
+
def value(serializer)
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def relationship_serializer(serializer, relationship_object = nil)
|
52
|
+
if @options[:serializer].blank?
|
53
|
+
association_class = if relationship_object.present?
|
54
|
+
relationship_object.class.name
|
55
|
+
else
|
56
|
+
serializer.object.class.reflect_on_association(@name.to_sym).class_name
|
57
|
+
end
|
58
|
+
"#{association_class.demodulize.camelize}Serializer".constantize
|
59
|
+
else
|
60
|
+
@options[:serializer]
|
61
|
+
end
|
60
62
|
end
|
61
|
-
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/plucker/version.rb
CHANGED
data/lib/plucker_serializer.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
1
|
+
require 'plucker/version'
|
2
|
+
require 'plucker/configuration'
|
3
|
+
require 'plucker/base'
|
4
|
+
require 'plucker/attribute'
|
5
|
+
require 'plucker/collection'
|
6
|
+
require 'plucker/relationship'
|
7
|
+
require 'plucker/belongs_to'
|
8
|
+
require 'plucker/has_one'
|
9
|
+
require 'plucker/has_many'
|
10
|
+
require 'plucker/descriptor'
|
11
|
+
require 'plucker/concerns/caching'
|
12
12
|
|
13
13
|
module Plucker
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
def configure
|
20
|
-
yield(config)
|
21
|
-
end
|
14
|
+
class << self
|
15
|
+
def config
|
16
|
+
@config ||= Configuration.new
|
22
17
|
end
|
23
|
-
|
18
|
+
|
19
|
+
def configure
|
20
|
+
yield(config)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
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:
|
11
|
+
date: 2025-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
|
-
rubygems_version: 3.
|
96
|
+
rubygems_version: 3.5.11
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: A blazing fast JSON serializer for ActiveRecord & Ruby objects
|