alba 2.0.0 → 2.0.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 +4 -4
- data/CHANGELOG.md +18 -7
- data/Gemfile +1 -1
- data/lib/alba/association.rb +6 -5
- data/lib/alba/conditional_attribute.rb +6 -6
- data/lib/alba/constants.rb +5 -0
- data/lib/alba/nested_attribute.rb +2 -1
- data/lib/alba/resource.rb +3 -2
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9018588ddacac0cf7db8dac1d23c8165665c7a93976b581d25ab51357ead7834
|
4
|
+
data.tar.gz: 6bde71c65d8fb6965d46cea2769cd9a97d4f76407136e47de4392cdfa1a58333
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7d907446dad8ff8a19814cbf27ba74bfc84a0f00fae8366f561efd5de03e1ac16d66f3b31641e8ec83b5081e6e5fc61074c5a5e47509343fade989ab65e41da
|
7
|
+
data.tar.gz: d81e0055f6899698d70ab1522e71adc8ce9e22e3a8855ad2a3db8b54e551d2ea459a77bc92242bc5c7f8121d150c322f3b99f6d286fd7118f5df2f94d0dc3fdc
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [2.0.1] 2022-11-02
|
10
|
+
|
11
|
+
### Fix
|
12
|
+
|
13
|
+
- the bug including key not in `within` [#262](https://github.com/okuramasafumi/alba/pull/262)
|
14
|
+
- key transformation now cascades multiple levels [#263](https://github.com/okuramasafumi/alba/pull/263)
|
15
|
+
|
9
16
|
## [2.0.0] 2022-10-21
|
10
17
|
|
11
18
|
### Breaking changes
|
@@ -13,16 +20,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
13
20
|
- All Hash-related methods now return String key instead of Symbol key
|
14
21
|
This affects all users, but you can use `deep_symbolize_keys` in Rails environment if you prefer Symbol keys
|
15
22
|
Some DSLs that take key argument such as `on_nil` and `on_error`, are also affected
|
23
|
+
- Remove deprecated methods: `Resource#to_hash`, `Resource.ignoring`, `Alba.on_nil`, `Alba.on_error`, `Alba.enable_root_key_transformation!` and `Alba.disable_root_key_transformation!`
|
24
|
+
- If using `transform_keys`, the default inflector is no longer set by default [d02245c8](https://github.com/okuramasafumi/alba/commit/d02245c87e9df303cb20e354a81e5457ea460bdd#diff-ecd8c835d2390b8cb89e7ff75e599f0c15cdbe18c30981d6090f4a515566686f)
|
25
|
+
To retain this functionality in Rails, add an initializer with the following:
|
26
|
+
`Alba.enable_inference!(with: :active_support)`
|
16
27
|
|
17
28
|
### New features
|
18
29
|
|
19
|
-
- Passing an initial object to proc function in associations
|
20
|
-
- Allow association resource to be Proc
|
21
|
-
- `collection_key` to serialize collection into a Hash
|
22
|
-
- params is now overridable
|
23
|
-
- Key transformation now cascades
|
24
|
-
- nested attribute
|
25
|
-
- Implement `as_json`
|
30
|
+
- Passing an initial object to proc function in associations [#209](https://github.com/okuramasafumi/alba/pull/209)
|
31
|
+
- Allow association resource to be Proc [#213](https://github.com/okuramasafumi/alba/pull/213)
|
32
|
+
- `collection_key` to serialize collection into a Hash [#119](https://github.com/okuramasafumi/alba/pull/119)
|
33
|
+
- params is now overridable [#227](https://github.com/okuramasafumi/alba/pull/227)
|
34
|
+
- Key transformation now cascades [#232](https://github.com/okuramasafumi/alba/pull/232)
|
35
|
+
- nested attribute [#237](https://github.com/okuramasafumi/alba/pull/237)
|
36
|
+
- Implement `as_json` [#249](https://github.com/okuramasafumi/alba/pull/249)
|
26
37
|
|
27
38
|
### Bugfix
|
28
39
|
|
data/Gemfile
CHANGED
@@ -10,7 +10,7 @@ gem 'inch', require: false # For inline documents
|
|
10
10
|
gem 'minitest', '~> 5.14' # For test
|
11
11
|
gem 'rake', '~> 13.0' # For test and automation
|
12
12
|
gem 'rubocop', '>= 0.79.0', require: false # For lint
|
13
|
-
gem 'rubocop-minitest', '~> 0.
|
13
|
+
gem 'rubocop-minitest', '~> 0.23.0', require: false # For lint
|
14
14
|
gem 'rubocop-performance', '~> 1.15.0', require: false # For lint
|
15
15
|
gem 'rubocop-rake', '>= 0.5.1', require: false # For lint
|
16
16
|
gem 'rubocop-sensible', '~> 0.3.0', require: false # For lint
|
data/lib/alba/association.rb
CHANGED
@@ -20,10 +20,9 @@ module Alba
|
|
20
20
|
@condition = condition
|
21
21
|
@resource = resource
|
22
22
|
@params = params
|
23
|
-
@key_transformation = key_transformation
|
24
23
|
return if @resource
|
25
24
|
|
26
|
-
assign_resource(nesting, block)
|
25
|
+
assign_resource(nesting, key_transformation, block)
|
27
26
|
end
|
28
27
|
|
29
28
|
# Recursively converts an object into a Hash
|
@@ -58,9 +57,12 @@ module Alba
|
|
58
57
|
end
|
59
58
|
end
|
60
59
|
|
61
|
-
def assign_resource(nesting, block)
|
60
|
+
def assign_resource(nesting, key_transformation, block)
|
62
61
|
@resource = if block
|
63
|
-
Alba.resource_class
|
62
|
+
klass = Alba.resource_class
|
63
|
+
klass.transform_keys(key_transformation)
|
64
|
+
klass.class_eval(&block)
|
65
|
+
klass
|
64
66
|
elsif Alba.inferring
|
65
67
|
Alba.infer_resource_class(@name, nesting: nesting)
|
66
68
|
else
|
@@ -76,7 +78,6 @@ module Alba
|
|
76
78
|
|
77
79
|
def to_h_with_constantize_resource(within, params)
|
78
80
|
@resource = constantize(@resource)
|
79
|
-
@resource.transform_keys(@key_transformation)
|
80
81
|
@resource.new(object, params: params, within: within).to_h
|
81
82
|
end
|
82
83
|
end
|
@@ -1,9 +1,9 @@
|
|
1
|
+
require_relative 'association'
|
2
|
+
require_relative 'constants'
|
3
|
+
|
1
4
|
module Alba
|
2
5
|
# Represents attribute with `if` option
|
3
6
|
class ConditionalAttribute
|
4
|
-
CONDITION_UNMET = Object.new.freeze
|
5
|
-
public_constant :CONDITION_UNMET # It's public for use in `Alba::Resource`
|
6
|
-
|
7
7
|
# @param body [Symbol, Proc, Alba::Association, Alba::TypedAttribute] real attribute wrapped with condition
|
8
8
|
# @param condition [Symbol, Proc] condition to check
|
9
9
|
def initialize(body:, condition:)
|
@@ -15,14 +15,14 @@ module Alba
|
|
15
15
|
#
|
16
16
|
# @param resource [Alba::Resource]
|
17
17
|
# @param object [Object] needed for collection, each object from collection
|
18
|
-
# @return [
|
18
|
+
# @return [Alba::REMOVE_KEY, Object] REMOVE_KEY if condition is unmet, fetched attribute otherwise
|
19
19
|
def with_passing_condition(resource:, object: nil)
|
20
|
-
return
|
20
|
+
return Alba::REMOVE_KEY unless condition_passes?(resource, object)
|
21
21
|
|
22
22
|
fetched_attribute = yield(@body)
|
23
23
|
return fetched_attribute if fetched_attribute.nil? || !with_two_arity_proc_condition
|
24
24
|
|
25
|
-
return
|
25
|
+
return Alba::REMOVE_KEY unless resource.instance_exec(object, attribute_from_association_body_or(fetched_attribute), &@condition)
|
26
26
|
|
27
27
|
fetched_attribute
|
28
28
|
end
|
@@ -10,8 +10,9 @@ module Alba
|
|
10
10
|
|
11
11
|
# @return [Hash]
|
12
12
|
def value(object)
|
13
|
-
resource_class = Alba.resource_class
|
13
|
+
resource_class = Alba.resource_class
|
14
14
|
resource_class.transform_keys(@key_transformation)
|
15
|
+
resource_class.class_eval(&@block)
|
15
16
|
resource_class.new(object).serializable_hash
|
16
17
|
end
|
17
18
|
end
|
data/lib/alba/resource.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require_relative 'association'
|
2
2
|
require_relative 'conditional_attribute'
|
3
|
+
require_relative 'constants'
|
3
4
|
require_relative 'typed_attribute'
|
4
5
|
require_relative 'nested_attribute'
|
5
6
|
require_relative 'deprecation'
|
@@ -215,7 +216,7 @@ module Alba
|
|
215
216
|
def set_key_and_attribute_body_from(object, key, attribute, hash)
|
216
217
|
key = transform_key(key)
|
217
218
|
value = fetch_attribute(object, key, attribute)
|
218
|
-
hash[key] = value unless value ==
|
219
|
+
hash[key] = value unless value == Alba::REMOVE_KEY
|
219
220
|
end
|
220
221
|
|
221
222
|
def handle_error(error, object, key, attribute, hash)
|
@@ -271,7 +272,7 @@ module Alba
|
|
271
272
|
|
272
273
|
def yield_if_within(association_name)
|
273
274
|
within = check_within(association_name)
|
274
|
-
yield(within)
|
275
|
+
within ? yield(within) : Alba::REMOVE_KEY
|
275
276
|
end
|
276
277
|
|
277
278
|
def check_within(association_name)
|
data/lib/alba/version.rb
CHANGED
data/lib/alba.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.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OKURA Masafumi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-02 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.
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- lib/alba.rb
|
53
53
|
- lib/alba/association.rb
|
54
54
|
- lib/alba/conditional_attribute.rb
|
55
|
+
- lib/alba/constants.rb
|
55
56
|
- lib/alba/default_inflector.rb
|
56
57
|
- lib/alba/deprecation.rb
|
57
58
|
- lib/alba/errors.rb
|