active_serialize 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 07f4f8ee2b874c8c7bac64edc87f7a174fc56465
4
- data.tar.gz: 7fe6d3bb7464b9964d97d336ffebb25aec319b06
2
+ SHA256:
3
+ metadata.gz: 4583050b756d16c61d563739d9f9f649f129b03688e14e28406693d9561cfabe
4
+ data.tar.gz: 0be0bf8aa6b67bb4f5a33db5793ba951c55f53876e141ddb345de703606a2568
5
5
  SHA512:
6
- metadata.gz: d7928027949f5e0b9bafa52105f43bffb6fd86eea3ae8077b8dcb422b0ede89e02a100ecb461753b8dcd580854002bb1f684bc029691776c6518387e101f4479
7
- data.tar.gz: ef414deffd617467ae4352a6c514384153aacd74adb5406a7386d57431427538061a0632b91c8c562fffd1893e709d257c1fae36540dd841f9ef48ded6539ae8
6
+ metadata.gz: 79e10e535ee0575de0e4492770a889b310b0c79a442929018d48da7c499776acbddb0844e8e2cd909fcd1d37268b82490a25caa39e75dd086e63ffd84379f798
7
+ data.tar.gz: cd94306cd22166cd9ef48beaa89ecaf1d7d0a2f157bf111fc73db9def897493b618b15fc96176a046867569f1dddc8fc3a99ffd2bc3778eeb8f461afcf8d2a21
data/README.md CHANGED
@@ -117,6 +117,13 @@ Choose one of the following ways:
117
117
 
118
118
  `=> { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx", "looove" => "Ruby" }`
119
119
 
120
+ ### Transform key format
121
+
122
+ 1. set as default: `active_serialize_default key_format: ...`
123
+ 2. only effect for a model: `active_serialize key_format: ...`
124
+
125
+ Optional value: `underscore / camelize / camelize_lower`
126
+
120
127
  ## Development
121
128
 
122
129
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -3,18 +3,20 @@
3
3
  require 'active_record'
4
4
 
5
5
  require 'active_serialize/version'
6
+ require 'active_serialize/key_formatter'
6
7
  require 'active_serialize/class_methods'
7
8
 
8
9
  module ActiveSerialize
9
10
  extend ActiveSupport::Concern
10
- cattr_accessor :configs, default: { default: { rmv: [ ], add: [ ] } }
11
+ cattr_accessor :configs, default: { default: { rmv: [ ], add: [ ], key_format: nil } }
11
12
 
12
13
  class_methods do
13
- def active_serialize rmv: [ ], add: [ ], recursive: [ ]
14
+ def active_serialize rmv: [ ], add: [ ], recursive: [ ], **configs
14
15
  extend ClassMethods
15
16
  include ToH
16
17
  delegate :active_serialize_keys, :_active_serialize, to: self
17
18
 
19
+ _active_serialize.merge!(configs)
18
20
  active_serialize_rmv *Array(rmv)
19
21
  active_serialize_add *Array(add)
20
22
  active_serialize_add *Array(recursive), recursive: true
@@ -29,10 +31,14 @@ module ActiveSerialize
29
31
  def to_h(rmv: [ ], add: [ ], merge: { })
30
32
  tran_key = ->(key) { (_active_serialize[:map][key] || key).to_s }
31
33
  recursion = _active_serialize[:recursive].map { |key| [ tran_key.(key), public_send(key)&.to_ha ] }.to_h
32
- active_serialize_keys(rmv: rmv, add: add)
33
- .map{ |key| [ tran_key.(key), public_send(key) ] }.to_h
34
- .merge(merge).merge(recursion)
34
+ KeyFormatter.(_active_serialize[:key_format],
35
+ active_serialize_keys(rmv: rmv, add: add)
36
+ .map { |key| [ tran_key.(key), public_send(key) ] }.to_h
37
+ .merge(merge.deep_stringify_keys!).merge(recursion)
38
+ )
35
39
  end
40
+
41
+ alias to_ha to_h
36
42
  end
37
43
  end
38
44
 
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSerialize
4
+ module KeyFormatter
5
+ def self.call(config, hash)
6
+ return hash unless config.present?
7
+ hash.deep_transform_keys! do |k, _|
8
+ case config.to_sym
9
+ when :underscore; k.underscore
10
+ when :camelize; k.camelize
11
+ when :camelize_lower; k.camelize(:lower)
12
+ else k
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveSerialize
4
- VERSION = '2.1.0'
4
+ VERSION = '2.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_serialize
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhandao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-31 00:00:00.000000000 Z
11
+ date: 2019-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -184,6 +184,7 @@ files:
184
184
  - bin/setup
185
185
  - lib/active_serialize.rb
186
186
  - lib/active_serialize/class_methods.rb
187
+ - lib/active_serialize/key_formatter.rb
187
188
  - lib/active_serialize/version.rb
188
189
  homepage: https://github.com/ikkiuchi/active_serialize
189
190
  licenses:
@@ -205,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
206
  version: '0'
206
207
  requirements: []
207
208
  rubyforge_project:
208
- rubygems_version: 2.6.12
209
+ rubygems_version: 2.7.3
209
210
  signing_key:
210
211
  specification_version: 4
211
212
  summary: Provide a very simple way to transform ActiveRecord data into Hash output.