alba 1.5.0 → 2.0.0
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/.codeclimate.yml +11 -0
- data/.github/dependabot.yml +4 -18
- data/.github/workflows/codeql-analysis.yml +70 -0
- data/.github/workflows/main.yml +4 -4
- data/.github/workflows/perf.yml +2 -2
- data/.rubocop.yml +5 -1
- data/CHANGELOG.md +34 -0
- data/CONTRIBUTING.md +30 -0
- data/Gemfile +6 -2
- data/HACKING.md +41 -0
- data/README.md +599 -128
- data/Rakefile +2 -2
- data/alba.gemspec +8 -4
- data/benchmark/README.md +81 -0
- data/benchmark/collection.rb +60 -74
- data/benchmark/single_resource.rb +33 -2
- data/docs/migrate_from_jbuilder.md +18 -4
- data/docs/rails.md +44 -0
- data/lib/alba/association.rb +49 -9
- data/lib/alba/conditional_attribute.rb +54 -0
- data/lib/alba/default_inflector.rb +13 -24
- data/lib/alba/errors.rb +10 -0
- data/lib/alba/layout.rb +67 -0
- data/lib/alba/nested_attribute.rb +18 -0
- data/lib/alba/resource.rb +240 -156
- data/lib/alba/typed_attribute.rb +1 -1
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +43 -58
- data/logo/alba-card.png +0 -0
- data/logo/alba-sign.png +0 -0
- data/logo/alba-typography.png +0 -0
- metadata +21 -11
- data/gemfiles/all.gemfile +0 -19
- data/lib/alba/key_transform_factory.rb +0 -33
- data/lib/alba/many.rb +0 -21
- data/lib/alba/one.rb +0 -21
- data/sider.yml +0 -60
data/lib/alba.rb
CHANGED
@@ -1,23 +1,15 @@
|
|
1
1
|
require 'json'
|
2
2
|
require_relative 'alba/version'
|
3
|
+
require_relative 'alba/errors'
|
3
4
|
require_relative 'alba/resource'
|
4
5
|
require_relative 'alba/deprecation'
|
5
6
|
|
6
7
|
# Core module
|
7
8
|
module Alba
|
8
|
-
# Base class for Errors
|
9
|
-
class Error < StandardError; end
|
10
|
-
|
11
|
-
# Error class for backend which is not supported
|
12
|
-
class UnsupportedBackend < Error; end
|
13
|
-
|
14
|
-
# Error class for type which is not supported
|
15
|
-
class UnsupportedType < Error; end
|
16
|
-
|
17
9
|
class << self
|
18
|
-
attr_reader :backend, :encoder, :inferring
|
10
|
+
attr_reader :backend, :encoder, :inferring
|
19
11
|
|
20
|
-
# Accessor for inflector, a module responsible for
|
12
|
+
# Accessor for inflector, a module responsible for inflecting strings
|
21
13
|
attr_accessor :inflector
|
22
14
|
|
23
15
|
# Set the backend, which actually serializes object into JSON
|
@@ -46,26 +38,24 @@ module Alba
|
|
46
38
|
# Serialize the object with inline definitions
|
47
39
|
#
|
48
40
|
# @param object [Object] the object to be serialized
|
49
|
-
# @param key [Symbol, nil, true] DEPRECATED, use root_key instead
|
50
41
|
# @param root_key [Symbol, nil, true]
|
51
42
|
# @param block [Block] resource block
|
52
43
|
# @return [String] serialized JSON string
|
53
44
|
# @raise [ArgumentError] if block is absent or `with` argument's type is wrong
|
54
|
-
def serialize(object,
|
55
|
-
Alba::Deprecation.warn '`key` option to `serialize` method is deprecated, use `root_key` instead.' if key
|
45
|
+
def serialize(object, root_key: nil, &block)
|
56
46
|
klass = block ? resource_class(&block) : infer_resource_class(object.class.name)
|
57
47
|
|
58
48
|
resource = klass.new(object)
|
59
|
-
resource.serialize(root_key: root_key
|
49
|
+
resource.serialize(root_key: root_key)
|
60
50
|
end
|
61
51
|
|
62
52
|
# Enable inference for key and resource name
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
53
|
+
#
|
54
|
+
# @param with [Symbol, Class, Module] inflector
|
55
|
+
# When it's a Symbol, it sets inflector with given name
|
56
|
+
# When it's a Class or a Module, it sets given object to inflector
|
57
|
+
def enable_inference!(with:)
|
58
|
+
@inflector = inflector_from(with)
|
69
59
|
@inferring = true
|
70
60
|
end
|
71
61
|
|
@@ -74,38 +64,6 @@ module Alba
|
|
74
64
|
@inferring = false
|
75
65
|
end
|
76
66
|
|
77
|
-
# Set error handler
|
78
|
-
#
|
79
|
-
# @param [Symbol] handler
|
80
|
-
# @param [Block]
|
81
|
-
# @raise [ArgumentError] if both handler and block params exist
|
82
|
-
# @raise [ArgumentError] if both handler and block params don't exist
|
83
|
-
# @return [void]
|
84
|
-
def on_error(handler = nil, &block)
|
85
|
-
raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block
|
86
|
-
raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block
|
87
|
-
|
88
|
-
@_on_error = handler || block
|
89
|
-
end
|
90
|
-
|
91
|
-
# Set nil handler
|
92
|
-
#
|
93
|
-
# @param block [Block]
|
94
|
-
# @return [void]
|
95
|
-
def on_nil(&block)
|
96
|
-
@_on_nil = block
|
97
|
-
end
|
98
|
-
|
99
|
-
# Enable root key transformation
|
100
|
-
def enable_root_key_transformation!
|
101
|
-
@transforming_root_key = true
|
102
|
-
end
|
103
|
-
|
104
|
-
# Disable root key transformation
|
105
|
-
def disable_root_key_transformation!
|
106
|
-
@transforming_root_key = false
|
107
|
-
end
|
108
|
-
|
109
67
|
# @param block [Block] resource body
|
110
68
|
# @return [Class<Alba::Resource>] resource class
|
111
69
|
def resource_class(&block)
|
@@ -119,9 +77,10 @@ module Alba
|
|
119
77
|
# @param nesting [String, nil] namespace Alba tries to find resource class in
|
120
78
|
# @return [Class<Alba::Resource>] resource class
|
121
79
|
def infer_resource_class(name, nesting: nil)
|
122
|
-
enable_inference
|
80
|
+
raise Alba::Error, 'Inference is disabled so Alba cannot infer resource name. Use `Alba.enable_inference!` before use.' unless Alba.inferring
|
81
|
+
|
123
82
|
const_parent = nesting.nil? ? Object : Object.const_get(nesting)
|
124
|
-
const_parent.const_get("#{
|
83
|
+
const_parent.const_get("#{inflector.classify(name)}Resource")
|
125
84
|
end
|
126
85
|
|
127
86
|
# Reset config variables
|
@@ -130,15 +89,28 @@ module Alba
|
|
130
89
|
@encoder = default_encoder
|
131
90
|
@_on_error = :raise
|
132
91
|
@_on_nil = nil
|
133
|
-
@transforming_root_key = false # TODO: This will be true since 2.0
|
134
92
|
end
|
135
93
|
|
136
94
|
private
|
137
95
|
|
96
|
+
def inflector_from(name_or_module)
|
97
|
+
case name_or_module
|
98
|
+
when :default, :active_support
|
99
|
+
require_relative 'alba/default_inflector'
|
100
|
+
Alba::DefaultInflector
|
101
|
+
when :dry
|
102
|
+
require 'dry/inflector'
|
103
|
+
Dry::Inflector.new
|
104
|
+
else
|
105
|
+
validate_inflector(name_or_module)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
138
109
|
def set_encoder_from_backend
|
139
110
|
@encoder = case @backend
|
140
111
|
when :oj, :oj_strict then try_oj
|
141
112
|
when :oj_rails then try_oj(mode: :rails)
|
113
|
+
when :oj_default then try_oj(mode: :default)
|
142
114
|
when :active_support then try_active_support
|
143
115
|
when nil, :default, :json then default_encoder
|
144
116
|
else
|
@@ -148,7 +120,12 @@ module Alba
|
|
148
120
|
|
149
121
|
def try_oj(mode: :strict)
|
150
122
|
require 'oj'
|
151
|
-
|
123
|
+
case mode
|
124
|
+
when :default
|
125
|
+
->(hash) { Oj.dump(hash) }
|
126
|
+
else
|
127
|
+
->(hash) { Oj.dump(hash, mode: mode) }
|
128
|
+
end
|
152
129
|
rescue LoadError
|
153
130
|
Kernel.warn '`Oj` is not installed, falling back to default JSON encoder.'
|
154
131
|
default_encoder
|
@@ -164,8 +141,16 @@ module Alba
|
|
164
141
|
|
165
142
|
def default_encoder
|
166
143
|
lambda do |hash|
|
167
|
-
JSON.
|
144
|
+
JSON.generate(hash)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def validate_inflector(inflector)
|
149
|
+
unless %i[camelize camelize_lower dasherize classify].all? { |m| inflector.respond_to?(m) }
|
150
|
+
raise Alba::Error, "Given inflector, #{inflector.inspect} is not valid. It must implement `camelize`, `camelize_lower`, `dasherize` and `classify`."
|
168
151
|
end
|
152
|
+
|
153
|
+
inflector
|
169
154
|
end
|
170
155
|
end
|
171
156
|
|
data/logo/alba-card.png
ADDED
Binary file
|
data/logo/alba-sign.png
ADDED
Binary file
|
Binary file
|
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:
|
4
|
+
version: 2.0.0
|
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: 2022-10-21 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.
|
@@ -18,9 +18,11 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- ".codeclimate.yml"
|
21
22
|
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
22
23
|
- ".github/ISSUE_TEMPLATE/feature_request.md"
|
23
24
|
- ".github/dependabot.yml"
|
25
|
+
- ".github/workflows/codeql-analysis.yml"
|
24
26
|
- ".github/workflows/main.yml"
|
25
27
|
- ".github/workflows/perf.yml"
|
26
28
|
- ".gitignore"
|
@@ -28,12 +30,15 @@ files:
|
|
28
30
|
- ".yardopts"
|
29
31
|
- CHANGELOG.md
|
30
32
|
- CODE_OF_CONDUCT.md
|
33
|
+
- CONTRIBUTING.md
|
31
34
|
- Gemfile
|
35
|
+
- HACKING.md
|
32
36
|
- LICENSE.txt
|
33
37
|
- README.md
|
34
38
|
- Rakefile
|
35
39
|
- SECURITY.md
|
36
40
|
- alba.gemspec
|
41
|
+
- benchmark/README.md
|
37
42
|
- benchmark/collection.rb
|
38
43
|
- benchmark/single_resource.rb
|
39
44
|
- bin/console
|
@@ -41,28 +46,33 @@ files:
|
|
41
46
|
- codecov.yml
|
42
47
|
- docs/migrate_from_active_model_serializers.md
|
43
48
|
- docs/migrate_from_jbuilder.md
|
44
|
-
-
|
49
|
+
- docs/rails.md
|
45
50
|
- gemfiles/without_active_support.gemfile
|
46
51
|
- gemfiles/without_oj.gemfile
|
47
52
|
- lib/alba.rb
|
48
53
|
- lib/alba/association.rb
|
54
|
+
- lib/alba/conditional_attribute.rb
|
49
55
|
- lib/alba/default_inflector.rb
|
50
56
|
- lib/alba/deprecation.rb
|
51
|
-
- lib/alba/
|
52
|
-
- lib/alba/
|
53
|
-
- lib/alba/
|
57
|
+
- lib/alba/errors.rb
|
58
|
+
- lib/alba/layout.rb
|
59
|
+
- lib/alba/nested_attribute.rb
|
54
60
|
- lib/alba/resource.rb
|
55
61
|
- lib/alba/typed_attribute.rb
|
56
62
|
- lib/alba/version.rb
|
63
|
+
- logo/alba-card.png
|
64
|
+
- logo/alba-sign.png
|
65
|
+
- logo/alba-typography.png
|
57
66
|
- script/perf_check.rb
|
58
|
-
- sider.yml
|
59
67
|
homepage: https://github.com/okuramasafumi/alba
|
60
68
|
licenses:
|
61
69
|
- MIT
|
62
70
|
metadata:
|
63
|
-
|
64
|
-
source_code_uri: https://github.com/okuramasafumi/alba
|
71
|
+
bug_tracker_uri: https://github.com/okuramasafumi/issues
|
65
72
|
changelog_uri: https://github.com/okuramasafumi/alba/blob/main/CHANGELOG.md
|
73
|
+
documentation_uri: https://rubydoc.info/github/okuramasafumi/alba
|
74
|
+
source_code_uri: https://github.com/okuramasafumi/alba
|
75
|
+
rubygems_mfa_required: 'true'
|
66
76
|
post_install_message:
|
67
77
|
rdoc_options: []
|
68
78
|
require_paths:
|
@@ -71,14 +81,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
81
|
requirements:
|
72
82
|
- - ">="
|
73
83
|
- !ruby/object:Gem::Version
|
74
|
-
version: 2.
|
84
|
+
version: 2.6.0
|
75
85
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
86
|
requirements:
|
77
87
|
- - ">="
|
78
88
|
- !ruby/object:Gem::Version
|
79
89
|
version: '0'
|
80
90
|
requirements: []
|
81
|
-
rubygems_version: 3.
|
91
|
+
rubygems_version: 3.3.21
|
82
92
|
signing_key:
|
83
93
|
specification_version: 4
|
84
94
|
summary: Alba is the fastest JSON serializer for Ruby.
|
data/gemfiles/all.gemfile
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gem 'activesupport', require: false # For backend
|
4
|
-
gem 'ffaker', require: false # For testing
|
5
|
-
gem 'minitest', '~> 5.14' # For test
|
6
|
-
gem 'rake', '~> 13.0' # For test and automation
|
7
|
-
gem 'rubocop', '>= 0.79.0', require: false # For lint
|
8
|
-
gem 'rubocop-minitest', '~> 0.11.0', require: false # For lint
|
9
|
-
gem 'rubocop-performance', '~> 1.10.1', require: false # For lint
|
10
|
-
gem 'rubocop-rake', '>= 0.5.1', require: false # For lint
|
11
|
-
gem 'rubocop-sensible', '~> 0.3.0', require: false # For lint
|
12
|
-
gem 'simplecov', '~> 0.21.0', require: false # For test coverage
|
13
|
-
gem 'simplecov-cobertura', require: false # For test coverage
|
14
|
-
gem 'yard', require: false # For documentation
|
15
|
-
|
16
|
-
platforms :ruby do
|
17
|
-
gem 'oj', '~> 3.11', require: false # For backend
|
18
|
-
gem 'ruby-prof', require: false # For performance profiling
|
19
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module Alba
|
2
|
-
# This module creates key transform functions
|
3
|
-
module KeyTransformFactory
|
4
|
-
class << self
|
5
|
-
# Create key transform function for given transform_type
|
6
|
-
#
|
7
|
-
# @param transform_type [Symbol] transform type
|
8
|
-
# @return [Proc] transform function
|
9
|
-
# @raise [Alba::Error] when transform_type is not supported
|
10
|
-
def create(transform_type)
|
11
|
-
case transform_type
|
12
|
-
when :camel
|
13
|
-
->(key) { _inflector.camelize(key) }
|
14
|
-
when :lower_camel
|
15
|
-
->(key) { _inflector.camelize_lower(key) }
|
16
|
-
when :dash
|
17
|
-
->(key) { _inflector.dasherize(key) }
|
18
|
-
else
|
19
|
-
raise ::Alba::Error, "Unknown transform_type: #{transform_type}. Supported transform_type are :camel, :lower_camel and :dash."
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def _inflector
|
26
|
-
Alba.inflector || begin
|
27
|
-
require_relative './default_inflector'
|
28
|
-
Alba::DefaultInflector
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
data/lib/alba/many.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require_relative 'association'
|
2
|
-
|
3
|
-
module Alba
|
4
|
-
# Representing many association
|
5
|
-
class Many < Association
|
6
|
-
# Recursively converts objects into an Array of Hashes
|
7
|
-
#
|
8
|
-
# @param target [Object] the object having an association method
|
9
|
-
# @param within [Hash] determines what associations to be serialized. If not set, it serializes all associations.
|
10
|
-
# @param params [Hash] user-given Hash for arbitrary data
|
11
|
-
# @return [Array<Hash>]
|
12
|
-
def to_hash(target, within: nil, params: {})
|
13
|
-
@object = target.public_send(@name)
|
14
|
-
@object = @condition.call(@object, params) if @condition
|
15
|
-
return if @object.nil?
|
16
|
-
|
17
|
-
@resource = constantize(@resource)
|
18
|
-
@resource.new(@object, params: params, within: within).to_hash
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/lib/alba/one.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require_relative 'association'
|
2
|
-
|
3
|
-
module Alba
|
4
|
-
# Representing one association
|
5
|
-
class One < Association
|
6
|
-
# Recursively converts an object into a Hash
|
7
|
-
#
|
8
|
-
# @param target [Object] the object having an association method
|
9
|
-
# @param within [Hash] determines what associations to be serialized. If not set, it serializes all associations.
|
10
|
-
# @param params [Hash] user-given Hash for arbitrary data
|
11
|
-
# @return [Hash]
|
12
|
-
def to_hash(target, within: nil, params: {})
|
13
|
-
@object = target.public_send(@name)
|
14
|
-
@object = @condition.call(object, params) if @condition
|
15
|
-
return if @object.nil?
|
16
|
-
|
17
|
-
@resource = constantize(@resource)
|
18
|
-
@resource.new(object, params: params, within: within).to_hash
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/sider.yml
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
linter:
|
2
|
-
# # https://help.sider.review/getting-started/custom-configuration
|
3
|
-
|
4
|
-
# # https://help.sider.review/tools/ruby/rubocop
|
5
|
-
rubocop:
|
6
|
-
gems:
|
7
|
-
- "rubocop-minitest"
|
8
|
-
- "rubocop-performance"
|
9
|
-
- "rubocop-sensible"
|
10
|
-
- "rubocop-rake"
|
11
|
-
safe: false
|
12
|
-
|
13
|
-
# # https://help.sider.review/tools/ruby/reek
|
14
|
-
# reek:
|
15
|
-
# gems:
|
16
|
-
# - name: "reek"
|
17
|
-
# version: "5.2.0"
|
18
|
-
# target:
|
19
|
-
# - lib/
|
20
|
-
# - test/
|
21
|
-
# config: config/.reek.yml
|
22
|
-
|
23
|
-
# # https://help.sider.review/tools/ruby/querly
|
24
|
-
# querly:
|
25
|
-
# gems:
|
26
|
-
# - "slim"
|
27
|
-
|
28
|
-
# # https://help.sider.review/tools/others/misspell
|
29
|
-
# misspell:
|
30
|
-
# exclude:
|
31
|
-
# - vendor
|
32
|
-
# - "**/*.min.js"
|
33
|
-
# - exclude_file.rb
|
34
|
-
# targets:
|
35
|
-
# - target_directory
|
36
|
-
# - another_target_directory/foo.rb
|
37
|
-
# - bar.rb
|
38
|
-
# locale: UK
|
39
|
-
# ignore: center,behavior
|
40
|
-
|
41
|
-
# # https://help.sider.review/tools/shellscript/shellcheck
|
42
|
-
# shellcheck:
|
43
|
-
# target: "src/**/*.{sh,bash}"
|
44
|
-
# include: "SC2104,SC2105"
|
45
|
-
# exclude: "SC1000,SC1118"
|
46
|
-
# enable: "all"
|
47
|
-
# shell: "bash"
|
48
|
-
# severity: "error"
|
49
|
-
# norc: true
|
50
|
-
|
51
|
-
# # https://help.sider.review/getting-started/custom-configuration#ignore
|
52
|
-
ignore:
|
53
|
-
- 'test/**/*'
|
54
|
-
|
55
|
-
# # https://help.sider.review/getting-started/custom-configuration#branchesexclude
|
56
|
-
# branches:
|
57
|
-
# exclude:
|
58
|
-
# - master
|
59
|
-
# - development
|
60
|
-
# - /^release-.*$/
|