alba 2.4.0 → 2.4.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: 89ce66083a51c7811468f9435288afbc53f81418f21635af4393eaf1ac635d14
4
- data.tar.gz: 7344d77ba35c156a6a18abe6f5ff498b7a8cd24b460c5b3f6bf1f0739bd0c8aa
3
+ metadata.gz: e96e153192419f36d10cdd2377720a4d818bd935e8b4d7364e0272f460f74c30
4
+ data.tar.gz: 2ca78d61d8e01c1655f294f1836c386bb561db3fca45e964bd3c2b0023912b03
5
5
  SHA512:
6
- metadata.gz: 33e534a676d589ba88db064b8ba57bbd10a20d268c417d8ed89fbc9b830f2861e4d3c1b46772d9151fce5dcff909e591f26f4fe1be77c247da4e1ceb4998d1cc
7
- data.tar.gz: 401030ee3033228b11d2b75fa411809e7707b0c8b6791c2fe9da8bf9ce290c290225cb4e23895bc31019cc8cead19ac20335fffb24682c58ebc7f09a8250999a
6
+ metadata.gz: 45c70af557b2fcb5ec1e8e7d2269fa6639e086d506b5b8706361a208c24a436ce61807d5d6466accfc516c4f1e556d23a3535236ed2d9047f3c72491702028bb
7
+ data.tar.gz: 148818585445adde1fa4f992df975400863856e227e12fe706f241523a06901e34d099511d13ae5fff4cbd5a9ea29c659551034ffb188bc1cc3d519c72e2a88e
data/CHANGELOG.md CHANGED
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.4.2] 2023-10-11
10
+
11
+ ### Fixed
12
+
13
+ - Multithread bug
14
+
15
+ ## [2.4.1] 2023-08-02
16
+
17
+ #### Fixed
18
+
19
+ - Fix the bug of resource name inference for classes whose name end with "Serializer" [No PR](https://github.com/okuramasafumi/alba/commit/1695af4351981725231fd071aaef5b2e4174fb26)
20
+
9
21
  ## [2.4.0] 2023-08-02
10
22
 
11
23
  ### Added
@@ -8,7 +8,7 @@ module Alba
8
8
  attr_reader :const_cache
9
9
  end
10
10
 
11
- attr_reader :object, :name
11
+ attr_reader :name
12
12
 
13
13
  # @param name [Symbol, String] name of the method to fetch association
14
14
  # @param condition [Proc, nil] a proc filtering data
@@ -36,16 +36,16 @@ module Alba
36
36
  # @return [Hash]
37
37
  def to_h(target, within: nil, params: {})
38
38
  params = params.merge(@params)
39
- @object = target.__send__(@name)
40
- @object = @condition.call(object, params, target) if @condition
41
- return if @object.nil?
39
+ object = target.__send__(@name)
40
+ object = @condition.call(object, params, target) if @condition
41
+ return if object.nil?
42
42
 
43
43
  if @resource.is_a?(Proc)
44
- return to_h_with_each_resource(within, params) if @object.is_a?(Enumerable)
44
+ return to_h_with_each_resource(object, within, params) if object.is_a?(Enumerable)
45
45
 
46
- @resource.call(@object).new(@object, within: within, params: params).to_h
46
+ @resource.call(object).new(object, within: within, params: params).to_h
47
47
  else
48
- to_h_with_constantize_resource(within, params)
48
+ to_h_with_constantize_resource(object, within, params)
49
49
  end
50
50
  end
51
51
 
@@ -56,6 +56,7 @@ module Alba
56
56
  when Class
57
57
  resource
58
58
  when Symbol, String
59
+ Object.const_get(resource)
59
60
  self.class.const_cache.fetch(resource) do
60
61
  self.class.const_cache[resource] = Object.const_get(resource)
61
62
  end
@@ -76,13 +77,13 @@ module Alba
76
77
  end
77
78
  end
78
79
 
79
- def to_h_with_each_resource(within, params)
80
- @object.map do |item|
80
+ def to_h_with_each_resource(object, within, params)
81
+ object.map do |item|
81
82
  @resource.call(item).new(item, within: within, params: params).to_h
82
83
  end
83
84
  end
84
85
 
85
- def to_h_with_constantize_resource(within, params)
86
+ def to_h_with_constantize_resource(object, within, params)
86
87
  @resource = constantize(@resource)
87
88
  @resource.new(object, params: params, within: within).to_h
88
89
  end
@@ -1,5 +1,6 @@
1
1
  require_relative 'association'
2
2
  require_relative 'constants'
3
+ require 'ostruct'
3
4
 
4
5
  module Alba
5
6
  # Represents attribute with `if` option
@@ -23,7 +24,7 @@ module Alba
23
24
  fetched_attribute = yield(@body)
24
25
  return fetched_attribute unless with_two_arity_proc_condition
25
26
 
26
- return Alba::REMOVE_KEY unless resource.instance_exec(object, attribute_from_association_body_or(fetched_attribute), &@condition)
27
+ return Alba::REMOVE_KEY unless resource.instance_exec(object, objectize(fetched_attribute), &@condition)
27
28
 
28
29
  fetched_attribute
29
30
  end
@@ -48,8 +49,18 @@ module Alba
48
49
  @condition.is_a?(Proc) && @condition.arity >= 2
49
50
  end
50
51
 
51
- def attribute_from_association_body_or(fetched_attribute)
52
- @body.is_a?(Alba::Association) ? @body.object : fetched_attribute
52
+ # OpenStruct is used as a simple solution for converting Hash or Array of Hash into an object
53
+ # Using OpenStruct is not good in general, but in this case there's no other solution
54
+ def objectize(fetched_attribute)
55
+ return fetched_attribute unless @body.is_a?(Alba::Association)
56
+
57
+ if fetched_attribute.is_a?(Array)
58
+ fetched_attribute.map do |hash|
59
+ OpenStruct.new(hash)
60
+ end
61
+ else
62
+ OpenStruct.new(fetched_attribute)
63
+ end
53
64
  end
54
65
  end
55
66
  end
data/lib/alba/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Alba
2
- VERSION = '2.4.0'.freeze
2
+ VERSION = '2.4.2'.freeze
3
3
  end
data/lib/alba.rb CHANGED
@@ -114,7 +114,11 @@ module Alba
114
114
  raise Alba::Error, 'Inference is disabled so Alba cannot infer resource name. Set inflector before use.' unless Alba.inflector
115
115
 
116
116
  const_parent = nesting.nil? ? Object : Object.const_get(nesting)
117
- const_parent.const_get("#{inflector.classify(name)}Resource")
117
+ begin
118
+ const_parent.const_get("#{inflector.classify(name)}Resource")
119
+ rescue # Retry for serializer
120
+ const_parent.const_get("#{inflector.classify(name)}Serializer")
121
+ end
118
122
  end
119
123
 
120
124
  # Configure Alba to symbolize keys
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alba
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - OKURA Masafumi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-01 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Alba is the fastest JSON serializer for Ruby. It focuses on performance,
14
14
  flexibility and usability.