transmutation 0.4.5 → 0.5.1
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 +4 -4
- data/Gemfile +3 -0
- data/lib/transmutation/serialization/lookup.rb +9 -13
- data/lib/transmutation/serialization.rb +14 -6
- data/lib/transmutation/serializer.rb +19 -17
- data/lib/transmutation/version.rb +1 -1
- data/lib/transmutation.rb +2 -2
- metadata +3 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d1be90589a50f1187323dfcb572a25a5f50329a71da0550deb133c7529cc4cf
|
4
|
+
data.tar.gz: 7dd9f520d65308273d0207c70a7ebe6288417a8afd0b76f299b8e61da0621c45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1682f31fa475e292df1e01d1e2c1eba4d3fb91982aa28e7cecdbf41114f9099faf28ed5deefbc9bb331ba40bfbdec117e6970ff70861df0c9308850e46439a36
|
7
|
+
data.tar.gz: 370d427728ab870d602a43bb5dd809f5bc339e3513560259ad05c0aaf3617ea81c301221ec208d966d81312f824bf6b6ac6c0f402ff49bb4ccaec4dd1d6cfecc
|
data/Gemfile
CHANGED
@@ -18,13 +18,13 @@ module Transmutation
|
|
18
18
|
# namespace: Api::V1::Admin::Detailed
|
19
19
|
# serializer: Chat::User
|
20
20
|
#
|
21
|
-
# This method will attempt to find a serializer defined in the following order:
|
21
|
+
# # This method will attempt to find a serializer defined in the following order:
|
22
22
|
#
|
23
|
-
# - Api::V1::Admin::Detailed::Chat::UserSerializer
|
24
|
-
# - Api::V1::Admin::Chat::UserSerializer
|
25
|
-
# - Api::V1::Chat::UserSerializer
|
26
|
-
# - Api::Chat::UserSerializer
|
27
|
-
# - Chat::UserSerializer
|
23
|
+
# # - Api::V1::Admin::Detailed::Chat::UserSerializer
|
24
|
+
# # - Api::V1::Admin::Chat::UserSerializer
|
25
|
+
# # - Api::V1::Chat::UserSerializer
|
26
|
+
# # - Api::Chat::UserSerializer
|
27
|
+
# # - Chat::UserSerializer
|
28
28
|
def serializer_for(object, serializer: nil)
|
29
29
|
serializer_name = serializer_name_for(object, serializer:)
|
30
30
|
|
@@ -65,14 +65,10 @@ module Transmutation
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def serializer_namespace
|
68
|
-
return
|
69
|
-
return @namespace
|
68
|
+
return @caller.namespace if @namespace.nil?
|
69
|
+
return @namespace if @namespace.start_with?("::")
|
70
70
|
|
71
|
-
"#{
|
72
|
-
end
|
73
|
-
|
74
|
-
def caller_namespace
|
75
|
-
@caller_namespace ||= @caller.class.name.split("::")[...-1].join("::")
|
71
|
+
"#{@caller.namespace}::#{@namespace}"
|
76
72
|
end
|
77
73
|
|
78
74
|
def constantize_serializer!(namespace, name, object:)
|
@@ -12,19 +12,18 @@ module Transmutation
|
|
12
12
|
# @return [Transmutation::Serializer] The serialized object. This will respond to `#as_json` and `#to_json`.
|
13
13
|
def serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: 1)
|
14
14
|
if object.respond_to?(:map) && !object.respond_to?(:to_hash)
|
15
|
-
return object.map
|
16
|
-
serialize(item, namespace:, serializer:, depth:, max_depth:)
|
17
|
-
end
|
15
|
+
return object.map { |item| serialize(item, namespace:, serializer:, depth:, max_depth:) }
|
18
16
|
end
|
19
17
|
|
20
|
-
lookup_serializer(object, namespace:, serializer:)
|
21
|
-
.new(object, depth:, max_depth:)
|
18
|
+
lookup_serializer(object, namespace:, serializer:).new(object, depth:, max_depth:)
|
22
19
|
end
|
23
20
|
|
24
21
|
# Lookup the serializer for the given object.
|
25
22
|
#
|
26
23
|
# This calls {Transmutation::Serialization::Lookup#serializer_for} to find the serializer for the given object.
|
27
24
|
#
|
25
|
+
# This also caches the result for future lookups.
|
26
|
+
#
|
28
27
|
# @param object [Object] The object to lookup the serializer for.
|
29
28
|
# @param namespace [String, Symbol, Module] The namespace to lookup the serializer in.
|
30
29
|
# @param serializer [String, Symbol, Class] The serializer to use.
|
@@ -32,7 +31,16 @@ module Transmutation
|
|
32
31
|
# @return [Class<Transmutation::Serializer>] The serializer for the given object.
|
33
32
|
#
|
34
33
|
def lookup_serializer(object, namespace: nil, serializer: nil)
|
35
|
-
|
34
|
+
Serialization.cache[[self.namespace, object.class, namespace, serializer]] ||=
|
35
|
+
Lookup.new(self, namespace:).serializer_for(object, serializer:)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.cache
|
39
|
+
@cache ||= {}
|
40
|
+
end
|
41
|
+
|
42
|
+
def namespace
|
43
|
+
@namespace ||= self.class.name.split("::")[...-1].join("::")
|
36
44
|
end
|
37
45
|
|
38
46
|
private_class_method def self.included(base)
|
@@ -44,7 +44,7 @@ module Transmutation
|
|
44
44
|
# Define an attribute to be serialized
|
45
45
|
#
|
46
46
|
# @param attribute_name [Symbol] The name of the attribute to serialize
|
47
|
-
# @
|
47
|
+
# @yield [object] The block to call to get the value of the attribute
|
48
48
|
# - The block is called in the context of the serializer instance
|
49
49
|
#
|
50
50
|
# @example
|
@@ -66,30 +66,28 @@ module Transmutation
|
|
66
66
|
# @param association_name [Symbol] The name of the association to serialize
|
67
67
|
# @param namespace [String, Symbol, Module] The namespace to lookup the association's serializer in
|
68
68
|
# @param serializer [String, Symbol, Class] The serializer to use for the association's serialization
|
69
|
+
# @yield [object] The block to call to get the value of the association
|
70
|
+
# - The block is called in the context of the serializer instance
|
71
|
+
# - The return value from the block is automatically serialized
|
69
72
|
#
|
70
73
|
# @example
|
71
74
|
# class UserSerializer < Transmutation::Serializer
|
72
75
|
# association :posts
|
73
76
|
# association :comments, namespace: "Nested", serializer: "User::CommentSerializer"
|
77
|
+
# association :archived_posts do
|
78
|
+
# object.posts.archived
|
79
|
+
# end
|
74
80
|
# end
|
75
|
-
def association(association_name, namespace: nil, serializer: nil)
|
81
|
+
def association(association_name, namespace: nil, serializer: nil, &custom_block)
|
76
82
|
block = lambda do
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
serializer:,
|
81
|
-
depth: @depth + 1,
|
82
|
-
max_depth: @max_depth
|
83
|
-
)
|
83
|
+
association_instance = custom_block ? instance_exec(&custom_block) : object.send(association_name)
|
84
|
+
|
85
|
+
serialize(association_instance, namespace:, serializer:, depth: @depth + 1, max_depth: @max_depth)
|
84
86
|
end
|
85
87
|
|
86
88
|
attributes_config[association_name] = { block:, association: true }
|
87
89
|
end
|
88
90
|
|
89
|
-
alias belongs_to association
|
90
|
-
alias has_one association
|
91
|
-
alias has_many association
|
92
|
-
|
93
91
|
# Shorthand for defining multiple attributes
|
94
92
|
#
|
95
93
|
# @param attribute_names [Array<Symbol>] The names of the attributes to serialize
|
@@ -98,9 +96,9 @@ module Transmutation
|
|
98
96
|
# class UserSerializer < Transmutation::Serializer
|
99
97
|
# attributes :first_name, :last_name
|
100
98
|
# end
|
101
|
-
def attributes(*attribute_names)
|
99
|
+
def attributes(*attribute_names, **, &)
|
102
100
|
attribute_names.each do |attribute_name|
|
103
|
-
attribute(attribute_name)
|
101
|
+
attribute(attribute_name, **, &)
|
104
102
|
end
|
105
103
|
end
|
106
104
|
|
@@ -112,11 +110,15 @@ module Transmutation
|
|
112
110
|
# class UserSerializer < Transmutation::Serializer
|
113
111
|
# associations :posts, :comments
|
114
112
|
# end
|
115
|
-
def associations(*association_names)
|
113
|
+
def associations(*association_names, **, &)
|
116
114
|
association_names.each do |association_name|
|
117
|
-
association(association_name)
|
115
|
+
association(association_name, **, &)
|
118
116
|
end
|
119
117
|
end
|
118
|
+
|
119
|
+
alias belongs_to associations
|
120
|
+
alias has_one associations
|
121
|
+
alias has_many associations
|
120
122
|
end
|
121
123
|
|
122
124
|
private
|
data/lib/transmutation.rb
CHANGED
@@ -42,8 +42,8 @@ module Transmutation
|
|
42
42
|
end
|
43
43
|
|
44
44
|
require "zeitwerk"
|
45
|
-
|
46
|
-
loader.
|
45
|
+
|
46
|
+
loader = Zeitwerk::Loader.for_gem
|
47
47
|
loader.setup
|
48
48
|
|
49
49
|
require "active_support"
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transmutation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nitemaeric
|
8
8
|
- borrabeer
|
9
|
-
autorequire:
|
10
9
|
bindir: exe
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activesupport
|
@@ -39,7 +38,6 @@ dependencies:
|
|
39
38
|
- - "~>"
|
40
39
|
- !ruby/object:Gem::Version
|
41
40
|
version: '2.6'
|
42
|
-
description:
|
43
41
|
email:
|
44
42
|
- daniel@spellbook.tech
|
45
43
|
- worapath.pakkavesa@spellbook.tech
|
@@ -75,7 +73,6 @@ metadata:
|
|
75
73
|
source_code_uri: https://github.com/spellbook-technology/transmutation
|
76
74
|
changelog_uri: https://github.com/spellbook-technology/transmutation/CHANGELOG.md
|
77
75
|
documentation_uri: https://rubydoc.info/gems/transmutation
|
78
|
-
post_install_message:
|
79
76
|
rdoc_options: []
|
80
77
|
require_paths:
|
81
78
|
- lib
|
@@ -90,8 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
87
|
- !ruby/object:Gem::Version
|
91
88
|
version: '0'
|
92
89
|
requirements: []
|
93
|
-
rubygems_version: 3.
|
94
|
-
signing_key:
|
90
|
+
rubygems_version: 3.6.8
|
95
91
|
specification_version: 4
|
96
92
|
summary: Ruby JSON serialization library
|
97
93
|
test_files: []
|