alba 3.7.0 → 3.7.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: 14523f0f31f5993205b02095c3e6a8115ef83728961cf720dde4d9599be3168a
4
- data.tar.gz: 9a2de9344c9b7bf95daa07e0d61339a08285f0cdadf9b694b58f73e7b0773970
3
+ metadata.gz: 9179eabfb7cd37ad4403470b3c83229f7dc558085d8946d84302473f8076694c
4
+ data.tar.gz: cb7092762e4881c13171447cc2dc4a6ec2ceb077bd6527a154d5dac6d3369967
5
5
  SHA512:
6
- metadata.gz: e7f8062cca1bbccbe4d4e871fd5ad2ab42a5b25acc0d2b9edd3e8247af021edaca981988eeb8428675c8bdc8bcbc14fa5ee45d90d524ca76987ab469fc2a6203
7
- data.tar.gz: ea0cd1598c2ef8f76e740309ff925b8c26ef60bef6174755b23549d3fd9a98aefeab26bb6d47b73c92c9b92d84125717b157369f25ba2d3631532bec522a9765
6
+ metadata.gz: 8baf63e1b739731f221c6a76bb452d730c703fe848853f26f91219357001832599196902e98f2266fa04631873e3bd791dca46c7898c3f16fc2909acf1a74a10
7
+ data.tar.gz: '0872f1ca1d1a263f8d7dff94ed1e965d88e9ec423213d0b631c3bf155ddaabc5905da4d3b74344950ede8a238761777100abb42749e378beba55f3796cf85dda'
data/CHANGELOG.md CHANGED
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.7.1] 2025-05-27
10
+
11
+ ### Fixed
12
+
13
+ - `select` now works with `trait` and `nested_attribute` [#436](https://github.com/okuramasafumi/alba/pull/436)
14
+
9
15
  ## [3.7.0] 2025-05-08
10
16
 
11
17
  ### 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,7 @@ 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
+ h.merge!(resource_class.new(obj, params: params, within: @within, select: method(:select)).serializable_hash)
121
125
  end
122
126
  h
123
127
  end
@@ -249,16 +253,18 @@ module Alba
249
253
  key = transform_key(key)
250
254
  value = fetch_attribute(obj, key, attribute)
251
255
  # 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
256
+ return if !@_select_arity.nil? && !do_select(key, value, attribute)
258
257
 
259
258
  hash[key] = value unless Alba::REMOVE_KEY == value # rubocop:disable Style/YodaCondition
260
259
  end
261
260
 
261
+ def do_select(key, value, attribute)
262
+ # `select` can be overridden with both 2 and 3 parameters
263
+ # Here we check the arity and build arguments accordingly
264
+ args = @_select_arity == 3 ? [key, value, attribute] : [key, value]
265
+ select(*args)
266
+ end
267
+
262
268
  def handle_error(error, obj, key, attribute, hash)
263
269
  case (on_error = @_on_error || :raise)
264
270
  when :raise, nil then raise(error)
@@ -286,7 +292,7 @@ module Alba
286
292
  when Proc then instance_exec(obj, &attribute)
287
293
  when Alba::Association then yield_if_within(attribute.name.to_sym) { |within| attribute.to_h(obj, params: params, within: within) }
288
294
  when TypedAttribute then attribute.value { |attr| fetch_attribute(obj, key, attr) }
289
- when NestedAttribute then attribute.value(object: obj, params: params, within: @within)
295
+ when NestedAttribute then attribute.value(object: obj, params: params, within: @within, select: method(:select))
290
296
  when ConditionalAttribute then attribute.with_passing_condition(resource: self, object: obj) { |attr| fetch_attribute(obj, key, attr) }
291
297
  # :nocov:
292
298
  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.1'
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.1
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.