plucker_serializer 0.7.0 → 0.7.2

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: 5944e6c05d1bab433f405984a62980c72501367bcddae6c91164aaf412eab4fc
4
- data.tar.gz: c3d20b80b2d9759239d1b35b669a6a1ea09bad571c952b9a3c768330937af866
3
+ metadata.gz: e84cbc970b672503e92f8825f69e898d886d513a9a19a8bd04998b358889bf4a
4
+ data.tar.gz: 8fae9558248c250b5414b72630c6637d4ab0215ec95db08e060e16804d036874
5
5
  SHA512:
6
- metadata.gz: d402fd1eebbd1b62394292f11b57a04e02e4d90dd2402272b239da98fe504900d9164dba7dc94c4fc13c8bc5b4db7350b1841021977ebe69c77d187c06b9fc0d
7
- data.tar.gz: fdd3db583cb2d0cee54ae0a96b137657d977a21477d8ea13550ca388162f0a5636108690166f34ce88c10ea5764730985858d9b71f9173422ededaaeb289e359
6
+ metadata.gz: ef153929e8bfaf8c9ae3243f8d99fc7526b93720b0efb06cf720f3f9f58c6e46ec2054c5ade7795c3ad5e1d315563966374bffb1a27ffbea352f9a9e46fb74dd
7
+ data.tar.gz: 57ccf7fac652350f672aae10f617d86976b543d31a7f65f0c78ae2d7a0f4d34ea31bc5e284567dd661af1221ca1a2cbfa83cd06226d86a38bfe1136e1fdd3288
data/lib/plucker/base.rb CHANGED
@@ -14,106 +14,101 @@ module Plucker
14
14
 
15
15
  attr_accessor :object
16
16
 
17
- with_options instance_writer: false, instance_reader: false do |serializer|
18
- serializer.class_attribute :_descriptor
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
- self.object = object
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) do
34
- get_hash
35
- end
34
+ fetch(adapter: :hash) { compute_hash }
36
35
  else
37
- get_hash
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(options = nil)
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) do
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(get_hash, mode: :rails)
50
+ Oj.dump(compute_hash, mode: :rails)
54
51
  end
55
52
  end
56
53
 
57
- def get_hash
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
- hash[key.to_sym] = relationship.value(self)
66
- end
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
- next unless attr.should_include?(self)
72
-
73
- hash[key.to_sym] = attr.value(self)
62
+ hash[key] = attr.value(self) if attr.should_include?(self)
74
63
  end
75
64
  end
76
65
 
77
- def self.pluckable?
78
- self._descriptor.pluckable?
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
- def self.pluckable_columns
82
- self._descriptor._pluckable_columns
83
- end
72
+ class << self
73
+ def pluckable?
74
+ _descriptor.pluckable?
75
+ end
84
76
 
85
- def self.attributes(*attrs)
86
- attrs.each do |attr|
87
- attribute(attr)
77
+ def pluckable_columns
78
+ _descriptor._pluckable_columns
88
79
  end
89
- end
90
80
 
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
81
+ def attributes(*attrs)
82
+ attrs.each { |attr| attribute(attr) }
83
+ end
96
84
 
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
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
102
90
 
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
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
108
96
 
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
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
114
102
 
115
- def self.model(attr)
116
- self._descriptor.set_model(attr)
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
117
112
  end
118
113
  end
119
114
  end
@@ -13,42 +13,40 @@ module Plucker
13
13
 
14
14
  def initialize(objects, options = {})
15
15
  @objects = objects
16
- @options = options
16
+ @options = options.freeze
17
17
  @cache_type = options[:cache] == :multi ? :multi : :collection
18
- @serializer_class = get_serialized_model(objects)
18
+ @serializer_class = determine_serializer_class(objects)
19
19
  end
20
20
 
21
21
  def serializable_hash
22
- if !objects.is_a?(ActiveRecord::Relation)
23
- objects.map do |object|
22
+ unless objects.is_a?(ActiveRecord::Relation)
23
+ return objects.map do |object|
24
24
  serializer_class.new(object).serializable_hash
25
25
  end.compact
26
- elsif serializer_class.cache_enabled?
27
- if @cache_type == :collection
28
- fetch(adapter: :hash) do
29
- get_hash(use_cache: false)
30
- end
31
- elsif @cache_type == :multi
32
- get_hash(use_cache: true)
26
+ end
27
+
28
+ if serializer_class.cache_enabled?
29
+ if cache_type == :collection
30
+ fetch(adapter: :hash) { compute_hash(use_cache: false) }
31
+ elsif cache_type == :multi
32
+ compute_hash(use_cache: true)
33
33
  end
34
34
  else
35
- get_hash(use_cache: false)
35
+ compute_hash(use_cache: false)
36
36
  end
37
37
  end
38
38
  alias to_hash serializable_hash
39
39
  alias to_h serializable_hash
40
40
 
41
- def as_json(options = nil)
41
+ def as_json(_options = nil)
42
42
  serializable_hash
