plucker_serializer 0.4.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: b1afbdbecad2e29b9ee1e5cc59f143597f8995ad851b2d4a7b28ddcab61349de
4
- data.tar.gz: 6a4bbd5de3a4d833833030fa8c2e35f43fc3ed56c2b2bfaeb435352495a904d0
3
+ metadata.gz: bf134b11d47d300e2df621e5f027ea881cfae344ae1a807eeb8a51b001888720
4
+ data.tar.gz: 20f2cee76e31ed9d07f69fe41013cc733b1f3194bc940425309b93639763c1aa
5
5
  SHA512:
6
- metadata.gz: d06fa3ebe3216c2aef19f60eb42887f8d4190111ce6f6d2d07010d2e0401965d9a5968e01e1da0915fd4dea468f727b2d05df323dda072351559e590c851cb04
7
- data.tar.gz: 2ee3ef45ca3635bdf8400ec8b988a7d6ee2fc0bc524a9ee957287f8f59a8713ffcc04b5ec91195fa7f546b261152e395b107d22d25e6b756ca656fff5f5c0c16
6
+ metadata.gz: d2693c8756d65c1a32f5b0be8aa857e9b5b0c77e8574b99de84ddac9289754f8df4f97cb80cd0928c1aa40ca23588366f0433b01ae60c1668081861f29c20c50
7
+ data.tar.gz: 9a11ff786953fe594b81dea45f4fa883bbb947bae007207c5724ba0c8077cfcdc0329b9f95457379c906ef39bee27d2acd6d4059eb103184b253a9dcdedf16c4
@@ -1,5 +1,51 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Plucker
3
- class Attribute < Plucker::Field
4
+ class Attribute
5
+ attr_reader :name, :block, :condition
6
+
7
+ def initialize(name, options = {}, block)
8
+ @name = name.to_sym
9
+ @block = block&.freeze
10
+ @condition = options[:if]&.freeze
4
11
  end
5
- end
12
+
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
33
+
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)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
data/lib/plucker/base.rb CHANGED
@@ -1,104 +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
31
- if self.class.cache_enabled?
32
- fetch 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 = {})
47
- Oj.dump(serializable_hash, mode: :rails)
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)
48
51
  end
52
+ else
53
+ Oj.dump(get_hash, mode: :rails)
54
+ end
55
+ end
49
56
 
50
- def get_hash
51
- attributes_hash.merge! associations_hash
52
- end
57
+ def get_hash
58
+ attributes_hash.merge! associations_hash
59
+ end
53
60
 
54
- def associations_hash
55
- hash = {}
56
- self.class._descriptor._relationships.each do |(key, relationship)|
57
- next if relationship.excluded?(self)
58
- hash[key.to_s] = relationship.value(self)
59
- end
60
- hash
61
- end
61
+ def associations_hash
62
+ self.class._descriptor._relationships.each_with_object({}) do |(key, relationship), hash|
63
+ next unless relationship.should_include?(self)
62
64
 
63
- def attributes_hash
64
- self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
65
- next if attr.excluded?(self)
66
- hash[key.to_s] = attr.value(self)
67
- end
68
- end
65
+ hash[key.to_s] = relationship.value(self)
66
+ end
67
+ end
69
68
 
70
- def self.is_pluckable?
71
- self._descriptor.is_pluckable?
72
- end
69
+ def attributes_hash
70
+ self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
71
+ next unless attr.should_include?(self)
73
72
 
74
- def self.pluckable_columns
75
- self._descriptor._pluckable_columns
76
- end
73
+ hash[key.to_s] = attr.value(self)
74
+ end
75
+ end
77
76
 
78
- def self.attributes(*attrs)
79
- attrs.each do |attr|
80
- attribute(attr)
81
- end
82
- end
77
+ def self.is_pluckable?
78
+ self._descriptor.is_pluckable?
79
+ end
83
80
 
84
- def self.attribute(attr, options = {}, &block)
85
- self._descriptor.add_attribute(options.fetch(:key, attr), Plucker::Attribute.new(attr, options, block))
86
- end
81
+ def self.pluckable_columns
82
+ self._descriptor._pluckable_columns
83
+ end
87
84
 
88
- def self.belongs_to(attr, options = {}, &block)
89
- self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::BelongsTo.new(attr, options, block))
90
- end
85
+ def self.attributes(*attrs)
86
+ attrs.each do |attr|
87
+ attribute(attr)
88
+ end
89
+ end
91
90
 
