ruby_var_dump 0.1.2 → 0.1.4

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: c8a763a401d478d5f71bede2331f0ed143c061d20da95b4a5d1ec8b3606b72e7
4
- data.tar.gz: 0b2e980c5c8cb43be780656dc618a3280beb77212e80e2ba9f8985424fceb498
3
+ metadata.gz: 1ca58788ccbccedeae45367f1209c79bd52f151eee3b53ee552b4d8a86558595
4
+ data.tar.gz: 3a255a625d7260888079ae633bc931dd9740df025eae4faf8264381172e0a19f
5
5
  SHA512:
6
- metadata.gz: bdb1e000065d3994ec480cbb3a6c75ef0df9d10085f2e8cfeaf9e1714d74ff1262b5657c4eb1cbe1b18b0a2cbe997a3d289af13cf3fa9b0264ca04e48c21fe1c
7
- data.tar.gz: 365330b1078f0189d0c041f09a9d5d449f21787ec235ad8c4baa35eb8f5d564972a3963fb7ecfff46bc82b40d04d8295d61db9bf7edf90cf6cabe1960deb109c
6
+ metadata.gz: 7b0142c9ec6a6130627f993b06810eaeac717cc0666e9af9f2f9d953f08927b619c6df9e582f2f6e4d2f05bb9cd11eeae9aec9800f8b01533432c87053e9f94d
7
+ data.tar.gz: 7ce09cea1d956caa410bc775cc137e2897248f7bbea2eac085c9254f8ba32cd99d3ad7ef2896d35306f4a47e693e9497f58d25a11463f6da9dd5f1bdcdf89f14
data/.rspec_status CHANGED
@@ -1,11 +1,11 @@
1
1
  example_id | status | run_time |
2
2
  ------------------------------------- | ------ | --------------- |
3
- ./spec/ruby_var_dump_spec.rb[1:1:1] | passed | 0.00026 seconds |
4
- ./spec/ruby_var_dump_spec.rb[1:1:2:1] | passed | 0.0002 seconds |
5
- ./spec/ruby_var_dump_spec.rb[1:1:2:2] | passed | 0.00003 seconds |
3
+ ./spec/ruby_var_dump_spec.rb[1:1:1] | passed | 0.00034 seconds |
4
+ ./spec/ruby_var_dump_spec.rb[1:1:2:1] | passed | 0.00027 seconds |
5
+ ./spec/ruby_var_dump_spec.rb[1:1:2:2] | passed | 0.00008 seconds |
6
6
  ./spec/ruby_var_dump_spec.rb[1:1:3:1] | passed | 0.00003 seconds |
7
7
  ./spec/ruby_var_dump_spec.rb[1:1:3:2] | passed | 0.00004 seconds |
8
8
  ./spec/ruby_var_dump_spec.rb[1:1:4:1] | passed | 0.00003 seconds |
9
9
  ./spec/ruby_var_dump_spec.rb[1:1:4:2] | passed | 0.00003 seconds |
10
10
  ./spec/ruby_var_dump_spec.rb[1:1:5:1] | passed | 0.00004 seconds |
11
- ./spec/ruby_var_dump_spec.rb[1:1:6:1] | passed | 0.00008 seconds |
11
+ ./spec/ruby_var_dump_spec.rb[1:1:6:1] | passed | 0.00009 seconds |
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyVarDump
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
  end
data/lib/ruby_var_dump.rb CHANGED
@@ -4,34 +4,46 @@ require_relative "ruby_var_dump/version"
4
4
 
5
5
  module RubyVarDump
6
6
  class Error < StandardError; end
7
- # Your code goes here...
8
7
 
9
- def dump(obj, level = 0, is_value = false)
8
+ def dump(obj, level = 0, is_value = false, dumped_objects = [], is_first=true)
10
9
  indent = ' ' * level * 2
10
+
11
+ if (defined?(ActiveRecord::Base) && obj.is_a?(ActiveRecord::Base)) || (defined?(ActiveRecord::Relation) && obj.is_a?(ActiveRecord::Relation))
12
+ if dumped_objects.any? { |dumped_obj| dumped_obj.object_id == obj.object_id && dumped_obj.class == obj.class }
13
+ puts "#{indent}<Recursive reference: #{obj.class}:#{obj.object_id}>"
14
+ return
15
+ else
16
+ dumped_objects << obj
17
+ end
18
+ end
19
+
11
20
  if obj.is_a?(Array)
12
21
  print "#{is_value ? '' : indent}" + "["
13
22
  if obj.empty?
14
23
  print "]"
24
+ print "\n" if level == 0 # メソッドの出力の最後に改行を追加
15
25
  else
16
26
  print "\n"
17
27
  obj.each_with_index do |item, index|
18
- dump(item, level + 1, false)
28
+ dump(item, level + 1, false, dumped_objects, false)
19
29
  print "," unless index == obj.size - 1
20
30
  print "\n"
21
31
  end
22
32
  print "#{indent}]"
33
+ print "\n" if level == 0 # メソッドの出力の最後に改行を追加
23
34
  end
24
35
  elsif obj.is_a?(Hash)
