active_record_to_hash 1.2.1 → 1.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
2
  SHA256:
3
- metadata.gz: 1fa2aa0c803cdaa15448897462fd144eeb3c1465b1010767ff3693e0f8d1b237
4
- data.tar.gz: 657ef30f57cb45c48adb334dac896d341211d8b68ae89f197ee88997282dad34
3
+ metadata.gz: 66315aee820fcea1d5206ffb48c85fdb1c036ad4cf418f3d35ba355041c4da8f
4
+ data.tar.gz: 6bb0c40f8017edea321b0d43ee0b1183465b56616733aebf12b1a6abbc14d01b
5
5
  SHA512:
6
- metadata.gz: d56deb1ac8c203ba63b6f0ad1afcef993f3af4dbe5d28a56dfe9f41825b3af69eb2850c815a986796ef9986d5958c516f3be0799a4097f8f84f32b7d3269ff62
7
- data.tar.gz: 05efcbb527ebb3d8ad2d0aa7ce75e77c94305b99a857c349a2b7927e58f42fc5239afb55ffa43d430648192d03a9f3e202364b6ee0a76dd6fa3df82a05ddad7e
6
+ metadata.gz: e27b516b74c9683b1c11387eeee294ca2895c815207665ce57f1a07fa222ad843dd9cace0dbcf946098aae96eb51f4ae444d78d2efe6778f037e9b3314c04223
7
+ data.tar.gz: 4fd44f141c4ba48ac6375b9a9d9cba96e6f7ccea159be41f6f7d6a341765cb2ac2008ed036f77da7f4e722157633356b55aacbdd6b4890e6d1e3fa3daa891cb6
data/README.md CHANGED
@@ -165,10 +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
+
173
+ shop.to_hash(ignore_nil: true)
174
+ # When the value is nil, it skips that key.
175
+
176
+
177
+ shop.to_hash(with_category_name: { delegate: { category: :name } })
178
+ # `category_name` is `shop.category.name`
179
+
172
180
  ```
173
181
 
174
182
  ### Configuration
@@ -15,6 +15,7 @@ module ActiveRecordToHash
15
15
  key = k.to_sym
16
16
  next if ActiveRecordToHash.to_a(options[:except]).include?(key)
17
17
  next if options[:only] && !ActiveRecordToHash.to_a(options[:only]).include?(key)
18
+ next if options[:ignore_nil] && v.nil?
18
19
 
19
20
  memo[key] = v
20
21
  end
@@ -24,6 +25,7 @@ module ActiveRecordToHash
24
25
  ActiveRecordToHash.handle_with_options(options) do |hash_key, attr_name, child_options|
25
26
  child = ActiveRecordToHash.retrieve_child_attribute(self, attr_name, child_options, __callee__)
26
27
  next if child_options[:optional] == true && child.blank?
28
+ next if options[:ignore_nil] && child.nil?
27
29
 
28
30
  result[hash_key] = ActiveRecordToHash.handle_alter(child, child_options)
29
31
  end
@@ -26,6 +26,13 @@ module ActiveRecordToHash
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
+ return record.public_send(key).public_send(value)
34
+ end
35
+
29
36
  args = options[:args] || []
30
37
  value = record.public_send(attr_name, *args)
31
38
  if options[:exists]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordToHash
4
- VERSION = '1.2.1'
4
+ VERSION = '1.4.0'
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.2.1
4
+ version: 1.4.0
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-07-29 00:00:00.000000000 Z
11
+ date: 2022-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails