plucker_serializer 0.2.2 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b8f9afc9f1301b0b3d66270d02332a3c561f154e812c14473d82b26455da90e
4
- data.tar.gz: 9081e6e37b2ae439c167774d58b35702e4bc6d8ba5daea474496feae8bcd7835
3
+ metadata.gz: af8283b8fb399c77763762271db9ee9e01c7cb67a6c1d65076bd76761b81c779
4
+ data.tar.gz: c0e79765da235b4cec454375c967ed85e0483d74a3403e8a11a8c7b30e916e00
5
5
  SHA512:
6
- metadata.gz: 831adf15e780f90a1ee9f64811f7a8bf0a684e929a2030884daac8496fafb645473c4d4633d061463f4ca99afb3134bed70b5d22bc691d0103a2f831bf23af68
7
- data.tar.gz: 03fc6353497d3abb9668fe4c36e8d68cade203310e2b2bd20afaf21baa09ad6caf9f16de434b30aec6c168573525a6c5e7d05daa15f0d570402d26352a412fbb
6
+ metadata.gz: c7acb91c9ec72c63fa5bce5f0d3d5c08979478caf08235efd7abefdf674ce146dd1f9ad7179fb6d40ac9ed4e67c9f152c10d8532dd338ec1b94374f675e6fd9c
7
+ data.tar.gz: fb0c12340759e5d09ccf56f3cbe49c04c112d6d219166de5004b1c241408788cfff8648053b2853b6c8689c15efd7f04e70ac9b37ee44e91ded5a705da752062
data/lib/plucker/base.rb CHANGED
@@ -38,19 +38,19 @@ module Plucker
38
38
  alias to_hash serializable_hash
39
39
  alias to_h serializable_hash
40
40
 
41
- def get_hash
42
- attributes_hash.merge! associations_hash
41
+ def as_json(options = nil)
42
+ serializable_hash
43
43
  end
44
44
 
45
- def as_json(options = nil)
46
- to_h.as_json(options)
45
+ def get_hash
46
+ attributes_hash.merge! associations_hash
47
47
  end
48
48
 
49
49
  def associations_hash
50
50
  hash = {}
51
51
  self.class._descriptor._relationships.each do |(key, relationship)|
52
52
  next if relationship.excluded?(self)
53
- hash[key] = relationship.value(self)
53
+ hash[key.to_s] = relationship.value(self)
54
54
  end
55
55
  hash
56
56
  end
@@ -58,7 +58,7 @@ module Plucker
58
58
  def attributes_hash
59
59
  self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
60
60
  next if attr.excluded?(self)
61
- hash[key] = attr.value(self)
61
+ hash[key.to_s] = attr.value(self)
62
62
  end
63
63
  end
64
64
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require_relative 'concerns/caching'
3
+ require "pluck_all"
3
4
 
4
5
  module Plucker
5
6
  class Collection
@@ -22,22 +23,10 @@ module Plucker
22
23
  else
23
24
  if serializer_class.cache_enabled?
24
25
  fetch do
25
- if serializer_class.is_pluckable?
26
- associated_hash
27
- else
28
- objects.map do |object|
29
- serializer_class.new(object).serializable_hash
30
- end.compact
31
- end
26
+ get_hash
32
27
  end
33
28
  else
34
- if serializer_class.is_pluckable?
35
- associated_hash
36
- else
37
- objects.map do |object|
38
- serializer_class.new(object).serializable_hash
39
- end.compact
40
- end
29
+ get_hash
41
30
  end
42
31
  end
43
32
  end
@@ -45,7 +34,17 @@ module Plucker
45
34
  alias to_h serializable_hash
46
35
 
47
36
  def as_json(options = nil)
48
- to_h.as_json(options)
37
+ serializable_hash
38
+ end
39
+
40
+ def get_hash
41
+ if serializer_class.is_pluckable?
42
+ associated_hash
43
+ else
44
+ objects.map do |object|
45
+ serializer_class.new(object).serializable_hash
46
+ end.compact
47
+ end
49
48
  end
50
49
 
51
50
  def cache_version
@@ -63,9 +62,9 @@ module Plucker
63
62
  pluck_to_hash(objects, serializer_class.pluckable_columns.to_a)
64
63
  end
65
64
 
66
- def pluck_to_hash(object, attrs)
67
- namespaced_attrs = attrs.map { |attr| object.model.table_name.to_s + "." + attr.to_s }
68
- object.pluck(Arel.sql(namespaced_attrs.join(','))).map { |el| attrs.zip(el).to_h }
65
+ def pluck_to_hash(objects, attrs)
66
+ namespaced_attrs = attrs.map { |attr| objects.model.table_name.to_s + "." + attr.to_s }
67
+ objects.pluck_all(namespaced_attrs.join(','))
69
68
  end
70
69
 
71
70
  def get_serialized_model(objects)
@@ -7,12 +7,12 @@ module Plucker
7
7
  self._serialized_model = get_serialized_model(serializer_class)
8
8
  self._attributes = {}
9
9
  self._relationships = {}
10
- self._pluckable_columns = Set.new([:updated_at])
10
+ self._pluckable_columns = Set.new
11
11
  self._is_pluckable = true
12
12
  end
13
13
 
14
14
  def is_pluckable?
15
- self._is_pluckable && !self._relationships.present?
15
+ self._is_pluckable
16
16
  end
17
17
 
18
18
  def add_attribute(key, attr)
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Plucker
3
- VERSION = "0.2.2"
3
+ VERSION = "0.3.0"
4
4
  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.2.2
4
+ version: 0.3.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: 2022-08-01 00:00:00.000000000 Z
11
+ date: 2022-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pluck_all
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: A blazing fast JSON serializer for ActiveRecord & Ruby objects
28
42
  email: henry@logora.fr
29
43
  executables: []