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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39130469c67108ad173209647cc7462624e53c27a7d4e582c9a41d25cad18b22
4
- data.tar.gz: dbbfaab64d02de0cd2614670b03abbd579e90845ec361f9e2bce7c1a579de788
3
+ metadata.gz: 5d1be90589a50f1187323dfcb572a25a5f50329a71da0550deb133c7529cc4cf
4
+ data.tar.gz: 7dd9f520d65308273d0207c70a7ebe6288417a8afd0b76f299b8e61da0621c45
5
5
  SHA512:
6
- metadata.gz: 9501c5f696fed8cab886f52d4fdfa64a17e6bdd2166c0c6e86fbe2f045500baeb0128b2b72845b47efc2e99a86247196cb009e0e567686a5f67ab39dd16cf931
7
- data.tar.gz: a2740709abf68970709355628e90b6ee7c37b0f590028001693b60aa066e763661cc887c3dc3961166af8dc5bdd53b162473b77c346dbd1c4895fafb2c7e2b69
6
+ metadata.gz: 1682f31fa475e292df1e01d1e2c1eba4d3fb91982aa28e7cecdbf41114f9099faf28ed5deefbc9bb331ba40bfbdec117e6970ff70861df0c9308850e46439a36
7
+ data.tar.gz: 370d427728ab870d602a43bb5dd809f5bc339e3513560259ad05c0aaf3617ea81c301221ec208d966d81312f824bf6b6ac6c0f402ff49bb4ccaec4dd1d6cfecc
data/Gemfile CHANGED
@@ -17,3 +17,6 @@ gem "simplecov-lcov"
17
17
  gem "undercover"
18
18
 
19
19
  gem "pry"
20
+
21
+ gem "gem-release", require: false
22
+ gem "solargraph", require: false
@@ -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 caller_namespace if @namespace.nil?
69
- return @namespace if @namespace.start_with?("::")
68
+ return @caller.namespace if @namespace.nil?
69
+ return @namespace if @namespace.start_with?("::")
70
70
 
71
- "#{caller_namespace}::#{@namespace}"
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 do |item|
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
- Lookup.new(self, namespace:).serializer_for(object, serializer:)
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
- # @param block [Proc] The block to call to get the value of the attribute
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
- serialize(
78
- object.send(association_name),
79
- namespace:,
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Transmutation
4
- VERSION = "0.4.5"
4
+ VERSION = "0.5.1"
5
5
  end
data/lib/transmutation.rb CHANGED
@@ -42,8 +42,8 @@ module Transmutation
42
42
  end
43
43
 
44
44
  require "zeitwerk"
45
- loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
46
- loader.ignore("#{__dir__}/transmutation/core_ext")
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.5
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: 2024-11-24 00:00:00.000000000 Z
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.4.19
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: []