92
- def self.has_one(attr, options = {}, &block)
93
- self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::HasOne.new(attr, options, block))
94
- 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
95
94
 
96
- def self.has_many(attr, options = {}, &block)
97
- self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::HasMany.new(attr, options, block))
98
- 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
99
98
 
100
- def self.model(attr)
101
- self._descriptor.set_model(attr)
102
- 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)
103
109
  end
104
- 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,18 +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 @cache_type == :collection && serializer_class.cache_enabled?
27
- fetch do
28
- get_hash
26
+ elsif serializer_class.cache_enabled?
27
+ if @cache_type == :collection
28
+ fetch(adapter: :hash) do
29
+ get_hash(use_cache: false)
29
30
  end
30
- else
31
- get_hash
31
+ elsif @cache_type == :multi
32
+ get_hash(use_cache: true)
32
33
  end
34
+ else
35
+ get_hash(use_cache: false)
33
36
  end
34
37
  end
35
38
  alias to_hash serializable_hash
@@ -40,36 +43,57 @@ module Plucker
40
43
  end
41
44
 
42
45
  def to_json(options = {})
43
- Oj.dump(serializable_hash, mode: :rails)
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
52
+ Oj.dump(get_collection_json(use_cache: true), mode: :rails)
53
+ end
54
+ else
55
+ Oj.dump(get_collection_json(use_cache: false), mode: :rails)
56
+ end
44
57
  end
45
58
 
46
- def get_hash
59
+ def get_collection_json(use_cache: false)
47
60
  if serializer_class.is_pluckable?
48
61
  associated_hash
49
62
  else
50
63
  objects.map do |object|
51
- serializer_class.new(object).serializable_hash
64
+ Oj.load(serializer_class.new(object).to_json(use_cache: use_cache))
65
+ end
66
+ end
67
+ end
68
+
69
+ def get_hash(use_cache: false)
70
+ if serializer_class.is_pluckable?
71
+ associated_hash
72
+ else
73
+ objects.map do |object|
74
+ serializer_class.new(object).serializable_hash(use_cache: use_cache)
52
75
  end.compact
53
76
  end
54
77
  end
55
78
 
56
79
  def cache_version
57
80
  return @cache_version if defined?(@cache_version)
81
+
58
82
  @cache_version = objects.cache_version
59
83
  end
60
84
 
61
- def cache_key
62
- return @cache_key if defined?(@cache_key)
63
- @cache_key = objects.cache_key + '/' + serializer_class._cache_digest
85
+ def cache_key(adapter: :json)
86
+ "#{objects.cache_key}/#{serializer_class._cache_digest}/#{adapter}"
64
87
  end
65
88
 
66
89
  private
90
+
67
91
  def associated_hash
68
92
  pluck_to_hash(objects, serializer_class.pluckable_columns.to_a)
69
93
  end
70
94
 
71
95
  def pluck_to_hash(objects, attrs)
72
- 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}" }
73
97
  objects.pluck_all(namespaced_attrs.join(','))
74
98
  end
75
99
 
@@ -81,4 +105,4 @@ module Plucker
81
105
  end
82
106
  end
83
107
  end
84
- end
108
+ end
@@ -30,9 +30,9 @@ module Plucker
30
30
  end
31
31
  end
32
32
 
33
- def fetch
33
+ def fetch(adapter: :json)
34
34
  if serializer_class.cache_enabled?
35
- serializer_class._cache_store.fetch(cache_key, version: cache_version, options: serializer_class._cache_options) do
35
+ serializer_class._cache_store.fetch(cache_key(adapter: adapter), version: cache_version, options: serializer_class._cache_options) do
36
36
  yield
37
37
  end
38
38
  else
@@ -45,9 +45,8 @@ module Plucker
45
45
  @cache_version = object.cache_version
46
46
  end
47
47
 
48
- def cache_key
49
- return @cache_key if defined?(@cache_key)
50
- @cache_key = object.cache_key + "/" + serializer_class._cache_digest
48
+ def cache_key(adapter: :json)
49
+ object.cache_key + "/" + serializer_class._cache_digest + "/" + adapter.to_s
51
50
  end
52
51
 
53
52
  def serializer_class
