plucker_serializer 0.5.0 → 0.6.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: 3b74f3d1f2fee4bfa2fd0fc3e6968f35f6d1bbe852b69c6fda56ad63bdbe8ada
4
- data.tar.gz: c9daa990c9d467958e6f30dd2faba3a69aa81dfa0708a6289243026504f88351
3
+ metadata.gz: bf134b11d47d300e2df621e5f027ea881cfae344ae1a807eeb8a51b001888720
4
+ data.tar.gz: 20f2cee76e31ed9d07f69fe41013cc733b1f3194bc940425309b93639763c1aa
5
5
  SHA512:
6
- metadata.gz: f051ad0a434f02690092cdeb7481db0fc606b780574d8109251746f99b3f1888842c906231321f82ba896e9615939461a93ad95245082086235cb670fc27710b
7
- data.tar.gz: b05c2e4c4f327a32c0c9faeae82fc017265f316e41f5f111a709577801862a70d481f7545b226b8cb848a8a83d705702a963851e9582ba2fbc72ec670ba3ccb3
6
+ metadata.gz: d2693c8756d65c1a32f5b0be8aa857e9b5b0c77e8574b99de84ddac9289754f8df4f97cb80cd0928c1aa40ca23588366f0433b01ae60c1668081861f29c20c50
7
+ data.tar.gz: 9a11ff786953fe594b81dea45f4fa883bbb947bae007207c5724ba0c8077cfcdc0329b9f95457379c906ef39bee27d2acd6d4059eb103184b253a9dcdedf16c4
@@ -1,48 +1,51 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Plucker
3
- class Attribute
4
- attr_reader :name, :block, :condition
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
- def value(serializer)
13
- block_value = instance_exec(serializer.object, &@block) if @block
14
- if @block && block_value != :nil
15
- block_value
16
- else
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
- def is_pluckable?
26
- @block.blank?
27
- end
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 is_pluckable?
31
+ @block.blank?
32
+ end
28
33
 
29
- def should_include?(serializer)
30
- case @condition
31
- when nil
32
- true
33
- when Symbol
34
- serializer.public_send(@condition)
35
- when String
36
- serializer.instance_eval(@condition)
37
- when Proc
38
- if @condition.arity.zero?
39
- serializer.instance_exec(&@condition)
40
- else
41
- serializer.instance_exec(serializer, &@condition)
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,111 @@
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 "oj"
8
+ require 'oj'
8
9
  require 'active_support/all'
9
10
 
10
11
  module Plucker
11
- class Base
12
- include Caching
12
+ class Base
13
+ include Caching
13
14
 
14
- attr_accessor :object
15
+ attr_accessor :object
15
16
 
16
- with_options instance_writer: false, instance_reader: false do |serializer|
17
- serializer.class_attribute :_descriptor
18
- self._descriptor ||= Plucker::Descriptor.new(self.class)
19
- end
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
- def self.inherited(base)
22
- super
23
- base._descriptor = Plucker::Descriptor.new(base)
24
- end
22
+ def self.inherited(base)
23
+ super
24
+ base._descriptor = Plucker::Descriptor.new(base)
25
+ end
25
26
 
26
- def initialize(object, options = {})
27
- self.object = object
28
- end
27
+ def initialize(object, options = {})
28
+ self.object = object
29
+ end
29
30
 
30
- def serializable_hash(use_cache: true)
31
- if use_cache && self.class.cache_enabled?
32
- fetch(adapter: :hash) do
33
- get_hash
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
- alias to_hash serializable_hash
40
- alias to_h serializable_hash
36
+ else
37
+ get_hash
38
+ end
39
+ end
40
+ alias to_hash serializable_hash
41
+ alias to_h serializable_hash
41
42
 
42
- def as_json(options = nil)
43
- serializable_hash
44
- end
43
+ def as_json(options = nil)
44
+ serializable_hash
45
+ end
45
46
 
46
- def to_json(options = {}, use_cache: true)
47
- if use_cache && self.class.cache_enabled?
48
- fetch(adapter: :json) do
49
- Oj.dump(get_hash, mode: :rails)
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
- def get_hash
57
- attributes_hash.merge! associations_hash
58
- end
57
+ def get_hash
58
+ attributes_hash.merge! associations_hash
59
+ end
59
60
 