25
36
  print "#{is_value ? '' : indent}" + "{"
26
37
  if obj.empty?
27
38
  print "}"
39
+ print "\n" if level == 0 # メソッドの出力の最後に改行を追加
28
40
  else
29
41
  print "\n"
30
42
  obj.each_with_index do |(key, value), index|
31
43
  # キーにインデントを適用し、値にはインデントを適用しない
32
44
  print "#{indent} #{key.inspect.chomp} => "
33
45
  if value.is_a?(Hash) || value.is_a?(Array)
34
- dump(value, level + 1, true) # ハッシュのバリューの場合には is_value を true に設定
46
+ dump(value, level + 1, true, dumped_objects, false) # ハッシュのバリューの場合には is_value を true に設定
35
47
  else
36
48
  print value.inspect # プリミティブな値の場合は現在のレベルで出力
37
49
  end
@@ -39,21 +51,53 @@ module RubyVarDump
39
51
  print "\n"
40
52
  end
41
53
  print "#{indent}}"
54
+ print "\n" if level == 0 # メソッドの出力の最後に改行を追加
42
55
  end
43
- elsif defined?(ActiveRecord::Relation) && obj.is_a?(ActiveRecord::Relation)
44
- obj.each_with_index do |record, index|
45
- dump(record, level, false)
46
- if index < obj.size - 1 # 要素が続く場合
47
- print ",\n"
48
- else
49
- print "\n" # レコードの最後の場合は改行のみ
56
+ elsif defined?(ActiveRecord::Base) && obj.is_a?(ActiveRecord::Base)
57
+ # ActiveRecordオブジェクトの場合
58
+ print "\n" unless is_first # 最初のレコード以外の場合に改行を入れる
59
+ print "#{indent}#{obj.class}:#{obj.object_id}\n"
60
+ print "#{indent}{\n"
61
+ obj.attributes.each do |attr_name, attr_value|
62
+ print "#{indent} #{attr_name}: #{attr_value.inspect},\n"
63
+ end
64
+ # リレーションも再帰的にダンプ
65
+ obj.class.reflect_on_all_associations.each do |association|
66
+ next if association.macro.nil? # アソシエーションが存在しない場合はスキップ
67
+
68
+ associated_value = obj.send(association.name)
69
+ if associated_value.respond_to?(:each) # アソシエーションがコレクションの場合
70
+ next if associated_value.empty?
71
+
72
+ associated_value.each do |item|
73
+ next if dumped_objects.any? { |dumped_obj| dumped_obj.object_id == item.object_id && dumped_obj.class == item.class }
74
+
75
+ # CollectionProxy の内容(アソシエーション)をダンプ
76
+ print("#{indent} #{association.name} (association): << #{item.class}:#{item.object_id} >>\n")
77
+ print("#{indent} {\n")
78
+ item.attributes.each do |attr_name, attr_value|
79
+ print("#{indent} #{attr_name}: #{attr_value.inspect}\n")
80
+ end
81
+
82
+ print("#{indent} }\n") #ここまでアソシエーションの描画
83
+ end
50
84
  end
51
85
  end
86
+ print "#{indent}}\n"
87
+ elsif defined?(ActiveRecord::Relation) && obj.is_a?(ActiveRecord::Relation)
88
+ print "#{indent}#{obj.class}:#{obj.object_id}\n"
89
+ print "#{indent}[\n"
90
+ obj.each_with_index do |item, index|
91
+ dump(item, level + 1, false, dumped_objects, index == 0)
92
+ end
93
+ print "#{indent}]"
94
+ print "\n" if level == 0 # メソッドの出力の最後に改行を追加
52
95
  elsif obj.respond_to?(:attributes)
53
- dump(obj.attributes, level, false)
96
+ dump(obj.attributes, level, false, dumped_objects, false)
54
97
  else
55
98
  print indent + obj.inspect.chomp
99
+ print "\n" if level == 0 # メソッドの出力の最後に改行を追加
56
100
  end
57
101
  end
58
- print "\n" # メソッドの出力の最後に改行を追加
59
102
  end
103
+ # gem build ruby_var_dump.gemspec
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_var_dump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - hirokiyam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-14 00:00:00.000000000 Z
11
+ date: 2024-05-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby gem for detailed debugging and inspection of objects, mimicking
14
14
  PHP's var_dump function.
@@ -28,12 +28,12 @@ files:
28
28
  - lib/ruby_var_dump.rb
29
29
  - lib/ruby_var_dump/version.rb
30
30
  - sig/ruby_var_dump.rbs
31
- homepage: https://github.com/hirokiyam
31
+ homepage: https://github.com/hirokiyam/ruby_var_dump
32
32
  licenses:
33
33
  - MIT
34
34
  metadata:
35
35
  allowed_push_host: https://rubygems.org
36
- homepage_uri: https://github.com/hirokiyam
36
+ homepage_uri: https://github.com/hirokiyam/ruby_var_dump
37
37
  source_code_uri: https://github.com/hirokiyam/ruby_var_dump
38
38
  changelog_uri: https://github.com/hirokiyam/ruby_var_dump/blob/main/CHANGELOG.md
39
39
  post_install_message: