alba 1.5.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/codeql-analysis.yml +70 -0
- data/.github/workflows/main.yml +3 -1
- data/.rubocop.yml +2 -0
- data/CHANGELOG.md +10 -0
- data/Gemfile +2 -2
- data/README.md +240 -92
- data/alba.gemspec +7 -3
- data/benchmark/collection.rb +60 -4
- data/benchmark/single_resource.rb +33 -2
- data/gemfiles/all.gemfile +1 -0
- data/lib/alba/association.rb +28 -8
- data/lib/alba/default_inflector.rb +19 -1
- data/lib/alba/errors.rb +10 -0
- data/lib/alba/resource.rb +117 -61
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +44 -16
- metadata +9 -8
- data/lib/alba/key_transform_factory.rb +0 -33
- data/lib/alba/many.rb +0 -21
- data/lib/alba/one.rb +0 -21
data/lib/alba.rb
CHANGED
@@ -1,19 +1,11 @@
|
|
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
10
|
attr_reader :backend, :encoder, :inferring, :_on_error, :_on_nil, :transforming_root_key
|
19
11
|
|
@@ -60,12 +52,16 @@ module Alba
|
|
60
52
|
end
|
61
53
|
|
62
54
|
# Enable inference for key and resource name
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
55
|
+
#
|
56
|
+
# @param with [Symbol, Class, Module] inflector
|
57
|
+
# When it's a Symbol, it sets inflector with given name
|
58
|
+
# When it's a Class or a Module, it sets given object to inflector
|
59
|
+
def enable_inference!(with: :default)
|
60
|
+
if with == :default
|
61
|
+
Alba::Deprecation.warn 'Calling `enable_inference!` without `with` keyword argument is deprecated. Pass `:active_support` to keep current behavior.'
|
68
62
|
end
|
63
|
+
|
64
|
+
@inflector = inflector_from(with)
|
69
65
|
@inferring = true
|
70
66
|
end
|
71
67
|
|
@@ -80,8 +76,10 @@ module Alba
|
|
80
76
|
# @param [Block]
|
81
77
|
# @raise [ArgumentError] if both handler and block params exist
|
82
78
|
# @raise [ArgumentError] if both handler and block params don't exist
|
79
|
+
# @deprecated Use `Resource.on_error` instead
|
83
80
|
# @return [void]
|
84
81
|
def on_error(handler = nil, &block)
|
82
|
+
Alba::Deprecation.warn '`Alba.on_error` is deprecated, use `on_error` on resource class instead.'
|
85
83
|
raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block
|
86
84
|
raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block
|
87
85
|
|
@@ -92,17 +90,25 @@ module Alba
|
|
92
90
|
#
|
93
91
|
# @param block [Block]
|
94
92
|
# @return [void]
|
93
|
+
# @deprecated Use `Resource.on_nil` instead
|
95
94
|
def on_nil(&block)
|
95
|
+
Alba::Deprecation.warn '`Alba.on_nil` is deprecated, use `on_nil` on resource class instead.'
|
96
96
|
@_on_nil = block
|
97
97
|
end
|
98
98
|
|
99
99
|
# Enable root key transformation
|
100
|
+
#
|
101
|
+
# @deprecated Use `Resource.transform_keys` with `root` option instead
|
100
102
|
def enable_root_key_transformation!
|
103
|
+
Alba::Deprecation.warn '`Alba.enable_root_key_transformation!` is deprecated, use `transform_keys` on resource class instead.'
|
101
104
|
@transforming_root_key = true
|
102
105
|
end
|
103
106
|
|
104
107
|
# Disable root key transformation
|
108
|
+
#
|
109
|
+
# @deprecated Use `Resource.transform_keys` with `root` option instead
|
105
110
|
def disable_root_key_transformation!
|
111
|
+
Alba::Deprecation.warn '`Alba.disable_root_key_transformation!` is deprecated, use `transform_keys` on resource class instead.'
|
106
112
|
@transforming_root_key = false
|
107
113
|
end
|
108
114
|
|
@@ -119,9 +125,10 @@ module Alba
|
|
119
125
|
# @param nesting [String, nil] namespace Alba tries to find resource class in
|
120
126
|
# @return [Class<Alba::Resource>] resource class
|
121
127
|
def infer_resource_class(name, nesting: nil)
|
122
|
-
enable_inference
|
128
|
+
raise Alba::Error, 'Inference is disabled so Alba cannot infer resource name. Use `Alba.enable_inference!` before use.' unless Alba.inferring
|
129
|
+
|
123
130
|
const_parent = nesting.nil? ? Object : Object.const_get(nesting)
|
124
|
-
const_parent.const_get("#{
|
131
|
+
const_parent.const_get("#{inflector.classify(name)}Resource")
|
125
132
|
end
|
126
133
|
|
127
134
|
# Reset config variables
|
@@ -135,6 +142,19 @@ module Alba
|
|
135
142
|
|
136
143
|
private
|
137
144
|
|
145
|
+
def inflector_from(name_or_module)
|
146
|
+
case name_or_module
|
147
|
+
when :default, :active_support
|
148
|
+
require_relative 'alba/default_inflector'
|
149
|
+
Alba::DefaultInflector
|
150
|
+
when :dry
|
151
|
+
require 'dry/inflector'
|
152
|
+
Dry::Inflector.new
|
153
|
+
else
|
154
|
+
validate_inflector(name_or_module)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
138
158
|
def set_encoder_from_backend
|
139
159
|
@encoder = case @backend
|
140
160
|
when :oj, :oj_strict then try_oj
|
@@ -167,6 +187,14 @@ module Alba
|
|
167
187
|
JSON.dump(hash)
|
168
188
|
end
|
169
189
|
end
|
190
|
+
|
191
|
+
def validate_inflector(inflector)
|
192
|
+
unless %i[camelize camelize_lower dasherize classify].all? { |m| inflector.respond_to?(m) }
|
193
|
+
raise Alba::Error, "Given inflector, #{inflector.inspect} is not valid. It must implement `camelize`, `camelize_lower`, `dasherize` and `classify`."
|
194
|
+
end
|
195
|
+
|
196
|
+
inflector
|
197
|
+
end
|
170
198
|
end
|
171
199
|
|
172
200
|
reset!
|
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: 1.
|
4
|
+
version: 1.6.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-03-15 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.
|
@@ -21,6 +21,7 @@ files:
|
|
21
21
|
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
22
22
|
- ".github/ISSUE_TEMPLATE/feature_request.md"
|
23
23
|
- ".github/dependabot.yml"
|
24
|
+
- ".github/workflows/codeql-analysis.yml"
|
24
25
|
- ".github/workflows/main.yml"
|
25
26
|
- ".github/workflows/perf.yml"
|
26
27
|
- ".gitignore"
|
@@ -48,9 +49,7 @@ files:
|
|
48
49
|
- lib/alba/association.rb
|
49
50
|
- lib/alba/default_inflector.rb
|
50
51
|
- lib/alba/deprecation.rb
|
51
|
-
- lib/alba/
|
52
|
-
- lib/alba/many.rb
|
53
|
-
- lib/alba/one.rb
|
52
|
+
- lib/alba/errors.rb
|
54
53
|
- lib/alba/resource.rb
|
55
54
|
- lib/alba/typed_attribute.rb
|
56
55
|
- lib/alba/version.rb
|
@@ -60,9 +59,11 @@ homepage: https://github.com/okuramasafumi/alba
|
|
60
59
|
licenses:
|
61
60
|
- MIT
|
62
61
|
metadata:
|
63
|
-
|
64
|
-
source_code_uri: https://github.com/okuramasafumi/alba
|
62
|
+
bug_tracker_uri: https://github.com/okuramasafumi/issues
|
65
63
|
changelog_uri: https://github.com/okuramasafumi/alba/blob/main/CHANGELOG.md
|
64
|
+
documentation_uri: https://rubydoc.info/github/okuramasafumi/alba
|
65
|
+
source_code_uri: https://github.com/okuramasafumi/alba
|
66
|
+
rubygems_mfa_required: 'true'
|
66
67
|
post_install_message:
|
67
68
|
rdoc_options: []
|
68
69
|
require_paths:
|
@@ -78,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
79
|
- !ruby/object:Gem::Version
|
79
80
|
version: '0'
|
80
81
|
requirements: []
|
81
|
-
rubygems_version: 3.
|
82
|
+
rubygems_version: 3.3.5
|
82
83
|
signing_key:
|
83
84
|
specification_version: 4
|
84
85
|
summary: Alba is the fastest JSON serializer for Ruby.
|
@@ -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
|