60
- def associations_hash
61
- self.class._descriptor._relationships.each_with_object({}) do |(key, relationship), hash|
62
- next if !relationship.should_include?(self)
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
- def attributes_hash
68
- self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
69
- next if !attr.should_include?(self)
70
- hash[key.to_s] = attr.value(self)
71
- end
72
- end
65
+ hash[key.to_s] = relationship.value(self)
66
+ end
67
+ end
73
68
 
74
- def self.is_pluckable?
75
- self._descriptor.is_pluckable?
76
- end
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
- def self.pluckable_columns
79
- self._descriptor._pluckable_columns
80
- end
73
+ hash[key.to_s] = attr.value(self)
74
+ end
75
+ end
81
76
 
82
- def self.attributes(*attrs)
83
- attrs.each do |attr|
84
- attribute(attr)
85
- end
86
- end
77
+ def self.is_pluckable?
78
+ self._descriptor.is_pluckable?
79
+ end
87
80
 
88
- def self.attribute(attr, options = {}, &block)
89
- self._descriptor.add_attribute(options.fetch(:key, attr), Plucker::Attribute.new(attr, options, block))
90
- end
81
+ def self.pluckable_columns
82
+ self._descriptor._pluckable_columns
83
+ end
91
84
 
92
- def self.belongs_to(attr, options = {}, &block)
93
- self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::BelongsTo.new(attr, options, block))
94
- end
85
+ def self.attributes(*attrs)
86
+ attrs.each do |attr|
87
+ attribute(attr)
88
+ end
89
+ end
95
90
 
96
- def self.has_one(attr, options = {}, &block)
97
- self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::HasOne.new(attr, options, block))
98
- end
91
+ def self.attribute(attr, options = {}, &block)
92
+ self._descriptor.add_attribute(options.fetch(:key, attr), Plucker::Attribute.new(attr, options, block))
93
+ end
99
94
 
100
- def self.has_many(attr, options = {}, &block)
101
- self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::HasMany.new(attr, options, block))
102
- end
95
+ def self.belongs_to(attr, options = {}, &block)
96
+ self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::BelongsTo.new(attr, options, block))
97
+ end
103
98
 
104
- def self.model(attr)
105
- self._descriptor.set_model(attr)
106
- end
99
+ def self.has_one(attr, options = {}, &block)
100
+ self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::HasOne.new(attr, options, block))
101
+ end
102
+
103
+ def self.has_many(attr, options = {}, &block)
104
+ self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::HasMany.new(attr, options, block))
105
+ end
106
+
107
+ def self.model(attr)
108
+ self._descriptor.set_model(attr)
107
109
  end
108
- end
110
+ end
111
+ end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
- require_relative "relationship"
2
+
3
+ require_relative 'relationship'
3
4
 
4
5
  module Plucker
5
- class BelongsTo < Plucker::Relationship
6
- def value(serializer)
7
- relationship_object = self.associated_object(serializer)
8
- return nil if relationship_object.blank?
9
- relationship_serializer(serializer, relationship_object).new(relationship_object).serializable_hash
10
- end
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
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative 'concerns/caching'
3
- require "oj"
4
- require "pluck_all"
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 (not objects.is_a?(ActiveRecord::Relation))
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
- else
26
- if 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
+ 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
- else
35
- get_hash(use_cache: false)
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
@@ -79,20 +78,22 @@ module Plucker
79
78
 
80
79
  def cache_version
81
80
  return @cache_version if defined?(@cache_version)
81
+
82
82
  @cache_version = objects.cache_version
83
83
  end
84
84
 
85
85
  def cache_key(adapter: :json)
86
- objects.cache_key + '/' + serializer_class._cache_digest + "/" + adapter.to_s
86
+ "#{objects.cache_key}/#{serializer_class._cache_digest}/#{adapter}"
87
87
  end
88
88
 
89
89
  private
90
+
90
91
  def associated_hash
91
92
  pluck_to_hash(objects, serializer_class.pluckable_columns.to_a)
92
93
  end
93
94
 
94
95
  def pluck_to_hash(objects, attrs)
95
- namespaced_attrs = attrs.map { |attr| objects.model.table_name.to_s + "." + attr.to_s }
96
+ namespaced_attrs = attrs.map { |attr| "#{objects.model.table_name}.#{attr}" }
96
97
  objects.pluck_all(namespaced_attrs.join(','))
97
98
  end
98
99
 
@@ -104,4 +105,4 @@ module Plucker
104
105
  end
105
106
  end
106
107
  end
