plucker_serializer 0.2.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/plucker/base.rb +11 -6
- data/lib/plucker/collection.rb +25 -20
- data/lib/plucker/descriptor.rb +2 -2
- data/lib/plucker/relationship.rb +6 -1
- data/lib/plucker/version.rb +1 -1
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1afbdbecad2e29b9ee1e5cc59f143597f8995ad851b2d4a7b28ddcab61349de
|
4
|
+
data.tar.gz: 6a4bbd5de3a4d833833030fa8c2e35f43fc3ed56c2b2bfaeb435352495a904d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d06fa3ebe3216c2aef19f60eb42887f8d4190111ce6f6d2d07010d2e0401965d9a5968e01e1da0915fd4dea468f727b2d05df323dda072351559e590c851cb04
|
7
|
+
data.tar.gz: 2ee3ef45ca3635bdf8400ec8b988a7d6ee2fc0bc524a9ee957287f8f59a8713ffcc04b5ec91195fa7f546b261152e395b107d22d25e6b756ca656fff5f5c0c16
|
data/lib/plucker/base.rb
CHANGED
@@ -4,6 +4,7 @@ require_relative 'descriptor'
|
|
4
4
|
require_relative 'has_many'
|
5
5
|
require_relative 'belongs_to'
|
6
6
|
require_relative 'has_one'
|
7
|
+
require "oj"
|
7
8
|
require 'active_support/all'
|
8
9
|
|
9
10
|
module Plucker
|
@@ -38,19 +39,23 @@ module Plucker
|
|
38
39
|
alias to_hash serializable_hash
|
39
40
|
alias to_h serializable_hash
|
40
41
|
|
41
|
-
def
|
42
|
-
|
42
|
+
def as_json(options = nil)
|
43
|
+
serializable_hash
|
43
44
|
end
|
44
45
|
|
45
|
-
def
|
46
|
-
|
46
|
+
def to_json(options = {})
|
47
|
+
Oj.dump(serializable_hash, mode: :rails)
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_hash
|
51
|
+
attributes_hash.merge! associations_hash
|
47
52
|
end
|
48
53
|
|
49
54
|
def associations_hash
|
50
55
|
hash = {}
|
51
56
|
self.class._descriptor._relationships.each do |(key, relationship)|
|
52
57
|
next if relationship.excluded?(self)
|
53
|
-
hash[key] = relationship.value(self)
|
58
|
+
hash[key.to_s] = relationship.value(self)
|
54
59
|
end
|
55
60
|
hash
|
56
61
|
end
|
@@ -58,7 +63,7 @@ module Plucker
|
|
58
63
|
def attributes_hash
|
59
64
|
self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
|
60
65
|
next if attr.excluded?(self)
|
61
|
-
hash[key] = attr.value(self)
|
66
|
+
hash[key.to_s] = attr.value(self)
|
62
67
|
end
|
63
68
|
end
|
64
69
|
|
data/lib/plucker/collection.rb
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require_relative 'concerns/caching'
|
3
|
+
require "oj"
|
4
|
+
require "pluck_all"
|
3
5
|
|
4
6
|
module Plucker
|
5
7
|
class Collection
|
6
8
|
include Enumerable
|
7
9
|
include Caching
|
8
10
|
|
9
|
-
attr_reader :objects, :serializer_class, :options
|
11
|
+
attr_reader :objects, :cache_type, :serializer_class, :options
|
10
12
|
|
11
13
|
def initialize(objects, options = {})
|
12
14
|
@objects = objects
|
13
15
|
@options = options
|
16
|
+
@cache_type = options[:cache] == :multi ? :multi : :collection
|
14
17
|
@serializer_class = get_serialized_model(objects)
|
15
18
|
end
|
16
19
|
|
@@ -20,24 +23,12 @@ module Plucker
|
|
20
23
|
serializer_class.new(object).serializable_hash
|
21
24
|
end.compact
|
22
25
|
else
|
23
|
-
if serializer_class.cache_enabled?
|
26
|
+
if @cache_type == :collection && serializer_class.cache_enabled?
|
24
27
|
fetch do
|
25
|
-
|
26
|
-
associated_hash
|
27
|
-
else
|
28
|
-
objects.map do |object|
|
29
|
-
serializer_class.new(object).serializable_hash
|
30
|
-
end.compact
|
31
|
-
end
|
28
|
+
get_hash
|
32
29
|
end
|
33
30
|
else
|
34
|
-
|
35
|
-
associated_hash
|
36
|
-
else
|
37
|
-
objects.map do |object|
|
38
|
-
serializer_class.new(object).serializable_hash
|
39
|
-
end.compact
|
40
|
-
end
|
31
|
+
get_hash
|
41
32
|
end
|
42
33
|
end
|
43
34
|
end
|
@@ -45,7 +36,21 @@ module Plucker
|
|
45
36
|
alias to_h serializable_hash
|
46
37
|
|
47
38
|
def as_json(options = nil)
|
48
|
-
|
39
|
+
serializable_hash
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_json(options = {})
|
43
|
+
Oj.dump(serializable_hash, mode: :rails)
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_hash
|
47
|
+
if serializer_class.is_pluckable?
|
48
|
+
associated_hash
|
49
|
+
else
|
50
|
+
objects.map do |object|
|
51
|
+
serializer_class.new(object).serializable_hash
|
52
|
+
end.compact
|
53
|
+
end
|
49
54
|
end
|
50
55
|
|
51
56
|
def cache_version
|
@@ -63,9 +68,9 @@ module Plucker
|
|
63
68
|
pluck_to_hash(objects, serializer_class.pluckable_columns.to_a)
|
64
69
|
end
|
65
70
|
|
66
|
-
def pluck_to_hash(
|
67
|
-
namespaced_attrs = attrs.map { |attr|
|
68
|
-
|
71
|
+
def pluck_to_hash(objects, attrs)
|
72
|
+
namespaced_attrs = attrs.map { |attr| objects.model.table_name.to_s + "." + attr.to_s }
|
73
|
+
objects.pluck_all(namespaced_attrs.join(','))
|
69
74
|
end
|
70
75
|
|
71
76
|
def get_serialized_model(objects)
|
data/lib/plucker/descriptor.rb
CHANGED
@@ -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
|
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
|
15
|
+
self._is_pluckable
|
16
16
|
end
|
17
17
|
|
18
18
|
def add_attribute(key, attr)
|
data/lib/plucker/relationship.rb
CHANGED
@@ -5,7 +5,12 @@ require_relative "field"
|
|
5
5
|
module Plucker
|
6
6
|
class Relationship < Plucker::Field
|
7
7
|
def associated_object(serializer)
|
8
|
-
serializer.object
|
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
|
9
14
|
end
|
10
15
|
|
11
16
|
def value(serializer)
|
data/lib/plucker/version.rb
CHANGED
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
|
+
version: 0.4.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-
|
11
|
+
date: 2022-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oj
|
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'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pluck_all
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
27
55
|
description: A blazing fast JSON serializer for ActiveRecord & Ruby objects
|
28
56
|
email: henry@logora.fr
|
29
57
|
executables: []
|