@@ -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,34 +1,64 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative 'collection'
3
- require_relative "field"
4
4
 
5
5
  module Plucker
6
- class Relationship < Plucker::Field
7
- def associated_object(serializer)
8
- block_value = instance_exec(serializer.object, &block) if block
9
- if block && block_value != :nil
10
- block_value
11
- else
12
- serializer.object.send(name)
13
- end
14
- end
6
+ class Relationship
7
+ attr_reader :name, :block, :options, :condition
15
8
 
16
- def value(serializer)
17
- nil
18
- 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
19
15
 
20
- private
21
- def relationship_serializer(serializer, relationship_object=nil)
22
- if self.options[:serializer].blank?
23
- if relationship_object.present?
24
- association_class = relationship_object.class.name
25
- else
26
- association_class = serializer.object.class.reflect_on_association(self.name.to_sym).class_name
27
- end
28
- "#{association_class.demodulize.camelize}Serializer".constantize
29
- else
30
- self.options[:serializer]
31
- 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)
32
29
  end
30
+ end
31
+ end
32
+
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
44
+
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
33
62
  end
34
- end
63
+ end
64
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Plucker
3
- VERSION = "0.4.0"
4
- end
4
+ VERSION = '0.6.0'
5
+ end
@@ -1,24 +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/field"
11
- require "plucker/descriptor"
12
- 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'
13
12
 
14
13
  module Plucker
15
- class << self
16
- def config
17
- @config ||= Configuration.new
18
- end
19
-
20
- def configure
21
- yield(config)
22
- end
14
+ class << self
15
+ def config
16
+ @config ||= Configuration.new
23
17
  end
24
- 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.4.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-03 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
@@ -65,7 +65,6 @@ files:
65
65
  - lib/plucker/concerns/caching.rb
66
66
  - lib/plucker/configuration.rb
67
67
  - lib/plucker/descriptor.rb
68
- - lib/plucker/field.rb
69
68
  - lib/plucker/has_many.rb
70
69
  - lib/plucker/has_one.rb
71
70
  - lib/plucker/relationship.rb
@@ -94,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
93
  - !ruby/object:Gem::Version
95
94
  version: '0'
96
95
  requirements: []
97
- rubygems_version: 3.1.2
96
+ rubygems_version: 3.5.11
98
97
  signing_key:
99
98
  specification_version: 4
100
99
  summary: A blazing fast JSON serializer for ActiveRecord & Ruby objects
data/lib/plucker/field.rb DELETED
@@ -1,81 +0,0 @@
1
- # frozen_string_literal: true
2
- module Plucker
3
- Field = Struct.new(:name, :options, :block) do
4
- def initialize(*)
5
- super
6
- validate_condition!
7
- end
8
-
9
- def value(serializer)
10
- block_value = instance_exec(serializer.object, &block) if block
11
- if block && block_value != :nil
12
- block_value
13
- else
14
- if serializer.respond_to?(name)
15
- serializer.send(name)
16
- else
17
- serializer.object.send(name)
18
- end
19
- end
20
- end
21
-
22
- def is_pluckable?
23
- block.blank?
24
- end
25
-
26
- def excluded?(serializer)
27
- case condition_type
28
- when :if
29
- !evaluate_condition(serializer)
30
- when :unless
31
- evaluate_condition(serializer)
32
- else
33
- false
34
- end
35
- end
36
-
37
- private
38
- def validate_condition!
39
- return if condition_type == :none
40
-
41
- case condition
42
- when Symbol, String, Proc
43
- # noop
44
- else
45
- fail TypeError, "#{condition_type.inspect} should be a Symbol, String or Proc"
46
- end
47
- end
48
-
49
- def evaluate_condition(serializer)
50
- case condition
51
- when Symbol
52
- serializer.public_send(condition)
53
- when String
54
- serializer.instance_eval(condition)
55
- when Proc
56
- if condition.arity.zero?
57
- serializer.instance_exec(&condition)
58
- else
59
- serializer.instance_exec(serializer, &condition)
60
- end
61
- else
62
- nil
63
- end
64
- end
65
-
66
- def condition_type
67
- @condition_type ||=
68
- if options.key?(:if)
69
- :if
70
- elsif options.key?(:unless)
71
- :unless
72
- else
73
- :none
74
- end
75
- end
76
-
77
- def condition
78
- options[condition_type]
79
- end
80
- end
81
- end