active_serialize 2.3.0 → 2.4.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
- SHA256:
3
- metadata.gz: c849c25a25fa9fefa132e1087b7aad46a7282d2a8257a3bb8fc58cf1b82752ab
4
- data.tar.gz: dbc355538fcb2d836b6c3efcc6fe6a46ce6b21645255ab5d60124153ad587c38
2
+ SHA1:
3
+ metadata.gz: 3d9b2859163166929c779368b00c17271635964c
4
+ data.tar.gz: 6ecb6dc7d235f89b2e95eaf382c84b72cc6dfc0e
5
5
  SHA512:
6
- metadata.gz: 53931320e6446ada93e5c4dcfae310f8844a3ade90f754a337e11e6c74395c92d93affcceb0a398f325974ebf31b01d4c864b23190d02e04ff99e61f79368a93
7
- data.tar.gz: afd509fc0026f7a00a31cbcf9f22d11b4e004f633092f1f535041b4fd9eed6094070c09d0fb3d1ab366891c430e98ecc9d47bfdcdf8987738e13fadfcbaaeb1e
6
+ metadata.gz: 192b5263921799f1a0acaa837a006b780a9939572d67d8cf3cec783b549358bc122276776668f988f3cf8e5f1b186c8e21358608b85ff24ea6a0a8a551f321ed
7
+ data.tar.gz: 8036dd8ceb1d21917b6e282c27fc512cce92e3717774d032e04eca05da97d206e2ad91eade924a250f7957d9224c90c93ef4126d2953ffcbc9b55c373642c9b5
@@ -5,21 +5,24 @@ require 'active_record'
5
5
  require 'active_serialize/version'
6
6
  require 'active_serialize/key_formatter'
7
7
  require 'active_serialize/class_methods'
8
+ require 'active_serialize/relation'
8
9
 
9
10
  module ActiveSerialize
10
11
  extend ActiveSupport::Concern
11
12
  cattr_accessor :configs, default: { default: { rmv: [ ], add: [ ], key_format: nil } }
12
13
 
13
14
  class_methods do
14
- def active_serialize rmv: [ ], add: [ ], recursive: [ ], **configs
15
+ def active_serialize rmv: [ ], add: [ ], recursive: [ ], pluck: [ ], **configs
15
16
  extend ClassMethods
16
17
  include ToH
18
+ ::ActiveRecord::Relation.include Relation
17
19
  delegate :active_serialize_keys, :_active_serialize, to: self
18
20
 
19
21
  _active_serialize.merge!(configs)
20
22
  active_serialize_rmv *Array(rmv)
21
23
  active_serialize_add *Array(add)
22
- active_serialize_add *Array(recursive), recursive: true
24
+ active_serialize_add *Array(recursive), to: :recursive
25
+ active_serialize_add *Array(pluck), to: :pluck
23
26
  end
24
27
 
25
28
  def active_serialize_default **args
@@ -28,13 +31,14 @@ module ActiveSerialize
28
31
  end
29
32
 
30
33
  module ToH
31
- def to_h(*groups, rmv: [ ], add: [ ], merge: { })
32
- tran_key = ->(key) { _active_serialize[:map][key] || key }
33
- recursion = _active_serialize[:recursive].map { |key| [ tran_key.(key), public_send(key)&.to_ha ] }.to_h
34
+ def to_h(*groups, rmv: [ ], add: [ ], recursive: [ ], plucked: { }, merge: { })
35
+ tran_key = ->(key) { (_active_serialize[:map][key] || key).to_s }
36
+ recursion = (_active_serialize[:recursive] + recursive).map { |k| [ k, public_send(k)&.to_ha ] }.to_h
37
+
34
38
  KeyFormatter.(_active_serialize[:key_format],
35
39
  active_serialize_keys(*groups, rmv: rmv, add: add)
36
- .map { |key| [ tran_key.(key), public_send(key) ] }.to_h
37
- .merge(merge).merge(recursion).deep_stringify_keys!
40
+ .map{ |key| [ tran_key.(key), public_send(key) ] }.to_h
41
+ .merge(plucked.merge(recursion).merge(merge).transform_keys(&tran_key))
38
42
  )
39
43
  end
40
44
 
@@ -3,21 +3,25 @@
3
3
  module ActiveSerialize
4
4
  module ClassMethods
5
5
  def _active_serialize
6
- ActiveSerialize.configs[name] ||= { **ActiveSerialize.configs[:default].deep_dup, final: nil, groups: { }, recursive: [ ], map: { } }
6
+ ActiveSerialize.configs[name] ||= {
7
+ **ActiveSerialize.configs[:default].deep_dup,
8
+ final: nil, groups: { }, recursive: [ ], map: { }, pluck: [ ]
9
+ }
7
10
  end
8
11
 
9
- def to_ha(**args)
10
- all.to_a.map { |record| record.to_h(**args) }
12
+ def to_ha(*args)
13
+ all.to_ha(*args)
11
14
  end
12
15
 
13
16
  def active_serialize_rmv *attrs
14
17
  _active_serialize[:rmv].concat attrs.map(&:to_sym)
15
18
  end
16
19
 
17
- def active_serialize_add *attrs, named: nil, recursive: false, group: nil
20
+ # to: `recursive` or `pluck`
21
+ def active_serialize_add *attrs, named: nil, to: nil, group: nil
18
22
  active_serialize_map attrs[0] => named if named
19
23
  return _active_serialize[:groups][group] = attrs.map(&:to_sym) if group
20
- _active_serialize[recursive ? :recursive : :add].concat attrs.map(&:to_sym)
24
+ _active_serialize[to || :add].concat attrs.map(&:to_sym)
21
25
  end
22
26
 
23
27
  def active_serialize_map **settings
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSerialize
4
+ module Relation
5
+ def to_ha(pluck: [ ], plucked: nil, **args)
6
+ plucked ||=
7
+ if pluck.is_a?(Proc)
8
+ instance_eval(&pluck)
9
+ else
10
+ (_active_serialize[:pluck] + pluck).map { |key| [ key, instance_eval(&method(key)) ] }.to_h
11
+ end
12
+
13
+ if plucked.present?
14
+ each_with_index.map do |record, i|
15
+ record.to_h(plucked: plucked.each_key.map { |k| [ k, plucked[k][i] ] }.to_h, **args)
16
+ end
17
+ else
18
+ map { |record| record.to_h(**args) }
19
+ end
20
+ end
21
+
22
+ def with_ha(*args)
23
+ return to_ha(*args), self
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveSerialize
4
- VERSION = '2.3.0'
4
+ VERSION = '2.4.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.3.0
4
+ version: 2.4.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-10 00:00:00.000000000 Z
11
+ date: 2019-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -185,6 +185,7 @@ files:
185
185
  - lib/active_serialize.rb
186
186
  - lib/active_serialize/class_methods.rb
187
187
  - lib/active_serialize/key_formatter.rb
188
+ - lib/active_serialize/relation.rb
188
189
  - lib/active_serialize/version.rb
189
190
  homepage: https://github.com/ikkiuchi/active_serialize
190
191
  licenses:
@@ -206,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
207
  version: '0'
207
208
  requirements: []
208
209
  rubyforge_project:
209
- rubygems_version: 2.7.3
210
+ rubygems_version: 2.6.12
210
211
  signing_key:
211
212
  specification_version: 4
212
213
  summary: Provide a very simple way to transform ActiveRecord data into Hash output.