debug_helper 1.0.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.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +5 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +22 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +593 -0
  9. data/Rakefile +10 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/debug_helper.gemspec +37 -0
  13. data/lib/debug_helper.rb +154 -0
  14. data/lib/debug_helper/version.rb +3 -0
  15. data/markdown/readme/Rakefile +53 -0
  16. data/markdown/readme/class_inclusions.md +6 -0
  17. data/markdown/readme/class_links.md +5 -0
  18. data/markdown/readme/classes/array/circular/show.md +27 -0
  19. data/markdown/readme/classes/array/circular/show.rb +7 -0
  20. data/markdown/readme/classes/array/circular/show.yaml +5 -0
  21. data/markdown/readme/classes/array/circular/template.md +11 -0
  22. data/markdown/readme/classes/array/mixed/show.md +25 -0
  23. data/markdown/readme/classes/array/mixed/show.rb +4 -0
  24. data/markdown/readme/classes/array/mixed/show.yaml +8 -0
  25. data/markdown/readme/classes/array/mixed/template.md +9 -0
  26. data/markdown/readme/classes/array/nested/show.md +28 -0
  27. data/markdown/readme/classes/array/nested/show.rb +4 -0
  28. data/markdown/readme/classes/array/nested/show.yaml +11 -0
  29. data/markdown/readme/classes/array/nested/template.md +9 -0
  30. data/markdown/readme/classes/array/simple/show.md +22 -0
  31. data/markdown/readme/classes/array/simple/show.rb +4 -0
  32. data/markdown/readme/classes/array/simple/show.yaml +5 -0
  33. data/markdown/readme/classes/array/simple/template.md +9 -0
  34. data/markdown/readme/classes/array/template.md +9 -0
  35. data/markdown/readme/classes/hash/circular/show.md +33 -0
  36. data/markdown/readme/classes/hash/circular/show.rb +7 -0
  37. data/markdown/readme/classes/hash/circular/show.yaml +11 -0
  38. data/markdown/readme/classes/hash/circular/template.md +11 -0
  39. data/markdown/readme/classes/hash/mixed/show.md +38 -0
  40. data/markdown/readme/classes/hash/mixed/show.rb +8 -0
  41. data/markdown/readme/classes/hash/mixed/show.yaml +17 -0
  42. data/markdown/readme/classes/hash/mixed/template.md +9 -0
  43. data/markdown/readme/classes/hash/nested/show.md +54 -0
  44. data/markdown/readme/classes/hash/nested/show.rb +13 -0
  45. data/markdown/readme/classes/hash/nested/show.yaml +28 -0
  46. data/markdown/readme/classes/hash/nested/template.md +9 -0
  47. data/markdown/readme/classes/hash/simple/show.md +31 -0
  48. data/markdown/readme/classes/hash/simple/show.rb +4 -0
  49. data/markdown/readme/classes/hash/simple/show.yaml +14 -0
  50. data/markdown/readme/classes/hash/simple/template.md +9 -0
  51. data/markdown/readme/classes/hash/template.md +9 -0
  52. data/markdown/readme/classes/object/datetime/show.md +19 -0
  53. data/markdown/readme/classes/object/datetime/show.rb +4 -0
  54. data/markdown/readme/classes/object/datetime/show.yaml +2 -0
  55. data/markdown/readme/classes/object/datetime/template.md +9 -0
  56. data/markdown/readme/classes/object/range/show.md +19 -0
  57. data/markdown/readme/classes/object/range/show.rb +4 -0
  58. data/markdown/readme/classes/object/range/show.yaml +2 -0
  59. data/markdown/readme/classes/object/range/template.md +9 -0
  60. data/markdown/readme/classes/object/regexp/show.md +19 -0
  61. data/markdown/readme/classes/object/regexp/show.rb +4 -0
  62. data/markdown/readme/classes/object/regexp/show.yaml +2 -0
  63. data/markdown/readme/classes/object/regexp/template.md +9 -0
  64. data/markdown/readme/classes/object/template.md +11 -0
  65. data/markdown/readme/classes/string/multiline/show.md +25 -0
  66. data/markdown/readme/classes/string/multiline/show.rb +7 -0
  67. data/markdown/readme/classes/string/multiline/show.yaml +5 -0
  68. data/markdown/readme/classes/string/multiline/template.md +9 -0
  69. data/markdown/readme/classes/string/simple/show.md +20 -0
  70. data/markdown/readme/classes/string/simple/show.rb +4 -0
  71. data/markdown/readme/classes/string/simple/show.yaml +3 -0
  72. data/markdown/readme/classes/string/simple/template.md +9 -0
  73. data/markdown/readme/classes/string/template.md +5 -0
  74. data/markdown/readme/classes/struct/circular/show.md +45 -0
  75. data/markdown/readme/classes/struct/circular/show.rb +8 -0
  76. data/markdown/readme/classes/struct/circular/show.yaml +22 -0
  77. data/markdown/readme/classes/struct/circular/template.md +11 -0
  78. data/markdown/readme/classes/struct/mixed/show.md +32 -0
  79. data/markdown/readme/classes/struct/mixed/show.rb +5 -0
  80. data/markdown/readme/classes/struct/mixed/show.yaml +14 -0
  81. data/markdown/readme/classes/struct/mixed/template.md +9 -0
  82. data/markdown/readme/classes/struct/nested/show.md +43 -0
  83. data/markdown/readme/classes/struct/nested/show.rb +8 -0
  84. data/markdown/readme/classes/struct/nested/show.yaml +22 -0
  85. data/markdown/readme/classes/struct/nested/template.md +9 -0
  86. data/markdown/readme/classes/struct/simple/show.md +29 -0
  87. data/markdown/readme/classes/struct/simple/show.rb +5 -0
  88. data/markdown/readme/classes/struct/simple/show.yaml +11 -0
  89. data/markdown/readme/classes/struct/simple/template.md +9 -0
  90. data/markdown/readme/classes/struct/template.md +9 -0
  91. data/markdown/readme/classes/symbol/simple/show.md +18 -0
  92. data/markdown/readme/classes/symbol/simple/show.rb +3 -0
  93. data/markdown/readme/classes/symbol/simple/show.yaml +2 -0
  94. data/markdown/readme/classes/symbol/simple/template.md +9 -0
  95. data/markdown/readme/classes/symbol/template.md +3 -0
  96. data/markdown/readme/classes/template.md +12 -0
  97. data/markdown/readme/show_array.rb +3 -0
  98. data/markdown/readme/show_hash.rb +3 -0
  99. data/markdown/readme/template.md +25 -0
  100. metadata +186 -0
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "debug_helper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "debug_helper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "debug_helper"
8
+ spec.version = DebugHelper::VERSION
9
+ spec.authors = ["burdettelamar"]
10
+ spec.email = ["burdettelamar@yahoo.com"]
11
+
12
+ spec.summary = %q{Help for printf-style debugging.}
13
+ spec.homepage = "https://github.com/BurdetteLamar/debug_helper"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "bin"
31
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.16"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "minitest", "~> 5.0"
37
+ end
@@ -0,0 +1,154 @@
1
+ require 'yaml'
2
+
3
+ require 'debug_helper/version'
4
+
5
+ class DebugHelper
6
+
7
+ attr_accessor \
8
+ :obj,
9
+ :name,
10
+ :object_ids
11
+
12
+ def self.show(obj, name = obj.class)
13
+ debug_helper = DebugHelper.new(obj, name)
14
+ x = debug_helper.send(:_show, obj, name, info = {})
15
+ puts x.to_yaml
16
+ end
17
+
18
+ private
19
+
20
+ def initialize(obj, name)
21
+ self.obj = obj
22
+ self.name = name
23
+ self.object_ids = []
24
+ end
25
+
26
+ def _show(obj, name, info)
27
+ if object_ids.include?(obj.object_id)
28
+ s = show_object(obj, name, info)
29
+ else
30
+ object_ids.push(obj.object_id)
31
+ s = case
32
+ when obj.kind_of?(Array)
33
+ show_array(obj, name, info)
34
+ when obj.kind_of?(Hash)
35
+ show_hash(obj, name, info)
36
+ when obj.kind_of?(String)
37
+ show_string(obj, name, info)
38
+ when obj.kind_of?(Struct)
39
+ show_struct(obj, name, info)
40
+ when obj.kind_of?(Symbol)
41
+ show_symbol(obj, name, info)
42
+ # when obj.kind_of?(Range)
43
+ # when obj.kind_of?(Set)
44
+ else
45
+ show_object(obj, name, info)
46
+ end
47
+ end
48
+ object_ids.pop
49
+ s
50
+ end
51
+
52
+ def show_array(obj, name, info)
53
+ content = {}
54
+ obj.each_with_index do |item, i|
55
+ content.store("Element #{i}", _show(item, nil, {}))
56
+ end
57
+ attrs = {
58
+ :name => name,
59
+ :size => obj.size,
60
+ }
61
+ _show_item(obj.class.name, content, attrs, info)
62
+ end
63
+
64
+ def show_hash(obj, name, info)
65
+ content = {}
66
+ obj.each_with_index do |pair, i|
67
+ key, value = *pair
68
+ pair = {'Key' => _show(key, nil, {}), 'Value' => _show(value, nil, {})}
69
+ content.store("Pair #{i}", pair)
70
+ end
71
+ attrs = {
72
+ :size => obj.size,
73
+ :default => obj.default,
74
+ :default_proc => obj.default_proc,
75
+ :name => name,
76
+ }
77
+ _show_item(obj.class.name, content, attrs, info)
78
+ end
79
+
80
+ def show_object(obj, name, info)
81
+ name_info = name.nil? ? '' : " (name='#{name}')"
82
+ "#{obj.class.name}#{name_info} #{obj}"
83
+ end
84
+
85
+ def show_string(obj, name, info)
86
+ attrs = {
87
+ :name => name,
88
+ :size => obj.size,
89
+ :encoding => obj.encoding,
90
+ :ascii_only => obj.ascii_only?,
91
+ :bytesize => obj.bytesize,
92
+ }
93
+ _show_item(obj.class.name, [obj], attrs, info)
94
+ end
95
+
96
+ def show_struct(obj, name, info)
97
+ content = {}
98
+ i = 0
99
+ obj.each_pair do |member|
100
+ member_name, value = *member
101
+ pair = {'Name' => member_name, 'Value' => _show(value, nil, {})}
102
+ content.store("Member #{i}", pair)
103
+ i += 1
104
+ end
105
+ attrs = {
106
+ :name => name,
107
+ :size => obj.size,
108
+ }
109
+ _show_item(obj.class.name, content, attrs, info)
110
+ end
111
+
112
+ def show_symbol(obj, name, info)
113
+ attrs = {
114
+ :name => name,
115
+ :size => obj.size,
116
+ :encoding => obj.encoding,
117
+ }
118
+ _show_item(obj.class.name, obj, attrs, info)
119
+ end
120
+
121
+ def _show_item(class_name, content, attrs, info)
122
+ name = attrs[:name]
123
+ unless name.nil?
124
+ attrs[:name] = "'#{name}'"
125
+ end
126
+ label = label(class_name, attrs)
127
+ info.store(label, content)
128
+ info
129
+ end
130
+
131
+ def label(class_name, attrs)
132
+ a = []
133
+ attrs.each_pair do |key, value|
134
+ a.push("#{key}=#{value}") unless value.nil?
135
+ end
136
+ attrs_s = a.join(' ')
137
+ "#{class_name} (#{attrs_s})"
138
+ end
139
+
140
+ # def respond_to!(obj, method)
141
+ # unless obj.respond_to?(method)
142
+ # message = "Instance of #{obj.class.name} does not respond to :#{method}"
143
+ # raise ArgumentError.new(message)
144
+ # end
145
+ # end
146
+ #
147
+ # def kind_of!(obj, klass)
148
+ # unless obj.kind_of?(klass)
149
+ # message = "Instance of #{obj.class.name} is not a kind of #{klass}"
150
+ # raise ArgumentError.new(message)
151
+ # end
152
+ # end
153
+
154
+ end
@@ -0,0 +1,3 @@
1
+ class DebugHelper
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,53 @@
1
+ require 'markdown_helper'
2
+
3
+ namespace :build do
4
+
5
+ desc 'Build README.md'
6
+ task :readme do
7
+ # Make output files.
8
+ class_dir_names = nil
9
+ Dir.chdir('classes') do
10
+ class_names = %w/
11
+ Array
12
+ Hash
13
+ Struct
14
+ String
15
+ Symbol
16
+ Object
17
+ /
18
+ class_dir_names = class_names.collect {|x| x.downcase}
19
+ class_dir_names.each do |class_dir_name|
20
+ Dir.chdir(class_dir_name) do
21
+ case_dir_names = Dir.glob('*').select {|f| File.directory? f}
22
+ case_dir_names.each do |case_dir_name|
23
+ Dir.chdir(case_dir_name) do
24
+ command = "ruby show.rb > show.yaml"
25
+ system(command)
26
+ command = "markdown_helper include --pristine template.md show.md"
27
+ system(command)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ # Make list of links to class sections.
34
+ File.open('class_links.md', 'w') do |file|
35
+ class_dir_names.each do |class_dir_name|
36
+ next if class_dir_name == 'object'
37
+ proper_class_name = class_dir_name.capitalize
38
+ link_line = "- [#{proper_class_name}](##{class_dir_name})"
39
+ file.puts(link_line)
40
+ end
41
+ end
42
+ # Make inclusions for class sections.
43
+ File.open('class_inclusions.md', 'w') do |file|
44
+ class_dir_names.each do |class_name|
45
+ inclusion_line = "@[:markdown](classes/#{class_name}/template.md)"
46
+ file.puts(inclusion_line)
47
+ end
48
+ end
49
+ # Make the README markdown.
50
+ command = "markdown_helper include --pristine template.md ../../README.md"
51
+ system(command)
52
+ end
53
+ end
@@ -0,0 +1,6 @@
1
+ @[:markdown](classes/array/template.md)
2
+ @[:markdown](classes/hash/template.md)
3
+ @[:markdown](classes/struct/template.md)
4
+ @[:markdown](classes/string/template.md)
5
+ @[:markdown](classes/symbol/template.md)
6
+ @[:markdown](classes/object/template.md)
@@ -0,0 +1,5 @@
1
+ - [Array](#array)
2
+ - [Hash](#hash)
3
+ - [Struct](#struct)
4
+ - [String](#string)
5
+ - [Symbol](#symbol)
@@ -0,0 +1,27 @@
1
+ #### Circular Arrays
2
+
3
+ This example shows arrays that make a circular reference.
4
+
5
+ ```show.rb```:
6
+ ```ruby
7
+ require 'debug_helper'
8
+
9
+ ary_0 = []
10
+ ary_1 = []
11
+ ary_0.push(ary_1)
12
+ ary_1.push(ary_0)
13
+ DebugHelper.show(ary_0, 'My circular arrays')
14
+ ```
15
+
16
+ The output shows details of the arrays.
17
+
18
+ The circular reference is not followed.
19
+
20
+ ```show.yaml```:
21
+ ```yaml
22
+ ---
23
+ Array (name='My circular arrays' size=1):
24
+ Element 0:
25
+ Array (size=1):
26
+ Element 0: Array [[[...]]]
27
+ ```
@@ -0,0 +1,7 @@
1
+ require 'debug_helper'
2
+
3
+ ary_0 = []
4
+ ary_1 = []
5
+ ary_0.push(ary_1)
6
+ ary_1.push(ary_0)
7
+ DebugHelper.show(ary_0, 'My circular arrays')
@@ -0,0 +1,5 @@
1
+ ---
2
+ Array (name='My circular arrays' size=1):
3
+ Element 0:
4
+ Array (size=1):
5
+ Element 0: Array [[[...]]]
@@ -0,0 +1,11 @@
1
+ #### Circular Arrays
2
+
3
+ This example shows arrays that make a circular reference.
4
+
5
+ @[ruby](show.rb)
6
+
7
+ The output shows details of the arrays.
8
+
9
+ The circular reference is not followed.
10
+
11
+ @[yaml](show.yaml)
@@ -0,0 +1,25 @@
1
+ #### Mixed Array
2
+
3
+ This example shows an array of mixed values.
4
+
5
+ ```show.rb```:
6
+ ```ruby
7
+ require 'debug_helper'
8
+
9
+ ary = [0, 'one', :two]
10
+ DebugHelper.show(ary, 'My mixed array')
11
+ ```
12
+
13
+ The output shows details of the array.
14
+
15
+ ```show.yaml```:
16
+ ```yaml
17
+ ---
18
+ Array (name='My mixed array' size=3):
19
+ Element 0: Fixnum 0
20
+ Element 1:
21
+ String (size=3 encoding=UTF-8 ascii_only=true bytesize=3):
22
+ - one
23
+ Element 2:
24
+ Symbol (size=3 encoding=US-ASCII): :two
25
+ ```
@@ -0,0 +1,4 @@
1
+ require 'debug_helper'
2
+
3
+ ary = [0, 'one', :two]
4
+ DebugHelper.show(ary, 'My mixed array')
@@ -0,0 +1,8 @@
1
+ ---
2
+ Array (name='My mixed array' size=3):
3
+ Element 0: Fixnum 0
4
+ Element 1:
5
+ String (size=3 encoding=UTF-8 ascii_only=true bytesize=3):
6
+ - one
7
+ Element 2:
8
+ Symbol (size=3 encoding=US-ASCII): :two
@@ -0,0 +1,9 @@
1
+ #### Mixed Array
2
+
3
+ This example shows an array of mixed values.
4
+
5
+ @[ruby](show.rb)
6
+
7
+ The output shows details of the array.
8
+
9
+ @[yaml](show.yaml)
@@ -0,0 +1,28 @@
1
+ #### Nested Arrays
2
+
3
+ This example shows nested arrays.
4
+
5
+ ```show.rb```:
6
+ ```ruby
7
+ require 'debug_helper'
8
+
9
+ ary = [0, [1, 2], [3, 4]]
10
+ DebugHelper.show(ary, 'My nested arrays')
11
+ ```
12
+
13
+ The output shows details of the arrays.
14
+
15
+ ```show.yaml```:
16
+ ```yaml
17
+ ---
18
+ Array (name='My nested arrays' size=3):
19
+ Element 0: Fixnum 0
20
+ Element 1:
21
+ Array (size=2):
22
+ Element 0: Fixnum 1
23
+ Element 1: Fixnum 2
24
+ Element 2:
25
+ Array (size=2):
26
+ Element 0: Fixnum 3
27
+ Element 1: Fixnum 4
28
+ ```