plucker_serializer 0.7.1 → 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: 965b22f7307d9bb8930fe6e49e44eed985ad2dabd98970e12e79349855f7d99d
4
- data.tar.gz: 1b21385382669c1eb7a6ebb15802a73edaceb8d8f81f9ed0caee8c9f9ade990f
3
+ metadata.gz: e84cbc970b672503e92f8825f69e898d886d513a9a19a8bd04998b358889bf4a
4
+ data.tar.gz: 8fae9558248c250b5414b72630c6637d4ab0215ec95db08e060e16804d036874
5
5
  SHA512:
6
- metadata.gz: ce2ee569cfc9b2fd4157715efa36c823001f0a4aad122270e41d4752048d8e74867869e3dfb51ca8af08ff6d217a6b692855af7dc657cdccdef689cfb331cad2
7
- data.tar.gz: 8cd5c5d6228800fc97429853b3b658ffe12222dca1567b3fea357b475948a4ce23c31cb7c91537b81514fe455c8dccc9b5764540c947d9f0737b92e4317e9687
6
+ metadata.gz: ef153929e8bfaf8c9ae3243f8d99fc7526b93720b0efb06cf720f3f9f58c6e46ec2054c5ade7795c3ad5e1d315563966374bffb1a27ffbea352f9a9e46fb74dd
7
+ data.tar.gz: 57ccf7fac652350f672aae10f617d86976b543d31a7f65f0c78ae2d7a0f4d34ea31bc5e284567dd661af1221ca1a2cbfa83cd06226d86a38bfe1136e1fdd3288
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plucker
4
- VERSION = '0.7.1'
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.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Boisgibault