107
- end
108
+ end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plucker
4
- class Configuration
5
- attr_accessor :cache_store
6
-
7
- def initialize
8
- @cache_store = nil
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
@@ -1,44 +1,43 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Plucker
3
- class Descriptor
4
- attr_accessor :_serialized_model, :_attributes, :_relationships, :_pluckable_columns, :_is_pluckable
4
+ class Descriptor
5
+ attr_accessor :_serialized_model, :_attributes, :_relationships, :_pluckable_columns, :_is_pluckable
5
6
 
6
- def initialize(serializer_class)
7
- self._serialized_model = get_serialized_model(serializer_class)
8
- self._attributes = {}
9
- self._relationships = {}
10
- self._pluckable_columns = Set.new
11
- self._is_pluckable = true
12
- end
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._is_pluckable = true
13
+ end
13
14
 
14
- def is_pluckable?
15
- self._is_pluckable
16
- end
15
+ def is_pluckable?
16
+ _is_pluckable
17
+ end
17
18
 
18
- def add_attribute(key, attr)
19
- self._attributes[key] = attr
20
- if attr.is_pluckable? && self._serialized_model && self._serialized_model.column_names.include?(attr.name.to_s)
21
- self._pluckable_columns << attr.name
22
- else
23
- self._is_pluckable = false
24
- end
25
- end
19
+ def add_attribute(key, attr)
20
+ _attributes[key] = attr
21
+ if attr.is_pluckable? && _serialized_model&.column_names&.include?(attr.name.to_s)
22
+ _pluckable_columns << attr.name
23
+ else
24
+ self._is_pluckable = false
25
+ end
26
+ end
26
27
 
27
- def add_relationship(key, relationship)
28
- self._relationships[key] = relationship
29
- self._is_pluckable = false
30
- end
28
+ def add_relationship(key, relationship)
29
+ _relationships[key] = relationship
30
+ self._is_pluckable = false
31
+ end
31
32
 
32
- def set_model(model)
33
- self._serialized_model = model
34
- end
33
+ def set_model(model)
34
+ self._serialized_model = model
35
+ end
35
36
 
36
- def get_serialized_model(serializer_class)
37
- begin
38
- serializer_class.name.split(/Serializer/).first.constantize
39
- rescue NameError, LoadError => e
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 => e
40
+ nil
43
41
  end
44
- end
42
+ end
43
+ end
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
- require_relative "relationship"
3
- require_relative "collection"
2
+
3
+ require_relative 'relationship'
4
+ require_relative 'collection'
4
5
 
5
6
  module Plucker
6
- class HasMany < Plucker::Relationship
7
- def value(serializer)
8
- Plucker::Collection.new(self.associated_object(serializer), serializer: relationship_serializer(serializer)).serializable_hash
9
- end
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
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
- require_relative "relationship"
2
+
3
+ require_relative 'relationship'
3
4
 
4
5
  module Plucker
5
- class HasOne < Plucker::Relationship
6
- def value(serializer)
7
- relationship_object = self.associated_object(serializer)
8
- return nil if relationship_object.blank?
9
- relationship_serializer(serializer, relationship_object).new(relationship_object).serializable_hash
10
- end
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
@@ -1,61 +1,64 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative 'collection'
3
4
 
4
5
  module Plucker
5
- class Relationship
6
- attr_reader :name, :block, :options, :condition
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
- def should_include?(serializer)
16
- case @condition
17
- when nil
18
- true
19
- when Symbol
20
- serializer.public_send(@condition)
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
- def associated_object(serializer)
35
- block_value = instance_exec(serializer.object, &@block) if @block
36
- if @block && block_value != :nil
37
- block_value
38
- else
39
- serializer.object.send(@name)
40
- end
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
- def value(serializer)
44
- nil
45
- end
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
- private
48
- def relationship_serializer(serializer, relationship_object=nil)
49
- if @options[:serializer].blank?
50
- if relationship_object.present?
51
- association_class = relationship_object.class.name
52
- else
53
- association_class = serializer.object.class.reflect_on_association(@name.to_sym).class_name
54
- end
55
- "#{association_class.demodulize.camelize}Serializer".constantize
56
- else
57
- @options[:serializer]
58
- end
59
- end
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
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Plucker
3
- VERSION = "0.5.0"
4
- end
4
+ VERSION = '0.6.0'
5
+ end
@@ -1,23 +1,23 @@
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"
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
- class << self
15
- def config
16
- @config ||= Configuration.new
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
- end
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.5.0
4
+ version: 0.6.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-05 00:00:00.000000000 Z
11
+ date: 2025-02-10 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.1.2
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