alba 2.4.1 → 2.4.3
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 +12 -0
- data/lib/alba/association.rb +11 -10
- data/lib/alba/conditional_attribute.rb +14 -3
- data/lib/alba/resource.rb +16 -4
- data/lib/alba/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d24d6ad9ef1b576685afb22e3f3c2033555afa358516204b42a31b11080d52b
|
4
|
+
data.tar.gz: 20cc1cb31b6cdfbb73ec87421d828ad274e4863e9156e0d22acb71d99ff0388b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbc694ec856084b9e008a9996b38aa52ae138cfc136c414002cdf375764501fc7ab37a1b72e433f743cddffa394e963e51070014954c273e7b02d8dfa894582a
|
7
|
+
data.tar.gz: 4dc0354d7b318a7f087bdcf4c9d32d5a6a2d956c5aea4783cdefbb587ab4e502b3f6d6e1becda772065978f64b9c463b69f7a41a8e1cab2f00e56ad1305f4036
|
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.3] 2024-10-31
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
|
13
|
+
- Resource method is those explicitly defined only [27996a04598c57943e184ee34224a2cee9bf2e9d]
|
14
|
+
|
15
|
+
## [2.4.2] 2023-10-11
|
16
|
+
|
17
|
+
### Fixed
|
18
|
+
|
19
|
+
- Multithread bug
|
20
|
+
|
9
21
|
## [2.4.1] 2023-08-02
|
10
22
|
|
11
23
|
#### Fixed
|
data/lib/alba/association.rb
CHANGED
@@ -8,7 +8,7 @@ module Alba
|
|
8
8
|
attr_reader :const_cache
|
9
9
|
end
|
10
10
|
|
11
|
-
attr_reader :
|
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
|
-
|
40
|
-
|
41
|
-
return if
|
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
|
44
|
+
return to_h_with_each_resource(object, within, params) if object.is_a?(Enumerable)
|
45
45
|
|
46
|
-
@resource.call(
|
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
|
-
|
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,
|
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
|
-
|
52
|
-
|
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/resource.rb
CHANGED
@@ -11,7 +11,7 @@ module Alba
|
|
11
11
|
module Resource
|
12
12
|
# @!parse include InstanceMethods
|
13
13
|
# @!parse extend ClassMethods
|
14
|
-
DSLS = {_attributes: {}, _key: nil, _key_for_collection: nil, _meta: nil, _transform_type: :none, _transforming_root_key: false, _key_transformation_cascade: true, _on_error: nil, _on_nil: nil, _layout: nil, _collection_key: nil, _helper: nil}.freeze # rubocop:disable Layout/LineLength
|
14
|
+
DSLS = {_attributes: {}, _key: nil, _key_for_collection: nil, _meta: nil, _transform_type: :none, _transforming_root_key: false, _key_transformation_cascade: true, _on_error: nil, _on_nil: nil, _layout: nil, _collection_key: nil, _helper: nil, _resource_methods: []}.freeze # rubocop:disable Layout/LineLength
|
15
15
|
private_constant :DSLS
|
16
16
|
|
17
17
|
WITHIN_DEFAULT = Object.new.freeze
|
@@ -277,9 +277,15 @@ module Alba
|
|
277
277
|
end
|
278
278
|
|
279
279
|
def _fetch_attribute_from_resource_first(obj, attribute)
|
280
|
-
|
281
|
-
|
282
|
-
|
280
|
+
if @_resource_methods.include?(attribute)
|
281
|
+
begin
|
282
|
+
__send__(attribute, obj)
|
283
|
+
rescue NoMethodError
|
284
|
+
obj.__send__(attribute)
|
285
|
+
end
|
286
|
+
else
|
287
|
+
obj.__send__(attribute)
|
288
|
+
end
|
283
289
|
end
|
284
290
|
|
285
291
|
def nil_handler
|
@@ -314,6 +320,12 @@ module Alba
|
|
314
320
|
module ClassMethods
|
315
321
|
attr_reader(*DSLS.keys)
|
316
322
|
|
323
|
+
# This `method_added` is used for defining "resource methods"
|
324
|
+
def method_added(method_name)
|
325
|
+
_resource_methods << method_name.to_sym unless method_name.to_sym == :_setup
|
326
|
+
super
|
327
|
+
end
|
328
|
+
|
317
329
|
# @private
|
318
330
|
def inherited(subclass)
|
319
331
|
super
|
data/lib/alba/version.rb
CHANGED
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.
|
4
|
+
version: 2.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OKURA Masafumi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-10-31 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.
|
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
|
-
rubygems_version: 3.
|
95
|
+
rubygems_version: 3.1.6
|
96
96
|
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Alba is the fastest JSON serializer for Ruby.
|