alba 0.12.0 → 0.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee2f8d482fedb98083c2acab37b425c7ecedb429ab7bae7698b4eea7949635d9
4
- data.tar.gz: 1a44f23d425072253ba6743e18c6da9d8fa81de448b58720848c4e9667b12bfb
3
+ metadata.gz: f4a5b866825838f457bb17c0c1037f68c3e88a9b96c86dd9e0c456d101f69158
4
+ data.tar.gz: d1f451fff558385b3ae4610355a3abb9aced052ce138eff9913169a12c8a23c6
5
5
  SHA512:
6
- metadata.gz: 9bf421fcd99f06cb56499ee31c88dda019e004c58a1880c03326f2a1ce395f7dc4b64be7cee012169c7039530194e85a312b3c53d964b1aaf5288cdf8cb78ae4
7
- data.tar.gz: 02f20dd839f72f9d2c1859b48470595fff263939f41421e240d74452677edae2dc0569078823984144f630cbc2e6000b6ac3bd0d4161a9ef65bd95dbf311294f
6
+ metadata.gz: 71fdab1911c6fc9e1ba88e8cbdc553311b99aecd975462b05958e5be1e491aa10734b6b6f6f15f24ae98c8ab5a1b1021d6c544a172befcedd23ca80283529e98
7
+ data.tar.gz: 05f06f2e7f3ec93e0a616bec6e3d71a90590b68c7ce397050bb9c019d2bbf206b070ed1cf44603cd301fa5dd2634df50c4e08b75a8c478b2f0063b6551b5bdcc
@@ -42,6 +42,10 @@ Style/ConstantVisibility:
42
42
  Style/Copyright:
43
43
  Enabled: false
44
44
 
45
+ # I know what I do :)
46
+ Style/DisableCopsWithinSourceCodeDirective:
47
+ Enabled: false
48
+
45
49
  Style/FrozenStringLiteralComment:
46
50
  Enabled: false
47
51
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- alba (0.12.0)
4
+ alba (0.13.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
@@ -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.transform_values do |attribute|
74
- case attribute
75
- when Symbol
76
- resource.public_send attribute
77
- when Proc
78
- instance_exec(resource, &attribute)
79
- when Alba::One, Alba::Many
80
- attribute.to_hash(resource, params: params)
81
- else
82
- raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
83
- end
84
- end
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
@@ -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
- unless metadata.empty?
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
@@ -1,3 +1,3 @@
1
1
  module Alba
2
- VERSION = '0.12.0'.freeze
2
+ VERSION = '0.13.0'.freeze
3
3
  end
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.12.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-20 00:00:00.000000000 Z
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