alba 3.11.0 → 4.0.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 +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +0 -10
- data/lib/alba/resource.rb +4 -53
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +0 -34
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0ec3b28fdf4ed9303e346f6dca7771480d8b9bba19dea65374b41a526e895c10
|
|
4
|
+
data.tar.gz: 1ca8398f44a505fbdfc7bc5df7b743de07e4dc6bcd7580bdff3dec0732278474
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0dd664193cc78874ac9e5e2630eaf201d172ce170137084041b65e8e683d95f435c4609df296b2f8b47b588219576e9e743473d83c0f7854a9e6ec6a25c0cce0
|
|
7
|
+
data.tar.gz: 3fc3dc98d059878f5f97e40c81999f0990ee96bb82848acc63cc5c0b49c4240bed2c1b17a077ac46d16754c1e603c6839617e1afa1e1185371885bdc9589955b
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## 4.0.0 2026-08-28
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- Call #nil? on attribute values only when a nil handler is set [#546](https://github.com/okuramasafumi/alba/pull/546)
|
|
14
|
+
|
|
15
|
+
### Removed
|
|
16
|
+
|
|
17
|
+
- Remove the deprecated inference methods `Alba.enable_inference!`, `Alba.disable_inference!`, and `Alba.inferring`; use `Alba.inflector` and `Alba.inflector=` instead
|
|
18
|
+
- Remove the deprecated `Alba.resource_with`; use `Alba.resource_for` instead
|
|
19
|
+
- Remove deprecated support for overriding `Alba::Resource#attributes`; override `Alba::Resource#select` to filter attributes instead
|
|
20
|
+
- Remove the deprecated `converter` and `collection_converter` serialization hooks
|
|
21
|
+
- Remove the deprecated `select` keyword argument from `Alba::Resource#initialize`; the `Alba::Resource#select` filtering method remains available
|
|
22
|
+
|
|
9
23
|
## 3.11.0 2026-07-25
|
|
10
24
|
|
|
11
25
|
### Added
|
data/README.md
CHANGED
|
@@ -12,16 +12,6 @@
|
|
|
12
12
|
|
|
13
13
|
Alba is a JSON serializer for Ruby, JRuby, and TruffleRuby.
|
|
14
14
|
|
|
15
|
-
## IMPORTANT NOTICE
|
|
16
|
-
|
|
17
|
-
Both version `3.0.0` and `2.4.2` contain important bug fix.
|
|
18
|
-
~~However, version `3.0.0` has some bugs (see https://github.com/okuramasafumi/alba/issues/342).
|
|
19
|
-
Until they get fixed, it's highly recommended to upgrade to version `2.4.2`.
|
|
20
|
-
Dependabot and similar tools might create an automated Pull Request to upgrade to `3.0.0`, so it might be required to upgrade to `2.4.2` manually.~~
|
|
21
|
-
Version `3.0.1` has been released so Ruby 3 users should upgrade to `3.0.1`.
|
|
22
|
-
For Ruby 2 users, it's highly recommended to upgrade to `2.4.2`.
|
|
23
|
-
Sorry for the inconvenience.
|
|
24
|
-
|
|
25
15
|
## TL;DR
|
|
26
16
|
|
|
27
17
|
Alba allows you to do something like below.
|
data/lib/alba/resource.rb
CHANGED
|
@@ -51,13 +51,11 @@ module Alba
|
|
|
51
51
|
# @param within [Alba::WITHIN_DEFAULT, Hash, Array, nil, false, true]
|
|
52
52
|
# determines what associations to be serialized. If not set, it serializes all associations.
|
|
53
53
|
# @param with_traits [Symbol, Array<Symbol>, nil] specified traits
|
|
54
|
-
|
|
55
|
-
def initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil)
|
|
54
|
+
def initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil)
|
|
56
55
|
@object = object
|
|
57
56
|
@params = params
|
|
58
57
|
@within = within
|
|
59
58
|
@with_traits = with_traits
|
|
60
|
-
warn '`select` keyword for Alba::Resource is deprecated', category: :deprecated, uplevel: 1 if select
|
|
61
59
|
_setup
|
|
62
60
|
end
|
|
63
61
|
|
|
@@ -127,10 +125,6 @@ module Alba
|
|
|
127
125
|
end
|
|
128
126
|
end
|
|
129
127
|
|
|
130
|
-
def deprecated_serializable_hash
|
|
131
|
-
Alba.collection?(@object) ? serializable_hash_for_collection : converter.call(@object)
|
|
132
|
-
end
|
|
133
|
-
|
|
134
128
|
def serialize_with(hash)
|
|
135
129
|
serialized_json = encode(hash)
|
|
136
130
|
return serialized_json unless @_layout
|
|
@@ -167,18 +161,6 @@ module Alba
|
|
|
167
161
|
end
|
|
168
162
|
end
|
|
169
163
|
|
|
170
|
-
def deprecated_serializable_hash_for_collection
|
|
171
|
-
if @_collection_key
|
|
172
|
-
@object.to_h do |item|
|
|
173
|
-
k = item.public_send(@_collection_key)
|
|
174
|
-
key = Alba.regularize_key(k)
|
|
175
|
-
[key, converter.call(item)]
|
|
176
|
-
end
|
|
177
|
-
else
|
|
178
|
-
@object.each_with_object([], &collection_converter)
|
|
179
|
-
end
|
|
180
|
-
end
|
|
181
|
-
|
|
182
164
|
# @return [String]
|
|
183
165
|
def fetch_key
|
|
184
166
|
k = Alba.collection?(@object) ? _key_for_collection : _key
|
|
@@ -210,23 +192,8 @@ module Alba
|
|
|
210
192
|
@_transforming_root_key
|
|
211
193
|
end
|
|
212
194
|
|
|
213
|
-
def converter
|
|
214
|
-
lambda do |obj|
|
|
215
|
-
attributes_to_hash(obj, {})
|
|
216
|
-
end
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
def collection_converter
|
|
220
|
-
lambda do |obj, a|
|
|
221
|
-
a << {}
|
|
222
|
-
h = a.last
|
|
223
|
-
attributes_to_hash(obj, h)
|
|
224
|
-
a
|
|
225
|
-
end
|
|
226
|
-
end
|
|
227
|
-
|
|
228
195
|
def attributes_to_hash(obj, hash)
|
|
229
|
-
|
|
196
|
+
@_attributes.each do |key, attribute|
|
|
230
197
|
set_key_and_attribute_body_from(obj, key, attribute, hash)
|
|
231
198
|
rescue ::Alba::Error, FrozenError, TypeError
|
|
232
199
|
raise
|
|
@@ -236,14 +203,6 @@ module Alba
|
|
|
236
203
|
@with_traits.nil? ? hash : hash.merge!(hash_from_traits(obj))
|
|
237
204
|
end
|
|
238
205
|
|
|
239
|
-
# This is default behavior for getting attributes for serialization
|
|
240
|
-
# Override this method to filter certain attributes
|
|
241
|
-
#
|
|
242
|
-
# @deprecated in favor of `select`
|
|
243
|
-
def attributes
|
|
244
|
-
@_attributes
|
|
245
|
-
end
|
|
246
|
-
|
|
247
206
|
# Default implementation for selecting attributes
|
|
248
207
|
# Override this method to filter attributes based on key and value
|
|
249
208
|
def select(_key, _value, _attribute)
|
|
@@ -299,7 +258,7 @@ module Alba
|
|
|
299
258
|
else raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
|
|
300
259
|
# :nocov:
|
|
301
260
|
end
|
|
302
|
-
value.nil?
|
|
261
|
+
nil_handler && value.nil? ? instance_exec(obj, key, attribute, &nil_handler) : value
|
|
303
262
|
end
|
|
304
263
|
|
|
305
264
|
def fetch_attribute_from_object_and_resource(obj, attribute)
|
|
@@ -353,16 +312,8 @@ module Alba
|
|
|
353
312
|
attr_reader(*INTERNAL_VARIABLES.keys)
|
|
354
313
|
|
|
355
314
|
# This `method_added` is used for defining "resource methods"
|
|
356
|
-
def method_added(method_name)
|
|
315
|
+
def method_added(method_name)
|
|
357
316
|
case method_name
|
|
358
|
-
when :collection_converter, :converter
|
|
359
|
-
warn "Defining ##{method_name} methods is deprecated", category: :deprecated, uplevel: 1
|
|
360
|
-
alias_method :serializable_hash_for_collection, :deprecated_serializable_hash_for_collection
|
|
361
|
-
private(:serializable_hash_for_collection)
|
|
362
|
-
alias_method :serializable_hash, :deprecated_serializable_hash
|
|
363
|
-
alias_method :to_h, :deprecated_serializable_hash
|
|
364
|
-
when :attributes
|
|
365
|
-
warn 'Overriding `attributes` is deprecated, use `select` instead.', category: :deprecated, uplevel: 1
|
|
366
317
|
when :select
|
|
367
318
|
@_select_arity = instance_method(:select).arity
|
|
368
319
|
when :_setup # noop
|
data/lib/alba/version.rb
CHANGED
data/lib/alba.rb
CHANGED
|
@@ -97,34 +97,6 @@ module Alba
|
|
|
97
97
|
object.is_a?(Enumerable) && @non_collection_types.none? { |type| object.is_a?(type) }
|
|
98
98
|
end
|
|
99
99
|
|
|
100
|
-
# Enable inference for key and resource name
|
|
101
|
-
#
|
|
102
|
-
# @param with [Symbol, Class, Module] inflector
|
|
103
|
-
# When it's a Symbol, it sets inflector with given name
|
|
104
|
-
# When it's a Class or a Module, it sets given object to inflector
|
|
105
|
-
# @deprecated Use {.inflector=} instead
|
|
106
|
-
def enable_inference!(with:)
|
|
107
|
-
Alba::Deprecation.warn('Alba.enable_inference! is deprecated. Use `Alba.inflector=` instead.')
|
|
108
|
-
@inflector = inflector_from(with)
|
|
109
|
-
@inferring = true
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
# Disable inference for key and resource name
|
|
113
|
-
#
|
|
114
|
-
# @deprecated Use {.inflector=} instead
|
|
115
|
-
def disable_inference!
|
|
116
|
-
Alba::Deprecation.warn('Alba.disable_inference! is deprecated. Use `Alba.inflector = nil` instead.')
|
|
117
|
-
@inferring = false
|
|
118
|
-
@inflector = nil
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
# @deprecated Use {.inflector} instead
|
|
122
|
-
# @return [Boolean] whether inference is enabled or not
|
|
123
|
-
def inferring
|
|
124
|
-
Alba::Deprecation.warn('Alba.inferring is deprecated. Use `Alba.inflector` instead.')
|
|
125
|
-
@inferring
|
|
126
|
-
end
|
|
127
|
-
|
|
128
100
|
# Set an inflector
|
|
129
101
|
#
|
|
130
102
|
# @param inflector [Symbol, Class, Module] inflector
|
|
@@ -240,12 +212,6 @@ module Alba
|
|
|
240
212
|
register_default_types
|
|
241
213
|
end
|
|
242
214
|
|
|
243
|
-
# @deprecated Use resource_for instead
|
|
244
|
-
def resource_with(object, with: :inference, &block)
|
|
245
|
-
Kernel.warn('Alba.resource_with is deprecated. Use `Alba.resource_for` instead.')
|
|
246
|
-
_resource_for(object, with: with, &block)
|
|
247
|
-
end
|
|
248
|
-
|
|
249
215
|
# Get a resource object from arguments
|
|
250
216
|
# If block is given, it creates a resource class with the block
|
|
251
217
|
# Otherwise, it behaves depending on `with` argument
|