alba 3.7.0 → 3.7.2

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: 14523f0f31f5993205b02095c3e6a8115ef83728961cf720dde4d9599be3168a
4
- data.tar.gz: 9a2de9344c9b7bf95daa07e0d61339a08285f0cdadf9b694b58f73e7b0773970
3
+ metadata.gz: b9bfe7db8f59dd13c0e93f69e1145d9d4fd6fd399b3ac03af5f3aaa4cab967e4
4
+ data.tar.gz: f71ed1a8fc48d3f11db60cb08f1b3d38714732f5082092240485432a0f255c22
5
5
  SHA512:
6
- metadata.gz: e7f8062cca1bbccbe4d4e871fd5ad2ab42a5b25acc0d2b9edd3e8247af021edaca981988eeb8428675c8bdc8bcbc14fa5ee45d90d524ca76987ab469fc2a6203
7
- data.tar.gz: ea0cd1598c2ef8f76e740309ff925b8c26ef60bef6174755b23549d3fd9a98aefeab26bb6d47b73c92c9b92d84125717b157369f25ba2d3631532bec522a9765
6
+ metadata.gz: bf4d087dfde7c29caa9ffd7316c7bf1e877c5dc84e89495cf50e68bfd4956daa86e60d57f8bd63dea27d4ad1216f29eea572e5ed6b2d388ace3bcf4f79264968
7
+ data.tar.gz: 31826991dc9ce74b265bd450de0991e9c93c7903df30da3d1d6542c3f3a931b54abfa74e78c75d59d39fa97a0bef083d070c40b9b8157f28cefaa6bd24e7a3da
data/CHANGELOG.md CHANGED
@@ -6,6 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.7.2] 2025-06-08
10
+
11
+ ### Fixed
12
+
13
+ - apply transform_keys directive for traits [#438](https://github.com/okuramasafumi/alba/pull/436)
14
+ - Thank you, @elShiaLabeouf
15
+
16
+ ## [3.7.1] 2025-05-27
17
+
18
+ ### Fixed
19
+
20
+ - `select` now works with `trait` and `nested_attribute` [#436](https://github.com/okuramasafumi/alba/pull/436)
21
+
9
22
  ## [3.7.0] 2025-05-08
10
23
 
11
24
  ### Added
@@ -47,8 +47,7 @@ module Alba
47
47
  # @return [Hash]
48
48
  def to_h(target, within: nil, params: {})
49
49
  params = params.merge(@params)
50
- object = target.is_a?(Hash) ? target.fetch(@name) : target.__send__(@name)
51
- object = @condition.call(object, params, target) if @condition
50
+ object = object_from(target, params)
52
51
  return if object.nil?
53
52
 
54
53
  if @resource.is_a?(Proc)
@@ -62,6 +61,12 @@ module Alba
62
61
 
63
62
  private
64
63
 
64
+ def object_from(target, params)
65
+ o = target.is_a?(Hash) ? target.fetch(@name) : target.__send__(@name)
66
+ o = @condition.call(o, params, target) if @condition
67
+ o
68
+ end
69
+
65
70
  def constantize(resource)
66
71
  case resource
67
72
  when Class
@@ -17,12 +17,13 @@ module Alba
17
17
  # @param object [Object] the object being serialized
18
18
  # @param params [Hash] params Hash inherited from Resource
19
19
  # @param within [Object, nil, false, true] determines what associations to be serialized. If not set, it serializes all associations.
20
+ # @param select [Method] select method object from its origin
20
21
  # @return [Hash] hash serialized from running the class body in the object
21
- def value(object:, params:, within:)
22
+ def value(object:, params:, within:, select: nil)
22
23
  resource_class = Alba.resource_class
23
24
  resource_class.transform_keys(@key_transformation)
24
25
  resource_class.class_eval(&@block)
25
- resource_class.new(object, params: params, within: within).serializable_hash
26
+ resource_class.new(object, params: params, within: within, select: select).serializable_hash
26
27
  end
27
28
  end
28
29
  end
data/lib/alba/resource.rb CHANGED
@@ -47,11 +47,15 @@ module Alba
47
47
  # @param params [Hash] user-given Hash for arbitrary data
48
48
  # @param within [Object, nil, false, true] determines what associations to be serialized. If not set, it serializes all associations.
49
49
  # @param with_traits [Symbol, Array<Symbol>, nil] specified traits
50
- def initialize(object, params: {}, within: WITHIN_DEFAULT, with_traits: nil)
50
+ # @param select [Method] select method object used with `nested_attribute` and `trait`
51
+ def initialize(object, params: {}, within: WITHIN_DEFAULT, with_traits: nil, select: nil)
51
52
  @object = object
52
53
  @params = params
53
54
  @within = within
54
55
  @with_traits = with_traits
56
+ # select override to share the same method with `trait` and `nested_attribute`
57
+ # Trait and NestedAttribute generates anonymous class so it checks if it's anonymous class to prevent accidental overriding
58
+ self.class.define_method(:select, &select) if select && self.class.name.nil?
55
59
  _setup
56
60
  end
57
61
 
@@ -117,7 +121,8 @@ module Alba
117
121
 
118
122
  resource_class = Alba.resource_class
119
123
  resource_class.class_eval(&body)
120
- h.merge!(resource_class.new(obj, params: params, within: @within).serializable_hash)
124
+ resource_class.transform_keys(@_transform_type) unless @_transform_type == :none
125
+ h.merge!(resource_class.new(obj, params: params, within: @within, select: method(:select)).serializable_hash)
121
126
  end
122
127
  h
123
128
  end
@@ -249,16 +254,18 @@ module Alba
249
254
  key = transform_key(key)
250
255
  value = fetch_attribute(obj, key, attribute)
251
256
  # When `select` is not overridden, skip calling it for better performance
252
- unless @_select_arity.nil?
253
- # `select` can be overridden with both 2 and 3 parameters
254
- # Here we check the arity and build arguments accordingly
255
- args = @_select_arity == 3 ? [key, value, attribute] : [key, value]
256
- return unless select(*args)
257
- end
257
+ return if !@_select_arity.nil? && !do_select(key, value, attribute)
258
258
 
259
259
  hash[key] = value unless Alba::REMOVE_KEY == value # rubocop:disable Style/YodaCondition
260
260
  end
261
261
 
262
+ def do_select(key, value, attribute)
263
+ # `select` can be overridden with both 2 and 3 parameters
264
+ # Here we check the arity and build arguments accordingly
265
+ args = @_select_arity == 3 ? [key, value, attribute] : [key, value]
266
+ select(*args)
267
+ end
268
+
262
269
  def handle_error(error, obj, key, attribute, hash)
263
270
  case (on_error = @_on_error || :raise)
264
271
  when :raise, nil then raise(error)
@@ -286,7 +293,7 @@ module Alba
286
293
  when Proc then instance_exec(obj, &attribute)
287
294
  when Alba::Association then yield_if_within(attribute.name.to_sym) { |within| attribute.to_h(obj, params: params, within: within) }
288
295
  when TypedAttribute then attribute.value { |attr| fetch_attribute(obj, key, attr) }
289
- when NestedAttribute then attribute.value(object: obj, params: params, within: @within)
296
+ when NestedAttribute then attribute.value(object: obj, params: params, within: @within, select: method(:select))
290
297
  when ConditionalAttribute then attribute.with_passing_condition(resource: self, object: obj) { |attr| fetch_attribute(obj, key, attr) }
291
298
  # :nocov:
292
299
  else raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
data/lib/alba/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Alba
4
- VERSION = '3.7.0'
4
+ VERSION = '3.7.2'
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alba
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.0
4
+ version: 3.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - OKURA Masafumi
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-05-08 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Alba is the fastest JSON serializer for Ruby. It focuses on performance,
13
13
  flexibility and usability.