alba 3.0.2 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +36 -0
- data/lib/alba/nested_attribute.rb +3 -2
- data/lib/alba/resource.rb +5 -4
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +3 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a2aa4a1fc6859ca700fefbe63af11657031743b9ad6c4ec9f97c7d7eac2cef7
|
4
|
+
data.tar.gz: 8703816ac23d30e432053c6836a8ed3e517f318a9924ae4bf67e5b3cff79f666
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abd511841f6100e00e53687a3f4b7a58a04645090f3e7204edec2c7f8ea438d70e9014b576e56aae302db293796ccd793aa4d976b4241f91f25a1cf15d0e19e6
|
7
|
+
data.tar.gz: bf76202cdf514b5134b5a3f1e17b17cca78dbb9c9f1029f562d9d4ccc992c9d8aaeefbf01d1c45d1f4aff658d53a21e24b0caa279b6852e1516765c6ce62d99d
|
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
|
+
## [3.0.3] 2023-12-25
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
|
13
|
+
- Make `as_json` compatible with Rails [#350](https://github.com/okuramasafumi/alba/pull/350)
|
14
|
+
- Fix circular association for nested_attribute [#353](https://github.com/okuramasafumi/alba/pull/353)
|
15
|
+
|
9
16
|
## [3.0.2] 2023-12-05
|
10
17
|
|
11
18
|
### Fixed
|
data/README.md
CHANGED
@@ -1457,6 +1457,8 @@ end
|
|
1457
1457
|
|
1458
1458
|
Within `helper` block, all methods should be defined without `self.`.
|
1459
1459
|
|
1460
|
+
`helper`
|
1461
|
+
|
1460
1462
|
### Caching
|
1461
1463
|
|
1462
1464
|
Currently, Alba doesn't support caching, primarily due to the behavior of `ActiveRecord::Relation`'s cache. See [the issue](https://github.com/rails/rails/issues/41784).
|
@@ -1563,6 +1565,40 @@ end
|
|
1563
1565
|
|
1564
1566
|
In this way we have shorter and cleaner code. Note that we need to use `send` or `public_send` in `attribute` block to get attribute data.
|
1565
1567
|
|
1568
|
+
#### Using `helper`
|
1569
|
+
|
1570
|
+
When we `extend AlbaExtension` like above, it's not available in inline associations.
|
1571
|
+
|
1572
|
+
```ruby
|
1573
|
+
class BarResource
|
1574
|
+
include Alba::Resource
|
1575
|
+
extend AlbaExtension
|
1576
|
+
# other attributes
|
1577
|
+
formatted_time_attributes :created_at, :updated_at
|
1578
|
+
|
1579
|
+
one :something do
|
1580
|
+
# This DOES NOT work!
|
1581
|
+
formatted_time_attributes :updated_at
|
1582
|
+
end
|
1583
|
+
end
|
1584
|
+
```
|
1585
|
+
|
1586
|
+
In this case, we can use [helper](#helper) instead of `extend`.
|
1587
|
+
|
1588
|
+
```ruby
|
1589
|
+
class BarResource
|
1590
|
+
include Alba::Resource
|
1591
|
+
helper AlbaExtension # HERE!
|
1592
|
+
# other attributes
|
1593
|
+
formatted_time_attributes :created_at, :updated_at
|
1594
|
+
|
1595
|
+
one :something do
|
1596
|
+
# This WORKS!
|
1597
|
+
formatted_time_attributes :updated_at
|
1598
|
+
end
|
1599
|
+
end
|
1600
|
+
```
|
1601
|
+
|
1566
1602
|
### Debugging
|
1567
1603
|
|
1568
1604
|
Debugging is not easy. If you find Alba not working as you expect, there are a few things to do:
|
@@ -11,12 +11,13 @@ module Alba
|
|
11
11
|
|
12
12
|
# @param object [Object] the object being serialized
|
13
13
|
# @param params [Hash] params Hash inherited from Resource
|
14
|
+
# @param within [Object, nil, false, true] determines what associations to be serialized. If not set, it serializes all associations.
|
14
15
|
# @return [Hash] hash serialized from running the class body in the object
|
15
|
-
def value(object:, params:)
|
16
|
+
def value(object:, params:, within:)
|
16
17
|
resource_class = Alba.resource_class
|
17
18
|
resource_class.transform_keys(@key_transformation)
|
18
19
|
resource_class.class_eval(&@block)
|
19
|
-
resource_class.new(object, params: params).serializable_hash
|
20
|
+
resource_class.new(object, params: params, within: within).serializable_hash
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|
data/lib/alba/resource.rb
CHANGED
@@ -26,7 +26,7 @@ module Alba
|
|
26
26
|
base.class_eval do
|
27
27
|
# Initialize
|
28
28
|
INTERNAL_VARIABLES.each do |name, initial|
|
29
|
-
instance_variable_set("@#{name}", initial.dup) unless instance_variable_defined?("@#{name}")
|
29
|
+
instance_variable_set(:"@#{name}", initial.dup) unless instance_variable_defined?(:"@#{name}")
|
30
30
|
setup_method_body << "@#{name} = self.class.#{name};"
|
31
31
|
end
|
32
32
|
base.define_method(:encode, Alba.encoder)
|
@@ -71,10 +71,11 @@ module Alba
|
|
71
71
|
|
72
72
|
# Returns a Hash correspondng {#serialize}
|
73
73
|
#
|
74
|
+
# @param _options [Hash] dummy parameter for Rails compatibility
|
74
75
|
# @param root_key [Symbol, nil, true]
|
75
76
|
# @param meta [Hash] metadata for this seialization
|
76
77
|
# @return [Hash]
|
77
|
-
def as_json(root_key: nil, meta: {})
|
78
|
+
def as_json(_options = {}, root_key: nil, meta: {})
|
78
79
|
key = root_key.nil? ? fetch_key : root_key
|
79
80
|
key = Alba.regularize_key(key)
|
80
81
|
if key && !key.empty?
|
@@ -246,7 +247,7 @@ module Alba
|
|
246
247
|
when Proc then instance_exec(obj, &attribute)
|
247
248
|
when Alba::Association then yield_if_within(attribute.name.to_sym) { |within| attribute.to_h(obj, params: params, within: within) }
|
248
249
|
when TypedAttribute then attribute.value(obj)
|
249
|
-
when NestedAttribute then attribute.value(object: obj, params: params)
|
250
|
+
when NestedAttribute then attribute.value(object: obj, params: params, within: @within)
|
250
251
|
when ConditionalAttribute then attribute.with_passing_condition(resource: self, object: obj) { |attr| fetch_attribute(obj, key, attr) }
|
251
252
|
else raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
|
252
253
|
end
|
@@ -316,7 +317,7 @@ module Alba
|
|
316
317
|
# @private
|
317
318
|
def inherited(subclass)
|
318
319
|
super
|
319
|
-
INTERNAL_VARIABLES.each_key { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}").clone) }
|
320
|
+
INTERNAL_VARIABLES.each_key { |name| subclass.instance_variable_set(:"@#{name}", instance_variable_get(:"@#{name}").clone) }
|
320
321
|
end
|
321
322
|
|
322
323
|
# Defining methods for DSLs and disable parameter number check since for users' benefits increasing params is fine
|
data/lib/alba/version.rb
CHANGED
data/lib/alba.rb
CHANGED
@@ -114,11 +114,14 @@ 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
|
+
# rubocop-performance 1.20.2 might resolve this
|
118
|
+
# rubocop:disable Performance/StringIdentifierArgument
|
117
119
|
begin
|
118
120
|
const_parent.const_get("#{inflector.classify(name)}Resource")
|
119
121
|
rescue NameError # Retry for serializer
|
120
122
|
const_parent.const_get("#{inflector.classify(name)}Serializer")
|
121
123
|
end
|
124
|
+
# rubocop:enable Performance/StringIdentifierArgument
|
122
125
|
end
|
123
126
|
|
124
127
|
# 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: 3.0.
|
4
|
+
version: 3.0.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: 2023-12-
|
11
|
+
date: 2023-12-25 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.
|