ruby_var_dump 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec_status +4 -4
- data/lib/ruby_var_dump/version.rb +1 -1
- data/lib/ruby_var_dump.rb +52 -13
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ca58788ccbccedeae45367f1209c79bd52f151eee3b53ee552b4d8a86558595
|
4
|
+
data.tar.gz: 3a255a625d7260888079ae633bc931dd9740df025eae4faf8264381172e0a19f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
4
|
-
./spec/ruby_var_dump_spec.rb[1:1:2:1] | passed | 0.
|
5
|
-
./spec/ruby_var_dump_spec.rb[1:1:2:2] | passed | 0.
|
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.
|
11
|
+
./spec/ruby_var_dump_spec.rb[1:1:6:1] | passed | 0.00009 seconds |
|
data/lib/ruby_var_dump.rb
CHANGED
@@ -4,10 +4,19 @@ 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?
|
@@ -16,7 +25,7 @@ module RubyVarDump
|
|
16
25
|
else
|
17
26
|
print "\n"
|
18
27
|
obj.each_with_index do |item, index|
|
19
|
-
dump(item, level + 1, false)
|
28
|
+
dump(item, level + 1, false, dumped_objects, false)
|
20
29
|
print "," unless index == obj.size - 1
|
21
30
|
print "\n"
|
22
31
|
end
|
@@ -34,7 +43,7 @@ module RubyVarDump
|
|
34
43
|
# キーにインデントを適用し、値にはインデントを適用しない
|
35
44
|
print "#{indent} #{key.inspect.chomp} => "
|
36
45
|
if value.is_a?(Hash) || value.is_a?(Array)
|
37
|
-
dump(value, level + 1, true) # ハッシュのバリューの場合には is_value を true に設定
|
46
|
+
dump(value, level + 1, true, dumped_objects, false) # ハッシュのバリューの場合には is_value を true に設定
|
38
47
|
else
|
39
48
|
print value.inspect # プリミティブな値の場合は現在のレベルで出力
|
40
49
|
end
|
@@ -44,21 +53,51 @@ module RubyVarDump
|
|
44
53
|
print "#{indent}}"
|
45
54
|
print "\n" if level == 0 # メソッドの出力の最後に改行を追加
|
46
55
|
end
|
47
|
-
elsif defined?(ActiveRecord::
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
55
84
|
end
|
56
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 # メソッドの出力の最後に改行を追加
|
57
95
|
elsif obj.respond_to?(:attributes)
|
58
|
-
dump(obj.attributes, level, false)
|
96
|
+
dump(obj.attributes, level, false, dumped_objects, false)
|
59
97
|
else
|
60
98
|
print indent + obj.inspect.chomp
|
61
99
|
print "\n" if level == 0 # メソッドの出力の最後に改行を追加
|
62
100
|
end
|
63
101
|
end
|
64
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.
|
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-
|
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:
|