alba 3.7.2 → 3.7.4

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: b9bfe7db8f59dd13c0e93f69e1145d9d4fd6fd399b3ac03af5f3aaa4cab967e4
4
- data.tar.gz: f71ed1a8fc48d3f11db60cb08f1b3d38714732f5082092240485432a0f255c22
3
+ metadata.gz: 19dd24ca7012ad65ea2c69fbd5f7fe9e0fe8931ac2a04320a74292d9d033943d
4
+ data.tar.gz: 95595afe6847cbaa2dde39d5d287d5d6ce82ec28182a3c12153d437dd140147e
5
5
  SHA512:
6
- metadata.gz: bf4d087dfde7c29caa9ffd7316c7bf1e877c5dc84e89495cf50e68bfd4956daa86e60d57f8bd63dea27d4ad1216f29eea572e5ed6b2d388ace3bcf4f79264968
7
- data.tar.gz: 31826991dc9ce74b265bd450de0991e9c93c7903df30da3d1d6542c3f3a931b54abfa74e78c75d59d39fa97a0bef083d070c40b9b8157f28cefaa6bd24e7a3da
6
+ metadata.gz: a0ff2aa000c80343b9bd02d0cbbe8789477aa4adf1a357fac87600c0b7821f992eec1986848ce97b81c678bb5e4ecbadfa50a076eb0a015caa9fbf7a814492db
7
+ data.tar.gz: 676be0a714bf6b5325b0e6412fcce1abac2f1d517b48dde00bdc4d8ca4ce5fa10838d735cede7f893dee8291001ef8331841160a7374a02da267d87859066ff6
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
+ ## 3.7.4 2025-07-24
10
+
11
+ ### Fixed
12
+
13
+ - Fix trait instance method access [#452](https://github.com/okuramasafumi/alba/pull/452)
14
+ - Thank you, @ether-moon
15
+
16
+ ## [3.7.3] 2025-06-28
17
+
18
+ ### Fixed
19
+
20
+ - Enable to apply traits for associations [Issue #444](https://github.com/okuramasafumi/alba/issues/444)
21
+ - Thank you for reporting, @rainerborene
22
+
9
23
  ## [3.7.2] 2025-06-08
10
24
 
11
25
  ### Fixed
data/README.md CHANGED
@@ -432,6 +432,8 @@ UserResource.new(user).serialize
432
432
  # => '{"id":1,"articles":[{"title":"Hello World!"},{"title":"Super nice"}]}'
433
433
  ```
434
434
 
435
+ #### Inline associations
436
+
435
437
  You can define associations inline if you don't need a class for association.
436
438
 
437
439
  ```ruby
@@ -461,6 +463,8 @@ class AnotherUserResource
461
463
  end
462
464
  ```
463
465
 
466
+ #### Filtering associations
467
+
464
468
  You can "filter" association using second proc argument. This proc takes association object, `params` and initial object.
465
469
 
466
470
  This feature is useful when you want to modify association, such as adding `includes` or `order` to ActiveRecord relations.
@@ -519,6 +523,8 @@ UserResource.new(user, params: {filter: :even?}).serialize
519
523
  # => '{"id":1,"articles":[{"title":"Super nice"}]}'
520
524
  ```
521
525
 
526
+ #### Changing a key
527
+
522
528
  You can change a key for association with `key` option.
523
529
 
524
530
  ```ruby
@@ -535,6 +541,8 @@ UserResource.new(user).serialize
535
541
  # => '{"id":1,"my_articles":[{"title":"Hello World!"}]}'
536
542
  ```
537
543
 
544
+ #### Determining a resource for the association
545
+
538
546
  You can omit the resource option if you enable Alba's [inference](#inference-configuration) feature.
539
547
 
540
548
  ```ruby
@@ -16,15 +16,17 @@ module Alba
16
16
  # @param condition [Proc, nil] a proc filtering data
17
17
  # @param resource [Class<Alba::Resource>, Proc, String, Symbol, nil]
18
18
  # a resource class for the association, a proc returning a resource class or a name of the resource
19
+ # @param with_traits [Symbol, Array<Symbol>, nil] specified traits
19
20
  # @param params [Hash] params override for the association
20
21
  # @param nesting [String] a namespace where source class is inferred with
21
22
  # @param key_transformation [Symbol] key transformation type
22
23
  # @param helper [Module] helper module to include
23
24
  # @param block [Block] used to define resource when resource arg is absent
24
- def initialize(name:, condition: nil, resource: nil, params: {}, nesting: nil, key_transformation: :none, helper: nil, &block)
25
+ def initialize(name:, condition: nil, resource: nil, with_traits: nil, params: {}, nesting: nil, key_transformation: :none, helper: nil, &block)
25
26
  @name = name
26
27
  @condition = condition
27
28
  @resource = resource
29
+ @with_traits = with_traits
28
30
  @params = params
29
31
  return if @resource
30
32
 
@@ -53,7 +55,7 @@ module Alba
53
55
  if @resource.is_a?(Proc)
54
56
  return to_h_with_each_resource(object, within, params) if object.is_a?(Enumerable)
55
57
 
56
- @resource.call(object).new(object, within: within, params: params).to_h
58
+ @resource.call(object).new(object, within: within, params: params, with_traits: @with_traits).to_h
57
59
  else
58
60
  to_h_with_constantize_resource(object, within, params)
59
61
  end
@@ -100,13 +102,13 @@ module Alba
100
102
 
101
103
  def to_h_with_each_resource(object, within, params)
102
104
  object.map do |item|
103
- @resource.call(item).new(item, within: within, params: params).to_h
105
+ @resource.call(item).new(item, within: within, params: params, with_traits: @with_traits).to_h
104
106
  end
105
107
  end
106
108
 
107
109
  def to_h_with_constantize_resource(object, within, params)
108
110
  @resource = constantize(@resource)
109
- @resource.new(object, params: params, within: within).to_h
111
+ @resource.new(object, params: params, within: within, with_traits: @with_traits).to_h
110
112
  end
111
113
  end
112
114
  end
@@ -23,7 +23,7 @@ module Alba
23
23
  return Alba::REMOVE_KEY unless condition_passes?(resource, object)
24
24
 
25
25
  fetched_attribute = yield(@body)
26
- return fetched_attribute unless with_two_arity_proc_condition
26
+ return fetched_attribute unless with_two_arity_proc_condition?
27
27
 
28
28
  return Alba::REMOVE_KEY unless resource.instance_exec(object, second_object(object), &@condition)
29
29
 
@@ -46,7 +46,7 @@ module Alba
46
46
  end
47
47
  end
48
48
 
49
- def with_two_arity_proc_condition
49
+ def with_two_arity_proc_condition?
50
50
  @condition.is_a?(Proc) && @condition.arity >= 2
51
51
  end
52
52
 
data/lib/alba/resource.rb CHANGED
@@ -113,18 +113,15 @@ module Alba
113
113
  private
114
114
 
115
115
  def hash_from_traits(obj)
116
- h = {}
117
- return h if @with_traits.nil?
116
+ return {} if @with_traits.nil?
118
117
 
119
- Array(@with_traits).each do |trait|
118
+ Array(@with_traits).each_with_object({}) do |trait, hash|
120
119
  body = @_traits.fetch(trait) { raise Alba::Error, "Trait not found: #{trait}" }
121
-
122
- resource_class = Alba.resource_class
120
+ resource_class = Class.new(self.class)
123
121
  resource_class.class_eval(&body)
124
122
  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)
123
+ hash.merge!(resource_class.new(obj, params: params, within: @within, select: method(:select)).serializable_hash)
126
124
  end
127
- h
128
125
  end
129
126
 
130
127
  def deprecated_serializable_hash
@@ -273,7 +270,7 @@ module Alba
273
270
  when :ignore then nil
274
271
  when Proc
275
272
  key, value = on_error.call(error, obj, key, attribute, self.class)
276
- hash[key] = value
273
+ hash[key] = value unless Alba::REMOVE_KEY == key # rubocop:disable Style/YodaCondition
277
274
  else
278
275
  # :nocov:
279
276
  raise Alba::Error, 'Impossible path'
@@ -433,17 +430,20 @@ module Alba
433
430
  # @param resource [Class<Alba::Resource>, String, Proc, nil] representing resource for this association
434
431
  # @param serializer [Class<Alba::Resource>, String, Proc, nil] alias for `resource`
435
432
  # @param key [String, Symbol, nil] used as key when given
433
+ # @param with_traits [Symbol, Array<Symbol>, nil] specified traits
436
434
  # @param params [Hash] params override for the association
437
435
  # @param options [Hash<Symbol, Proc>]
438
436
  # @option options [Proc] if a condition to decide if this association should be serialized
439
437
  # @param block [Block]
440
438
  # @return [void]
441
439
  # @see Alba::Association#initialize
442
- def association(name, condition = nil, resource: nil, serializer: nil, key: nil, params: {}, **options, &block)
440
+ def association(name, condition = nil, resource: nil, serializer: nil, key: nil, with_traits: nil, params: {}, **options, &block)
443
441
  resource ||= serializer
444
442
  transformation = @_key_transformation_cascade ? @_transform_type : :none
445
443
  assoc = Association.new(
446
- name: name, condition: condition, resource: resource, params: params, nesting: nesting, key_transformation: transformation, helper: @_helper, &block
444
+ name: name, condition: condition, resource: resource, with_traits: with_traits,
445
+ params: params, nesting: nesting, key_transformation: transformation, helper: @_helper,
446
+ &block
447
447
  )
448
448
  @_attributes[key&.to_sym || name.to_sym] = options[:if] ? ConditionalAttribute.new(body: assoc, condition: options[:if]) : assoc
449
449
  end
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.2'
4
+ VERSION = '3.7.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alba
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.2
4
+ version: 3.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - OKURA Masafumi