transmutation 0.5.0 → 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: bfdba62ded204b2a1007b3f4194ada3d25cfdb1851d21a23d3d0d8baa4b69c78
4
- data.tar.gz: 97c1a41fb6a6550adce185f402a9d9db738cdc912bbd15a7ab13f133fa1a83e8
3
+ metadata.gz: 5d1be90589a50f1187323dfcb572a25a5f50329a71da0550deb133c7529cc4cf
4
+ data.tar.gz: 7dd9f520d65308273d0207c70a7ebe6288417a8afd0b76f299b8e61da0621c45
5
5
  SHA512:
6
- metadata.gz: db4415229791b74647ffd4441d4b930f3ab98a2a7c9e1fb84de94537f6eec1a370116745860fe1bf2a98bb3b8dc6541d7c6ac5933779a022e97e96bbe0113ebc
7
- data.tar.gz: ea012d8cc51843103d938a1953619f33bc603a0a51b70787cbaebcc653bae9e83eded5a3807407ad143ffd50300f063ae4b15d254c03c079674a64701a9781fd
6
+ metadata.gz: 1682f31fa475e292df1e01d1e2c1eba4d3fb91982aa28e7cecdbf41114f9099faf28ed5deefbc9bb331ba40bfbdec117e6970ff70861df0c9308850e46439a36
7
+ data.tar.gz: 370d427728ab870d602a43bb5dd809f5bc339e3513560259ad05c0aaf3617ea81c301221ec208d966d81312f824bf6b6ac6c0f402ff49bb4ccaec4dd1d6cfecc
data/Gemfile CHANGED
@@ -19,3 +19,4 @@ gem "undercover"
19
19
  gem "pry"
20
20
 
21
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)
@@ -88,10 +88,6 @@ module Transmutation
88
88
  attributes_config[association_name] = { block:, association: true }
89
89
  end
90
90
 
91
- alias belongs_to association
92
- alias has_one association
93
- alias has_many association
94
-
95
91
  # Shorthand for defining multiple attributes
96
92
  #
97
93
  # @param attribute_names [Array<Symbol>] The names of the attributes to serialize
@@ -100,9 +96,9 @@ module Transmutation
100
96
  # class UserSerializer < Transmutation::Serializer
101
97
  # attributes :first_name, :last_name
102
98
  # end
103
- def attributes(*attribute_names)
99
+ def attributes(*attribute_names, **, &)
104
100
  attribute_names.each do |attribute_name|
105
- attribute(attribute_name)
101
+ attribute(attribute_name, **, &)
106
102
  end
107
103
  end
108
104
 
@@ -114,11 +110,15 @@ module Transmutation
114
110
  # class UserSerializer < Transmutation::Serializer
115
111
  # associations :posts, :comments
116
112
  # end
117
- def associations(*association_names)
113
+ def associations(*association_names, **, &)
118
114
  association_names.each do |association_name|
119
- association(association_name)
115
+ association(association_name, **, &)
120
116
  end
121
117
  end
118
+
119
+ alias belongs_to associations
120
+ alias has_one associations
121
+ alias has_many associations
122
122
  end
123
123
 
124
124
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Transmutation
4
- VERSION = "0.5.0"
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.5.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: 2025-04-11 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: []