43
43
  end
44
44
 
45
- def to_json(options = {})
45
+ def to_json(_options = {})
46
46
  if serializer_class.cache_enabled?
47
- if @cache_type == :collection
48
- fetch(adapter: :json) do
49
- Oj.dump(get_collection_json(use_cache: false), mode: :rails)
50
- end
51
- elsif @cache_type == :multi
47
+ if cache_type == :collection
48
+ fetch(adapter: :json) { Oj.dump(get_collection_json(use_cache: false), mode: :rails) }
49
+ elsif cache_type == :multi
52
50
  Oj.dump(get_collection_json(use_cache: true), mode: :rails)
53
51
  end
54
52
  else
@@ -56,46 +54,44 @@ module Plucker
56
54
  end
57
55
  end
58
56
 
57
+ def cache_version
58
+ @cache_version ||= objects.cache_version
59
+ end
60
+
61
+ def cache_key(adapter: :json)
62
+ "#{objects.cache_key}/#{serializer_class._cache_digest}/#{adapter}"
63
+ end
64
+
65
+ private
66
+
59
67
  def get_collection_json(use_cache: false)
60
68
  if serializer_class.pluckable?
61
69
  associated_hash
62
70
  else
63
- objects.map do |object|
64
- Oj.load(serializer_class.new(object).to_json(use_cache: use_cache))
65
- end
71
+ objects.map { |object| Oj.load(serializer_class.new(object).to_json(use_cache: use_cache)) }
66
72
  end
67
73
  end
68
74
 
69
- def get_hash(use_cache: false)
75
+ def compute_hash(use_cache: false)
70
76
  if serializer_class.pluckable?
71
77
  associated_hash.map(&:symbolize_keys)
72
78
  else
73
- objects.map do |object|
74
- serializer_class.new(object).serializable_hash(use_cache: use_cache)
75
- end.compact
79
+ objects.map { |object| serializer_class.new(object).serializable_hash(use_cache: use_cache) }.compact
76
80
  end
77
81
  end
78
82
 
79
- def cache_version
80
- @cache_version ||= objects.cache_version
81
- end
82
-
83
- def cache_key(adapter: :json)
84
- "#{objects.cache_key}/#{serializer_class._cache_digest}/#{adapter}"
85
- end
86
-
87
- private
88
-
89
83
  def associated_hash
90
- pluck_to_hash(objects, serializer_class.pluckable_columns.to_a)
84
+ objects.pluck_all(namespaced_columns)
91
85
  end
92
86
 
93
- def pluck_to_hash(objects, attrs)
94
- namespaced_attrs = attrs.map { |attr| "#{objects.model.table_name}.#{attr}" }
95
- objects.pluck_all(namespaced_attrs.join(','))
87
+ def namespaced_columns
88
+ @namespaced_columns ||= begin
89
+ cols = serializer_class.pluckable_columns.to_a.map { |attr| "#{objects.model.table_name}.#{attr}" }
90
+ cols.join(',').freeze
91
+ end
96
92
  end
97
93
 
98
- def get_serialized_model(objects)
94
+ def determine_serializer_class(objects)
99
95
  if options[:serializer].blank?
100
96
  "#{objects.klass.name.demodulize.camelize}Serializer".constantize
101
97
  else
@@ -5,37 +5,39 @@ module Plucker
5
5
  attr_accessor :_serialized_model, :_attributes, :_relationships, :_pluckable_columns, :_pluckable
6
6
 
7
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
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
15
  def pluckable?
16
- _pluckable
16
+ @_pluckable
17
17
  end
18
18
 
19
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
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
- self._pluckable = false
25
+ @_pluckable = false
25
26
  end
26
27
  end
27
28
 
28
29
  def add_relationship(key, relationship)
29
- _relationships[key] = relationship
30
- self._pluckable = false
30
+ @_relationships[key] = relationship
31
+ @_pluckable = false
31
32
  end
32
33
 
33
34
  def set_model(model)
34
- self._serialized_model = model
35
+ @_serialized_model = model
35
36
  end
36
37
 
37
38
  def get_serialized_model(serializer_class)
38
- serializer_class.name.split(/Serializer/).first.constantize
39
+ model_name = serializer_class.name.split(/Serializer/).first.freeze
40
+ model_name.constantize
39
41
  rescue NameError, LoadError
40
42
  nil
41
43
  end
@@ -14,50 +14,45 @@ module Plucker
14
14
  end
15
15
 
16
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)
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
- if @condition.arity.zero?
26
- serializer.instance_exec(&@condition)
27
- else
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 @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
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
- serializer.object.public_send(@name)
34
+
35
+ serializer.object.public_send(name)
43
36
  end
44
37
 
45
- def value(serializer)
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 @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
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
- @options[:serializer]
55
+ options[:serializer]
61
56
  end
62
57
  end
63
58
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plucker
4
- VERSION = '0.7.0'
4
+ VERSION = '0.7.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plucker_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Boisgibault