active_serialize 2.4.2 → 2.5.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
  SHA1:
3
- metadata.gz: c37b91e388b4825a96e1054e39d9c3df6bf98a50
4
- data.tar.gz: 37c32696a41aa300b79ab9119a9d5e697b71ff0d
3
+ metadata.gz: 367ae37d7d28d38e8b6ad1a449590ddb72b42a94
4
+ data.tar.gz: 527f9f6c0bbbb718aa4c88229088237918429465
5
5
  SHA512:
6
- metadata.gz: 9f3b807ed104b10aeb173193cb34de6d6d4bf9255c6c790fdc3882694af04720187bd60e5e61d19ed1623b1cb153d15d317b8c425daf027edee5d44d87765cee
7
- data.tar.gz: 788348b34876bdf18be7dbd59d02f081f88ad36ad9dbe10da56a1797d077ba47ddba65a2f8f575296cb1c080554f078f74c74c41b71084f8fa97e35bd43443ea
6
+ metadata.gz: 0cea92163449a43f483e950a4df97bafbdcd351a2851cfe0cec9be18f6d7a18fc8c258e2f595d656ca65b170f61647b8ffbfc40bd1fd33973cd1d9f509643622
7
+ data.tar.gz: 2b9c1d407792742d64270a95c79bdda36690c5605cf3d00e05a2e80465ab644df05b8470cf67969ae56967435ba24087a6e33b69fd0a1673a415b004f7b000c8
@@ -28,10 +28,12 @@ module ActiveSerialize
28
28
  _active_serialize[:map].merge! settings
29
29
  end
30
30
 
31
- def active_serialize_keys(*groups, rmv: [ ], add: [ ])
31
+ def active_serialize_keys(*groups, rmv: [ ], add: [ ], only: nil, **opts)
32
+ return only if only
32
33
  _active_serialize[:final] ||= column_names.map(&:to_sym) - _active_serialize[:rmv] + _active_serialize[:add]
33
- _active_serialize[:final] - Array(rmv) + Array(add) +
34
+ result = _active_serialize[:final] - Array(rmv) + Array(add) +
34
35
  _active_serialize[:groups].values_at(*groups).flatten.compact.uniq
36
+ opts[:if] ? { normal: result, if: opts[:if] } : result
35
37
  end
36
38
  end
37
39
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveSerialize
4
- VERSION = '2.4.2'
4
+ VERSION = '2.5.0'
5
5
  end
@@ -31,13 +31,19 @@ module ActiveSerialize
31
31
  end
32
32
 
33
33
  module ToH
34
- def to_h(*groups, rmv: [ ], add: [ ], flat_add: [ ], recursive: [ ], plucked: { }, merge: { })
34
+ def to_h(*groups, rmv: [ ], add: [ ], flat_add: [ ], recursive: [ ], plucked: { }, merge: { }, **opts)
35
35
  recursion = (_active_serialize[:recursive] + recursive).map { |k| [ k, public_send(k)&.to_ha(*groups) ] }.to_h
36
36
  merge.merge!(flat_add.map(&method(:public_send)).reduce({ }, :merge)) if flat_add.present?
37
+ keys = active_serialize_keys(*groups, rmv: rmv, add: add, **opts)
38
+ if keys.is_a?(Hash) && instance_exec(&keys[:if].first)
39
+ keys = keys[:if].last[:only] ||
40
+ keys[:normal] + (keys[:if].last[:add] || [ ]) - (keys[:if].last[:rmv] || [ ])
41
+ else
42
+ keys = keys[:normal]
43
+ end
37
44
 
38
45
  KeyFormatter.(_active_serialize[:key_format],
39
- active_serialize_keys(*groups, rmv: rmv, add: add)
40
- .map { |key| [ key, public_send(key) ] }.to_h
46
+ keys.map { |key| [ key, public_send(key) ] }.to_h
41
47
  .merge(plucked).merge(recursion).merge(merge)
42
48
  .transform_keys { |key| _active_serialize[:map][key] || key }
43
49
  .deep_stringify_keys!
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.4.2
4
+ version: 2.5.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-04-24 00:00:00.000000000 Z
11
+ date: 2019-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler