ruby_var_dump 0.1.5 → 0.2.0

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: 32a37618c09c721d7677fa7324b178ab96bcee787792be3f183ace3ceb2902da
4
- data.tar.gz: 68d8fb523c3d91e95e2e3b3d7983a3e2262940396b17942acc72c65816b1cf61
3
+ metadata.gz: 2500e049b226d5690eaf4d5352d6817b19f331713fa2866f0d7b9750e36a9a65
4
+ data.tar.gz: f6593f187ebf2dfffb5fae98c3b906f0fdebbf3014a9e32b960e9f4b7eb5ac05
5
5
  SHA512:
6
- metadata.gz: 83e17077afb7639b4dbc1b11ecda8dcb6c7a94ee411084045b73efec8c73f70c4a4315f1bbcfc7e7ef2835d08a727fb0631ef362cb855ebf6358d56f53db0f62
7
- data.tar.gz: efd57f9f7ebceb180080b2288ba7e16ea08412ccc58620da63a4b22ee4064a5008e7656fdc9873effb41a8a12bcf5e5dd70473fba1612ca5167e14df573fcdca
6
+ metadata.gz: 71512e0e4cafdd53d9a20c764119d7b7e4fee1703ed2ea84fa4aff823848b6aedda1ea91e19326854bccc5336a37d06b5304f99d7c329621aaaf702635384631
7
+ data.tar.gz: 525b53da60b91a9182326e501f7d1f494070cea54f6b4e74868bff8a4809b097038a47ebc800f637f141a1c149cf754a68643c2a5fa072b7c9f26714b1597074
data/.rspec_status CHANGED
@@ -1,11 +1,15 @@
1
- example_id | status | run_time |
2
- ------------------------------------- | ------ | --------------- |
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
- ./spec/ruby_var_dump_spec.rb[1:1:3:1] | passed | 0.00003 seconds |
7
- ./spec/ruby_var_dump_spec.rb[1:1:3:2] | passed | 0.00004 seconds |
8
- ./spec/ruby_var_dump_spec.rb[1:1:4:1] | passed | 0.00003 seconds |
9
- ./spec/ruby_var_dump_spec.rb[1:1:4:2] | passed | 0.00003 seconds |
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.00009 seconds |
1
+ example_id | status | run_time |
2
+ --------------------------------------- | ------ | --------------- |
3
+ ./spec/ruby_var_dump_spec.rb[1:1:1] | passed | 0.00037 seconds |
4
+ ./spec/ruby_var_dump_spec.rb[1:1:2:1:1] | passed | 0.00031 seconds |
5
+ ./spec/ruby_var_dump_spec.rb[1:1:2:1:2] | passed | 0.001 seconds |
6
+ ./spec/ruby_var_dump_spec.rb[1:1:2:2:1] | passed | 0.00004 seconds |
7
+ ./spec/ruby_var_dump_spec.rb[1:1:2:2:2] | passed | 0.00003 seconds |
8
+ ./spec/ruby_var_dump_spec.rb[1:1:2:3:1] | passed | 0.0001 seconds |
9
+ ./spec/ruby_var_dump_spec.rb[1:1:2:3:2] | passed | 0.00004 seconds |
10
+ ./spec/ruby_var_dump_spec.rb[1:1:3:1] | passed | 0.00003 seconds |
11
+ ./spec/ruby_var_dump_spec.rb[1:1:3:2] | passed | 0.00004 seconds |
12
+ ./spec/ruby_var_dump_spec.rb[1:1:4:1] | passed | 0.00003 seconds |
13
+ ./spec/ruby_var_dump_spec.rb[1:1:4:2] | passed | 0.00003 seconds |
14
+ ./spec/ruby_var_dump_spec.rb[1:1:5:1] | passed | 0.00004 seconds |
15
+ ./spec/ruby_var_dump_spec.rb[1:1:6:1] | passed | 0.00009 seconds |
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2024 hirokiyam
3
+ Copyright (c) 2025 hirokiyam
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyVarDump
4
- VERSION = "0.1.5"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/ruby_var_dump.rb CHANGED
@@ -5,6 +5,16 @@ require_relative "ruby_var_dump/version"
5
5
  module RubyVarDump
6
6
  class Error < StandardError; end
7
7
 
8
+ # カラーコードの定義
9
+ RED_COLOR = "\e[38;5;196m" # 赤色
10
+ LIGHT_RED_COLOR = "\e[38;5;203m" # サーモンピンク
11
+ BLUE_COLOR = "\e[38;5;27m" # 青色
12
+ GREEN_COLOR = "\e[38;5;2m" # 緑色
13
+ LIGHT_GREEN_COLOR = "\e[38;5;10m" # 薄緑色
14
+ # LIGHT_BLUE = "\e[38;5;45m" # 水色
15
+ ORANGE_COLOR = "\e[38;5;214m" # オレンジ色
16
+ RESET_COLOR = "\e[0m" # 色リセット
17
+
8
18
  def vdump(obj, level = 0, is_value = false, dumped_objects = [], is_first=true)
9
19
  indent = ' ' * level * 2
10
20
 
@@ -45,7 +55,8 @@ module RubyVarDump
45
55
  if value.is_a?(Hash) || value.is_a?(Array)
46
56
  vdump(value, level + 1, true, dumped_objects, false) # ハッシュのバリューの場合には is_value を true に設定
47
57
  else
48
- print value.inspect # プリミティブな値の場合は現在のレベルで出力
58
+ # プリミティブな値の場合は現在のレベルで出力
59
+ print colorize_by_type(value)
49
60
  end
50
61
  print "," unless index == obj.size - 1
51
62
  print "\n"
@@ -56,31 +67,46 @@ module RubyVarDump
56
67
  elsif defined?(ActiveRecord::Base) && obj.is_a?(ActiveRecord::Base)
57
68
  # ActiveRecordオブジェクトの場合
58
69
  print "\n" unless is_first # 最初のレコード以外の場合に改行を入れる
59
- print "#{indent}#{obj.class}:#{obj.object_id}\n"
70
+ print "#{indent}#{GREEN_COLOR}#{obj.class}:#{obj.object_id}#{RESET_COLOR}\n"
60
71
  print "#{indent}{\n"
61
72
  obj.attributes.each do |attr_name, attr_value|
62
- print "#{indent} #{attr_name}: #{attr_value.inspect},\n"
73
+ print "#{indent} #{attr_name}: #{colorize_by_type(attr_value)},\n"
63
74
  end
64
75
  # リレーションも再帰的にダンプ
65
76
  obj.class.reflect_on_all_associations.each do |association|
66
77
  next if association.macro.nil? # アソシエーションが存在しない場合はスキップ
67
78
 
68
79
  associated_value = obj.send(association.name)
69
- if associated_value.respond_to?(:each) # アソシエーションがコレクションの場合
70
- next if associated_value.empty?
80
+
81
+ # has_many や has_and_belongs_to_many など、コレクションを返すアソシエーション
82
+ if association.macro == :has_many || association.macro == :has_and_belongs_to_many
83
+ next if associated_value.nil? || (associated_value.respond_to?(:empty?) && associated_value.empty?)
71
84
 
72
85
  associated_value.each do |item|
73
86
  next if dumped_objects.any? { |dumped_obj| dumped_obj.object_id == item.object_id && dumped_obj.class == item.class }
74
87
 
75
- # CollectionProxy の内容(アソシエーション)をダンプ
76
- print("#{indent} #{association.name} (association): << #{item.class}:#{item.object_id} >>\n")
88
+ # CollectionProxy の内容(アソシエーション)をダンプ
89
+ print("#{indent} #{RED_COLOR}#{association.name} #{ORANGE_COLOR}(#{association.macro})#{RESET_COLOR}: #{GREEN_COLOR}<< #{item.class}:#{item.object_id} >>#{RESET_COLOR}\n")
77
90
  print("#{indent} {\n")
78
91
  item.attributes.each do |attr_name, attr_value|
79
- print("#{indent} #{attr_name}: #{attr_value.inspect}\n")
92
+ print("#{indent} #{attr_name}: #{colorize_by_type(attr_value)}\n")
80
93
  end
81
94
 
82
95
  print("#{indent} }\n") #ここまでアソシエーションの描画
83
96
  end
97
+ # belongs_to や has_one など、単一オブジェクトを返すアソシエーション
98
+ elsif association.macro == :belongs_to || association.macro == :has_one
99
+ next if associated_value.nil?
100
+ next if dumped_objects.any? { |dumped_obj| dumped_obj.object_id == associated_value.object_id && dumped_obj.class == associated_value.class }
101
+
102
+ # 単一オブジェクトのアソシエーションをダンプ
103
+ print("#{indent} #{RED_COLOR}#{association.name} #{ORANGE_COLOR}(#{association.macro})#{RESET_COLOR}: #{GREEN_COLOR}<< #{associated_value.class}:#{associated_value.object_id} >>#{RESET_COLOR}\n")
104
+ print("#{indent} {\n")
105
+ associated_value.attributes.each do |attr_name, attr_value|
106
+ print("#{indent} #{attr_name}: #{colorize_by_type(attr_value)}\n")
107
+ end
108
+
109
+ print("#{indent} }\n") #ここまでアソシエーションの描画
84
110
  end
85
111
  end
86
112
  print "#{indent}}\n"
@@ -95,9 +121,31 @@ module RubyVarDump
95
121
  elsif obj.respond_to?(:attributes)
96
122
  vdump(obj.attributes, level, false, dumped_objects, false)
97
123
  else
98
- print indent + obj.inspect.chomp
124
+ print indent + colorize_by_type(obj) # プリミティブな値の場合を出力
99
125
  print "\n" if level == 0 # メソッドの出力の最後に改行を追加
100
126
  end
101
127
  end
128
+
129
+ # エイリアスを設定。vdump メソッドの別名として vpp を定義
130
+ alias vpp vdump
131
+
132
+ private
133
+
134
+ def colorize_by_type(obj)
135
+ color = color_for(obj)
136
+ "#{color}#{obj.inspect.chomp}#{RESET_COLOR}"
137
+ end
138
+
139
+ def color_for(obj)
140
+ if obj.is_a?(String)
141
+ LIGHT_RED_COLOR
142
+ elsif obj.is_a?(Numeric)
143
+ BLUE_COLOR
144
+ elsif obj.is_a?(Date) || obj.is_a?(Time) || obj.is_a?(DateTime)
145
+ LIGHT_GREEN_COLOR
146
+ else
147
+ ""
148
+ end
149
+ end
102
150
  end
103
151
  # 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.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hirokiyam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-02-27 00:00:00.000000000 Z
11
+ date: 2025-05-06 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.
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubygems_version: 3.5.9
54
+ rubygems_version: 3.5.10
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: A Ruby gem for detailed debugging and inspection of objects.