alba 3.9.0 → 3.9.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 +8 -1
- data/README.md +41 -0
- data/lib/alba/resource.rb +19 -4
- data/lib/alba/typed_attribute.rb +1 -1
- data/lib/alba/version.rb +1 -1
- 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: ce994ee53f2a5f9291096f897469bd131c5f925560a0cd13690247d1278b9310
|
4
|
+
data.tar.gz: cbcc9e5e031fc52c20560fa0d8f06465728e5825a08a36d50d6d26f5f1ca4a3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ac432a009738c6e6d69e3d9db07895ca67653a33f7c226194a1851978076dbde05de1da6c767e5b582f5e6e0cded2347fb2bcda858a147d4911cc7b55b06617
|
7
|
+
data.tar.gz: f317a32584cc90e2b49043e4e0c5610351ba710866cd29336a43e151fb0b42061d82e8e39837ed900c6994f7c8d65214cc31845e315a84cb0fd10904c8b09b89
|
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.9.1 2025-09-27
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
- RBS types
|
14
|
+
- This change itself is just an addition, but not feature addition. Here I picked `3.9.1`, but I don't know how semver works in this case...
|
15
|
+
|
9
16
|
## 3.9.0 2025-08-14
|
10
17
|
|
11
18
|
### Added
|
@@ -22,7 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
22
29
|
### Added
|
23
30
|
|
24
31
|
- add support for ignoring key when using proc for on_error [#450](https://github.com/okuramasafumi/alba/pull/450)
|
25
|
-
- Thank you, @mainmethod
|
32
|
+
- Thank you, @mainmethod
|
26
33
|
|
27
34
|
### Note
|
28
35
|
|
data/README.md
CHANGED
@@ -100,6 +100,26 @@ While Alba's core is simple, it provides additional features when you need them.
|
|
100
100
|
- Well tested, the test coverage is 99%
|
101
101
|
- Well maintained, getting frequent update and new releases (see [version history](https://rubygems.org/gems/alba/versions))
|
102
102
|
|
103
|
+
## Comparison with other serializers
|
104
|
+
|
105
|
+
Alba aims to provide a well-balanced combination of simplicity, performance, and features. Here's how it compares to other popular Ruby JSON serializers:
|
106
|
+
|
107
|
+
| Feature | Alba | [AMS](https://github.com/rails-api/active_model_serializers) | [Blueprinter](https://github.com/procore/blueprinter) | [JSONAPI::Serializer](https://github.com/jsonapi-serializer/jsonapi-serializer) | [JBuilder](https://github.com/rails/jbuilder) |
|
108
|
+
|---------|------|-----|-------------|---------------------|----------|
|
109
|
+
| **Dependencies** | [None](https://github.com/okuramasafumi/alba#other-reasons) | ActiveSupport | Minimal | Minimal | Rails |
|
110
|
+
| **JSON:API Compliance** | Manual | [Planned](https://github.com/rails-api/active_model_serializers#status-of-ams) | Via extension | [Full support](https://github.com/jsonapi-serializer/jsonapi-serializer#json-api-serializer) | Manual |
|
111
|
+
| **Caching** | [Not built-in](https://github.com/okuramasafumi/alba#caching) | Yes | Via extension | [Supported](https://github.com/jsonapi-serializer/jsonapi-serializer) | [Fragment caching](https://github.com/rails/jbuilder#caching) |
|
112
|
+
| **Key Transformation** | [Yes](https://github.com/okuramasafumi/alba#key-transformation) | Yes | [Yes](https://github.com/procore/blueprinter) | [Yes](https://goithub.com/jsonapi-serializer/jsonapi-serializer) | [Yes](https://github.com/rails/jbuilder#key-formatting) |
|
113
|
+
| **Conditional Attributes** | [Yes](https://github.com/okuramasafumi/alba#conditional-attributes) | Yes | [Yes](https://github.com/procore/blueprinter) | [Yes](https://github.com/jsonapi-serializer/jsonapi-serializer) | [Yes](https://github.com/rails/jbuilder) |
|
114
|
+
| **Type Validation & Coercion** | [Yes](https://github.com/okuramasafumi/alba#types) | No | No | No | No |
|
115
|
+
| **Custom Type System** | [Yes](https://github.com/okuramasafumi/alba#custom-types) | No | No | No | No |
|
116
|
+
| **Nested Attributes** | [Yes](https://github.com/okuramasafumi/alba#nested-attribute) | No | No | No | Partial |
|
117
|
+
| **Circular Reference Control** | [Yes with `within`](https://github.com/okuramasafumi/alba#circular-associations-control) | Limited | No | No | No |
|
118
|
+
| **Error Handling Strategies** | [Flexible](https://github.com/okuramasafumi/alba#error-handling) | Limited | Limited | Basic | Basic |
|
119
|
+
| **Nil Handling** | [Yes](https://github.com/okuramasafumi/alba#nil-handling) | No | No | No | No |
|
120
|
+
| **Layout System** | [Yes](https://github.com/okuramasafumi/alba#layout) | No | No | No | No |
|
121
|
+
| **Maintenance Status** | Active | [Minimal](https://github.com/rails-api/active_model_serializers#status-of-ams) | Active | Active | Active |
|
122
|
+
|
103
123
|
## Installation
|
104
124
|
|
105
125
|
Add this line to your application's Gemfile:
|
@@ -725,6 +745,10 @@ Alba.serialize(something)
|
|
725
745
|
|
726
746
|
Although this might be useful sometimes, it's generally recommended to define a class for Resource. Defining a class is often more readable and more maintainable, and inline definitions cannot levarage the benefit of YJIT (it's the slowest with the benchmark YJIT enabled).
|
727
747
|
|
748
|
+
#### Alba.hashify
|
749
|
+
|
750
|
+
`Alba.hashify` is similar to `Alba.serialize`, but returns a Hash instead of JSON string.
|
751
|
+
|
728
752
|
#### Inline definition for multiple root keys
|
729
753
|
|
730
754
|
While Alba doesn't directly support multiple root keys, you can simulate it with `Alba.serialize`.
|
@@ -1974,6 +1998,23 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
1974
1998
|
|
1975
1999
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
1976
2000
|
|
2001
|
+
### Type Checking
|
2002
|
+
|
2003
|
+
Alba uses RBS for type signatures and Steep for type checking.
|
2004
|
+
To run type checking:
|
2005
|
+
|
2006
|
+
```bash
|
2007
|
+
# Install type checking dependencies (CRuby only)
|
2008
|
+
bundle install --with type
|
2009
|
+
|
2010
|
+
# Run all type checks
|
2011
|
+
bundle exec rake typecheck
|
2012
|
+
|
2013
|
+
# Or run individual checks
|
2014
|
+
bundle exec rake rbs # Validate RBS signatures
|
2015
|
+
bundle exec rake steep # Run Steep type checker
|
2016
|
+
```
|
2017
|
+
|
1977
2018
|
## Contributing
|
1978
2019
|
|
1979
2020
|
Thank you for begin interested in contributing to Alba! Please see [contributors guide](https://github.com/okuramasafumi/alba/blob/main/CONTRIBUTING.md) before start contributing. If you have any questions, please feel free to ask in [Discussions](https://github.com/okuramasafumi/alba/discussions).
|
data/lib/alba/resource.rb
CHANGED
@@ -45,7 +45,8 @@ module Alba
|
|
45
45
|
|
46
46
|
# @param object [Object] the object to be serialized
|
47
47
|
# @param params [Hash] user-given Hash for arbitrary data
|
48
|
-
# @param within [
|
48
|
+
# @param within [Alba::WITHIN_DEFAULT, Hash, Array, nil, false, true]
|
49
|
+
# determines what associations to be serialized. If not set, it serializes all associations.
|
49
50
|
# @param with_traits [Symbol, Array<Symbol>, nil] specified traits
|
50
51
|
# @param select [Method] select method object used with `nested_attribute` and `trait`
|
51
52
|
def initialize(object, params: {}, within: WITHIN_DEFAULT, with_traits: nil, select: nil)
|
@@ -61,7 +62,7 @@ module Alba
|
|
61
62
|
|
62
63
|
# Serialize object into JSON string
|
63
64
|
#
|
64
|
-
# @param root_key [Symbol, nil
|
65
|
+
# @param root_key [Symbol, nil]
|
65
66
|
# @param meta [Hash] metadata for this serialization
|
66
67
|
# @return [String] serialized JSON string
|
67
68
|
def serialize(root_key: nil, meta: {})
|
@@ -88,7 +89,7 @@ module Alba
|
|
88
89
|
# Returns a Hash corresponding {#serialize}
|
89
90
|
#
|
90
91
|
# @param _options [Hash] dummy parameter for Rails compatibility
|
91
|
-
# @param root_key [Symbol, nil
|
92
|
+
# @param root_key [Symbol, nil]
|
92
93
|
# @param meta [Hash] metadata for this serialization
|
93
94
|
# @return [Hash]
|
94
95
|
def as_json(_options = {}, root_key: nil, meta: {})
|
@@ -434,7 +435,9 @@ module Alba
|
|
434
435
|
# @param with_traits [Symbol, Array<Symbol>, nil] specified traits
|
435
436
|
# @param params [Hash] params override for the association
|
436
437
|
# @param options [Hash<Symbol, Proc>]
|
437
|
-
# @option options [Proc] if a condition to decide if this association should be serialized
|
438
|
+
# @option options [Proc, Symbol, nil] if a condition to decide if this association should be serialized
|
439
|
+
# When it's Proc, it's called to check condition
|
440
|
+
# When it's Symbol, it's treated as a method name on the Resource and the method is called
|
438
441
|
# @param block [Block]
|
439
442
|
# @return [void]
|
440
443
|
# @see Alba::Association#initialize
|
@@ -497,6 +500,7 @@ module Alba
|
|
497
500
|
# @param key [String, Symbol]
|
498
501
|
# @param key_for_collection [String, Symbol]
|
499
502
|
# @raise [NoMethodError] when key doesn't respond to `to_sym` method
|
503
|
+
# @return [void]
|
500
504
|
def root_key(key, key_for_collection = nil)
|
501
505
|
@_key = key.to_sym
|
502
506
|
@_key_for_collection = key_for_collection&.to_sym
|
@@ -506,18 +510,21 @@ module Alba
|
|
506
510
|
#
|
507
511
|
# @param key [String, Symbol]
|
508
512
|
# @raise [NoMethodError] when key doesn't respond to `to_sym` method
|
513
|
+
# @return [void]
|
509
514
|
def root_key_for_collection(key)
|
510
515
|
@_key = true
|
511
516
|
@_key_for_collection = key.to_sym
|
512
517
|
end
|
513
518
|
|
514
519
|
# Set root key to true
|
520
|
+
# @return [void]
|
515
521
|
def root_key!
|
516
522
|
@_key = true
|
517
523
|
@_key_for_collection = true
|
518
524
|
end
|
519
525
|
|
520
526
|
# Set metadata
|
527
|
+
# @return [void]
|
521
528
|
def meta(key = :meta, &block)
|
522
529
|
@_meta = [key, block]
|
523
530
|
end
|
@@ -526,6 +533,7 @@ module Alba
|
|
526
533
|
#
|
527
534
|
# @param file [String] name of the layout file
|
528
535
|
# @param inline [Proc] a proc returning JSON string or a Hash representing JSON
|
536
|
+
# @return [void]
|
529
537
|
def layout(file: nil, inline: nil)
|
530
538
|
@_layout = Layout.new(file: file, inline: inline)
|
531
539
|
end
|
@@ -537,6 +545,7 @@ module Alba
|
|
537
545
|
# @param cascade [Boolean] decides if key transformation cascades into inline association
|
538
546
|
# Default is true but can be set false for old (v1) behavior
|
539
547
|
# @raise [Alba::Error] when type is not supported
|
548
|
+
# @return [void]
|
540
549
|
def transform_keys(type, root: true, cascade: true)
|
541
550
|
type = type.to_sym
|
542
551
|
unless %i[none snake camel lower_camel dash].include?(type)
|
@@ -570,6 +579,7 @@ module Alba
|
|
570
579
|
# Sets key for collection serialization
|
571
580
|
#
|
572
581
|
# @param key [String, Symbol]
|
582
|
+
# @return [void]
|
573
583
|
def collection_key(key)
|
574
584
|
@_collection_key = key.to_sym
|
575
585
|
end
|
@@ -579,6 +589,7 @@ module Alba
|
|
579
589
|
#
|
580
590
|
# @param handler [Symbol] `:raise`, `:ignore` or `:nullify`
|
581
591
|
# @param block [Block]
|
592
|
+
# @return [void]
|
582
593
|
def on_error(handler = nil, &block)
|
583
594
|
raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block
|
584
595
|
raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block
|
@@ -600,6 +611,7 @@ module Alba
|
|
600
611
|
# Set nil handler
|
601
612
|
#
|
602
613
|
# @param block [Block]
|
614
|
+
# @return [void]
|
603
615
|
def on_nil(&block)
|
604
616
|
@_on_nil = block
|
605
617
|
end
|
@@ -607,6 +619,7 @@ module Alba
|
|
607
619
|
# Define helper methods
|
608
620
|
#
|
609
621
|
# @param mod [Module] a module to extend
|
622
|
+
# @return [void]
|
610
623
|
def helper(mod = @_helper || Module.new, &block)
|
611
624
|
mod.module_eval(&block) if block
|
612
625
|
extend mod
|
@@ -615,11 +628,13 @@ module Alba
|
|
615
628
|
end
|
616
629
|
|
617
630
|
# DSL for alias, purely for readability
|
631
|
+
# @return [void]
|
618
632
|
def prefer_resource_method!
|
619
633
|
alias_method :fetch_attribute_from_object_and_resource, :_fetch_attribute_from_resource_first
|
620
634
|
end
|
621
635
|
|
622
636
|
# DSL for alias, purely for readability
|
637
|
+
# @return [void]
|
623
638
|
def prefer_object_method!
|
624
639
|
alias_method :fetch_attribute_from_object_and_resource, :_fetch_attribute_from_object_first
|
625
640
|
end
|
data/lib/alba/typed_attribute.rb
CHANGED
data/lib/alba/version.rb
CHANGED
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.9.
|
4
|
+
version: 3.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OKURA Masafumi
|
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
57
|
- !ruby/object:Gem::Version
|
58
58
|
version: '0'
|
59
59
|
requirements: []
|
60
|
-
rubygems_version: 3.7
|
60
|
+
rubygems_version: 3.6.7
|
61
61
|
specification_version: 4
|
62
62
|
summary: Alba is the fastest JSON serializer for Ruby.
|
63
63
|
test_files: []
|