active_record_to_hash 1.1.0 → 1.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: d58db5d3bc83a1c3d5aeacc76b93c4a74170cd56f4ccce73a7c2fbda1c231bad
4
- data.tar.gz: a386699d79c74de0ea5a1f6446544ac90a61c56b355abff1bb17b40c1f1a1f52
3
+ metadata.gz: e6fd8387c13ea938de4bd07f3136535a99c11f294e06f895565da55970e916a6
4
+ data.tar.gz: 1ed0ac8096bc6ff084516be1fb130e43a34d7578044a043da73543bf640ada14
5
5
  SHA512:
6
- metadata.gz: 9131d35e459532105d66f8144d73c152a6f0a66f1f1e366ddfdbacd34b6ee223c1b6457ce03682be00323db915ed09eadabda846ad5056c7aaac61b0e8e1dfca
7
- data.tar.gz: 5ca180e0781e2bad5bfc98a697394ee44d6c9ab87a8222e81bc25041810af1df57cd291962a9f3529edee7ae3c8c63e92e17ca6ff27c2654f7699d5a5defc06d
6
+ metadata.gz: df5703d7c50fa512ec6d4294a07a967a81c4faa1fb827fefc3e0cec25c3dca038bd7a2c557353fe00d235e6caa4414d8aa01a394d48f10ff1a19bcf934029e97
7
+ data.tar.gz: fe940a5caee54389c0641b12c3e64057686b6acc7e7232b42c6e16c45cebb6dbe710be35a8d1a184305101f151c7e2e4c2e6ff386d115f683baf278d61af5a62
data/README.md CHANGED
@@ -169,6 +169,10 @@ p 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
+ p shop.to_hash(ignore_nil: true)
174
+ # When the value is nil, it skips that key.
175
+
172
176
  ```
173
177
 
174
178
  ### 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
@@ -23,6 +24,9 @@ module ActiveRecordToHash
23
24
 
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__)
27
+ next if child_options[:optional] == true && child.blank?
28
+ next if options[:ignore_nil] && child.nil?
29
+
26
30
  result[hash_key] = ActiveRecordToHash.handle_alter(child, child_options)
27
31
  end
28
32
 
@@ -20,7 +20,7 @@ module ActiveRecordToHash
20
20
  end
21
21
 
22
22
  def retrieve_child_attribute(record, attr_name, options, callee) # rubocop:disable Metrics/AbcSize
23
- if options[:value]
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]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordToHash
4
- VERSION = '1.1.0'
4
+ VERSION = '1.3.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.1.0
4
+ version: 1.3.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-21 00:00:00.000000000 Z
11
+ date: 2022-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails