active_serialize 2.2.0 → 2.3.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
2
  SHA256:
3
- metadata.gz: 4583050b756d16c61d563739d9f9f649f129b03688e14e28406693d9561cfabe
4
- data.tar.gz: 0be0bf8aa6b67bb4f5a33db5793ba951c55f53876e141ddb345de703606a2568
3
+ metadata.gz: c849c25a25fa9fefa132e1087b7aad46a7282d2a8257a3bb8fc58cf1b82752ab
4
+ data.tar.gz: dbc355538fcb2d836b6c3efcc6fe6a46ce6b21645255ab5d60124153ad587c38
5
5
  SHA512:
6
- metadata.gz: 79e10e535ee0575de0e4492770a889b310b0c79a442929018d48da7c499776acbddb0844e8e2cd909fcd1d37268b82490a25caa39e75dd086e63ffd84379f798
7
- data.tar.gz: cd94306cd22166cd9ef48beaa89ecaf1d7d0a2f157bf111fc73db9def897493b618b15fc96176a046867569f1dddc8fc3a99ffd2bc3778eeb8f461afcf8d2a21
6
+ metadata.gz: 53931320e6446ada93e5c4dcfae310f8844a3ade90f754a337e11e6c74395c92d93affcceb0a398f325974ebf31b01d4c864b23190d02e04ff99e61f79368a93
7
+ data.tar.gz: afd509fc0026f7a00a31cbcf9f22d11b4e004f633092f1f535041b4fd9eed6094070c09d0fb3d1ab366891c430e98ecc9d47bfdcdf8987738e13fadfcbaaeb1e
data/README.md CHANGED
@@ -108,6 +108,18 @@ User.first.to_h
108
108
  # => { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx", "books" => [{ "name" => "Rails Guide" }] }
109
109
  ```
110
110
 
111
+ ### Add attributes only when passing the specified `group` key
112
+
113
+ Like the example below:
114
+
115
+ ```ruby
116
+ User.active_serialize_add :love, group: :abcd
117
+ # Then:
118
+ User.first.to_h.keys.include?('love') # => false
119
+ User.first.to_h(:abcd).keys.include('love') # => true
120
+ ```
121
+
122
+
111
123
  ### Transform key names
112
124
 
113
125
  Choose one of the following ways:
@@ -3,7 +3,7 @@
3
3
  module ActiveSerialize
4
4
  module ClassMethods
5
5
  def _active_serialize
6
- ActiveSerialize.configs[name] ||= { **ActiveSerialize.configs[:default].deep_dup, final: nil, recursive: [ ], map: { } }
6
+ ActiveSerialize.configs[name] ||= { **ActiveSerialize.configs[:default].deep_dup, final: nil, groups: { }, recursive: [ ], map: { } }
7
7
  end
8
8
 
9
9
  def to_ha(**args)
@@ -14,8 +14,9 @@ module ActiveSerialize
14
14
  _active_serialize[:rmv].concat attrs.map(&:to_sym)
15
15
  end
16
16
 
17
- def active_serialize_add *attrs, named: nil, recursive: false
17
+ def active_serialize_add *attrs, named: nil, recursive: false, group: nil
18
18
  active_serialize_map attrs[0] => named if named
19
+ return _active_serialize[:groups][group] = attrs.map(&:to_sym) if group
19
20
  _active_serialize[recursive ? :recursive : :add].concat attrs.map(&:to_sym)
20
21
  end
21
22
 
@@ -23,9 +24,10 @@ module ActiveSerialize
23
24
  _active_serialize[:map].merge! settings
24
25
  end
25
26
 
26
- def active_serialize_keys(rmv: [ ], add: [ ])
27
+ def active_serialize_keys(*groups, rmv: [ ], add: [ ])
27
28
  _active_serialize[:final] ||= column_names.map(&:to_sym) - _active_serialize[:rmv] + _active_serialize[:add]
28
- _active_serialize[:final] - Array(rmv) + Array(add)
29
+ _active_serialize[:final] - Array(rmv) + Array(add) +
30
+ _active_serialize[:groups].values_at(*groups).flatten.compact.uniq
29
31
  end
30
32
  end
31
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveSerialize
4
- VERSION = '2.2.0'
4
+ VERSION = '2.3.0'
5
5
  end
@@ -28,13 +28,13 @@ module ActiveSerialize
28
28
  end
29
29
 
30
30
  module ToH
31
- def to_h(rmv: [ ], add: [ ], merge: { })
32
- tran_key = ->(key) { (_active_serialize[:map][key] || key).to_s }
31
+ def to_h(*groups, rmv: [ ], add: [ ], merge: { })
32
+ tran_key = ->(key) { _active_serialize[:map][key] || key }
33
33
  recursion = _active_serialize[:recursive].map { |key| [ tran_key.(key), public_send(key)&.to_ha ] }.to_h
34
34
  KeyFormatter.(_active_serialize[:key_format],
35
- active_serialize_keys(rmv: rmv, add: add)
35
+ active_serialize_keys(*groups, rmv: rmv, add: add)
36
36
  .map { |key| [ tran_key.(key), public_send(key) ] }.to_h
37
- .merge(merge.deep_stringify_keys!).merge(recursion)
37
+ .merge(merge).merge(recursion).deep_stringify_keys!
38
38
  )
39
39
  end
40
40
 
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.2.0
4
+ version: 2.3.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-02-06 00:00:00.000000000 Z
11
+ date: 2019-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler