active_record_to_hash 1.3.0 → 1.4.1

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: e6fd8387c13ea938de4bd07f3136535a99c11f294e06f895565da55970e916a6
4
- data.tar.gz: 1ed0ac8096bc6ff084516be1fb130e43a34d7578044a043da73543bf640ada14
3
+ metadata.gz: ee7d18936f63f25e4f4c3b57577a451722efd964f70bdc558fc71595d7b4083d
4
+ data.tar.gz: 2fd74f49bd53f8e05d4297fc83f9c66cd678a5f8407d0e05bf5a11603a87f1bf
5
5
  SHA512:
6
- metadata.gz: df5703d7c50fa512ec6d4294a07a967a81c4faa1fb827fefc3e0cec25c3dca038bd7a2c557353fe00d235e6caa4414d8aa01a394d48f10ff1a19bcf934029e97
7
- data.tar.gz: fe940a5caee54389c0641b12c3e64057686b6acc7e7232b42c6e16c45cebb6dbe710be35a8d1a184305101f151c7e2e4c2e6ff386d115f683baf278d61af5a62
6
+ metadata.gz: 0374f18c448d2f28c5448a251326403f9331884dc16c5d7933ac5f428c581c5ec2e8cad16ab40514239e87993eb239124a9b8cdebc9e3620e87185c3c8ff9df1
7
+ data.tar.gz: 3a24e2e80ca5b2f61ee4208719b61ef9ca3bdf9fb9cc5ab4ff7234b0d7bd27fbe1f99557dee7f7347fa337ee68686de0fe49603252a758937b901f3106952b0f
data/README.md CHANGED
@@ -165,14 +165,18 @@ p shop.to_hash(
165
165
  # :areas=>{1=>"Area No1", 2=>"Area No2", 3=>"Area No3"}
166
166
  # }
167
167
 
168
- p shop.to_hash(only: [:id, :name], with_areas: :exists)
168
+ shop.to_hash(only: [:id, :name], with_areas: :exists)
169
169
  # {:id=>1, :name=>"Shop No1", :areas=>true}
170
170
  # You can use this option only with ActiveRecord::Relation
171
171
 
172
172
 
173
- p shop.to_hash(ignore_nil: true)
173
+ shop.to_hash(ignore_nil: true)
174
174
  # When the value is nil, it skips that key.
175
175
 
176
+
177
+ shop.to_hash(with_category_name: { delegate: { category: :name } })
178
+ # `category_name` is `shop.category.name`
179
+
176
180
  ```
177
181
 
178
182
  ### Configuration
@@ -19,13 +19,23 @@ module ActiveRecordToHash
19
19
  [value]
20
20
  end
21
21
 
22
- def retrieve_child_attribute(record, attr_name, options, callee) # rubocop:disable Metrics/AbcSize
22
+ def retrieve_child_attribute(record, attr_name, options, callee) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/LineLength
23
23
  if options.key?(:value)
24
24
  return options[:value].call(record) if options[:value].is_a? Proc
25
25
 
26
26
  return options[:value]
27
27
  end
28
28
 
29
+ if options.key?(:delegate)
30
+ key = options[:delegate].keys.first
31
+ value = options[:delegate].values.first
32
+
33
+ delegator = record.public_send(key)
34
+ return nil if delegator.nil?
35
+
36
+ return delegator.public_send(value)
37
+ end
38
+
29
39
  args = options[:args] || []
30
40
  value = record.public_send(attr_name, *args)
31
41
  if options[:exists]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordToHash
4
- VERSION = '1.3.0'
4
+ VERSION = '1.4.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_to_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masamoto Miyata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-07 00:00:00.000000000 Z
11
+ date: 2022-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails