alba 0.12.0 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +32 -0
- data/lib/alba/key_transformer.rb +31 -0
- data/lib/alba/resource.rb +34 -13
- data/lib/alba/serializer.rb +3 -3
- data/lib/alba/version.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: f4a5b866825838f457bb17c0c1037f68c3e88a9b96c86dd9e0c456d101f69158
|
4
|
+
data.tar.gz: d1f451fff558385b3ae4610355a3abb9aced052ce138eff9913169a12c8a23c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71fdab1911c6fc9e1ba88e8cbdc553311b99aecd975462b05958e5be1e491aa10734b6b6f6f15f24ae98c8ab5a1b1021d6c544a172befcedd23ca80283529e98
|
7
|
+
data.tar.gz: 05f06f2e7f3ec93e0a616bec6e3d71a90590b68c7ce397050bb9c019d2bbf206b070ed1cf44603cd301fa5dd2634df50c4e08b75a8c478b2f0063b6551b5bdcc
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -221,6 +221,38 @@ RestrictedFooResouce.new(foo).serialize
|
|
221
221
|
end
|
222
222
|
```
|
223
223
|
|
224
|
+
### Attribute key transformation
|
225
|
+
|
226
|
+
** Note: You need to install `active_support` gem to use `transform_keys` DSL.
|
227
|
+
|
228
|
+
With `active_support` installed, you can transform attribute keys.
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
class User
|
232
|
+
attr_reader :id, :first_name, :last_name
|
233
|
+
|
234
|
+
def initialize(id, first_name, last_name)
|
235
|
+
@id = id
|
236
|
+
@first_name = first_name
|
237
|
+
@last_name = last_name
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
class UserResource
|
242
|
+
include Alba::Resource
|
243
|
+
|
244
|
+
attributes :id, :first_name, :last_name
|
245
|
+
|
246
|
+
transform_keys :lower_camel
|
247
|
+
end
|
248
|
+
|
249
|
+
user = User.new(1, 'Masafumi', 'Okura')
|
250
|
+
UserResourceCamel.new(user).serialize
|
251
|
+
# => '{"id":1,"firstName":"Masafumi","lastName":"Okura"}'
|
252
|
+
```
|
253
|
+
|
254
|
+
Supported transformation types are :camel, :lower_camel and :dash.
|
255
|
+
|
224
256
|
## Comparison
|
225
257
|
|
226
258
|
Alba is faster than alternatives.
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Alba
|
2
|
+
# Transform keys using `ActiveSupport::Inflector`
|
3
|
+
module KeyTransformer
|
4
|
+
begin
|
5
|
+
require 'active_support/inflector'
|
6
|
+
rescue LoadError
|
7
|
+
raise ::Alba::Error, 'To use transform_keys, please install `ActiveSupport` gem.'
|
8
|
+
end
|
9
|
+
|
10
|
+
module_function
|
11
|
+
|
12
|
+
# Transform key as given transform_type
|
13
|
+
#
|
14
|
+
# @params key [String] key to be transformed
|
15
|
+
# @params transform_type [Symbol] transform type
|
16
|
+
# @return [String] transformed key
|
17
|
+
# @raise [Alba::Error] when transform_type is not supported
|
18
|
+
def transform(key, transform_type)
|
19
|
+
case transform_type
|
20
|
+
when :camel
|
21
|
+
ActiveSupport::Inflector.camelize(key)
|
22
|
+
when :lower_camel
|
23
|
+
ActiveSupport::Inflector.camelize(key, false)
|
24
|
+
when :dash
|
25
|
+
ActiveSupport::Inflector.dasherize(key)
|
26
|
+
else
|
27
|
+
raise ::Alba::Error, "Unknown transform_type: #{transform_type}. Supported transform_type are :camel and :dash."
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/alba/resource.rb
CHANGED
@@ -7,7 +7,7 @@ module Alba
|
|
7
7
|
module Resource
|
8
8
|
# @!parse include InstanceMethods
|
9
9
|
# @!parse extend ClassMethods
|
10
|
-
DSLS = {_attributes: {}, _serializer: nil, _key: nil}.freeze
|
10
|
+
DSLS = {_attributes: {}, _serializer: nil, _key: nil, _transform_keys: nil}.freeze
|
11
11
|
private_constant :DSLS
|
12
12
|
|
13
13
|
# @private
|
@@ -68,20 +68,34 @@ module Alba
|
|
68
68
|
|
69
69
|
private
|
70
70
|
|
71
|
+
# rubocop:disable Style/MethodCalledOnDoEndBlock
|
71
72
|
def converter
|
72
73
|
lambda do |resource|
|
73
|
-
@_attributes.
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
74
|
+
@_attributes.map do |key, attribute|
|
75
|
+
[transform_key(key), fetch_attribute(resource, attribute)]
|
76
|
+
end.to_h
|
77
|
+
end
|
78
|
+
end
|
79
|
+
# rubocop:enable Style/MethodCalledOnDoEndBlock
|
80
|
+
|
81
|
+
# Override this method to supply custom key transform method
|
82
|
+
def transform_key(key)
|
83
|
+
return key unless @_transform_keys
|
84
|
+
|
85
|
+
require_relative 'key_transformer'
|
86
|
+
KeyTransformer.transform(key, @_transform_keys)
|
87
|
+
end
|
88
|
+
|
89
|
+
def fetch_attribute(resource, attribute)
|
90
|
+
case attribute
|
91
|
+
when Symbol
|
92
|
+
resource.public_send attribute
|
93
|
+
when Proc
|
94
|
+
instance_exec(resource, &attribute)
|
95
|
+
when Alba::One, Alba::Many
|
96
|
+
attribute.to_hash(resource, params: params)
|
97
|
+
else
|
98
|
+
raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
|
85
99
|
end
|
86
100
|
end
|
87
101
|
|
@@ -179,6 +193,13 @@ module Alba
|
|
179
193
|
@_attributes.delete(attr_name.to_sym)
|
180
194
|
end
|
181
195
|
end
|
196
|
+
|
197
|
+
# Transform keys as specified type
|
198
|
+
#
|
199
|
+
# @params type [String, Symbol]
|
200
|
+
def transform_keys(type)
|
201
|
+
@_transform_keys = type.to_sym
|
202
|
+
end
|
182
203
|
end
|
183
204
|
end
|
184
205
|
end
|
data/lib/alba/serializer.rb
CHANGED
@@ -22,10 +22,10 @@ module Alba
|
|
22
22
|
@resource = resource
|
23
23
|
@hash = resource.serializable_hash
|
24
24
|
@hash = {key.to_sym => @hash} if key
|
25
|
+
return if metadata.empty?
|
26
|
+
|
25
27
|
# @hash is either Hash or Array
|
26
|
-
|
27
|
-
@hash.is_a?(Hash) ? @hash.merge!(metadata.to_h) : @hash << metadata
|
28
|
-
end
|
28
|
+
@hash.is_a?(Hash) ? @hash.merge!(metadata.to_h) : @hash << metadata
|
29
29
|
end
|
30
30
|
|
31
31
|
# Use real encoder to actually serialize to JSON
|
data/lib/alba/version.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: 0.
|
4
|
+
version: 0.13.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: 2021-01-
|
11
|
+
date: 2021-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Alba is designed to be a simple, easy to use and fast alternative to
|
14
14
|
existing JSON serializers. Its performance is better than almost all gems which
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- bin/setup
|
35
35
|
- lib/alba.rb
|
36
36
|
- lib/alba/association.rb
|
37
|
+
- lib/alba/key_transformer.rb
|
37
38
|
- lib/alba/many.rb
|
38
39
|
- lib/alba/one.rb
|
39
40
|
- lib/alba/resource.rb
|