ClassBrowser 1.0.1 → 1.0.2

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
  SHA1:
3
- metadata.gz: 2212f8e9247ef7b04d813d71cec1e7a6d86d1d1f
4
- data.tar.gz: bf29e15f87efe4cbdce77fc103890beeabe6f6f6
3
+ metadata.gz: 170630389503dadbd207003e5886dd06af6df094
4
+ data.tar.gz: 2272dbe3b246fc93fa22f0aaa5c70b65f4aa52e3
5
5
  SHA512:
6
- metadata.gz: ab6d006238797bb1b703829bb4ab3833407bb334e4f8a3fedf35d72740a13dee9f663f1a6510d21ddca6f6bd6be5b700070d81111a6cc22438ccbaeafe8fc5dd
7
- data.tar.gz: 14815e27cefe3a983179a11c927470641d1510a9e976d61c6f4c20b781be522b1ad5c9fad1d0826ae23446cbc485926bfd93f983a5525e13c590d1714ddd274b
6
+ metadata.gz: 4b8c380fdc955225981e230b92c43de7c02170d7dc25306c8fee260382af3ad1f03bd699eae5a3ec8778c99046c3a6d6a2b60de78f4bb8e2c00b0e6e31309102
7
+ data.tar.gz: 8b7dfc565e50d41017c4c32c401fad923dd36e510036043f92186fcc2ee511af10b1ed35c35ceb84c5bea00b12c1d0f947c874ef5be02ccac97dd37976bdd0a8
data/lib/ClassBrowser.rb CHANGED
@@ -3,33 +3,121 @@ require_relative 'HierarchyWriter'
3
3
 
4
4
  class ClassBrowser
5
5
  attr_reader :class_root_node
6
+ attr_reader :show_help
6
7
  attr_reader :depth
8
+ attr_reader :show_modules
9
+ attr_reader :show_methods
7
10
 
8
11
  def initialize root_class = Object
9
12
  @class_root_node = ClassNode.new root_class
13
+ @show_help = false
10
14
  @depth = :depth_immediate
15
+ @show_modules = false
16
+ @show_methods = :methods_none
11
17
  end
12
18
 
13
19
  def dump_hierarchy
14
20
  HierarchyWriter::dump_hierarchy_of @class_root_node, @depth
15
21
  end
16
22
 
17
- def parse_arguments argv
18
- if argv.include? "-di"
19
- @depth = :depth_immediate
23
+ def pretty_print prefix, methods, suffix = ""
24
+ methods.sort_by { |m| m.to_s }
25
+ width = ENV['COLUMNS'].to_i
26
+ col = 0
27
+ methods.each do |m|
28
+ msg = '%-24.24s' % "#{prefix}#{m.to_s}#{suffix}"
29
+ col += msg.length
30
+ if col >= width
31
+ print "\n"
32
+ col = msg.length
33
+ end
34
+ print msg
35
+ end
36
+ print "\n"
37
+ end
38
+
39
+ def dump_modules
40
+ if @class_root_node.klass && @show_modules
41
+
42
+ modules = @class_root_node.klass.ancestors.select { |a| a.class == Module }
43
+ pretty_print "[", modules, "]"
44
+ end
45
+ end
46
+
47
+ def dump_methods
48
+ if @class_root_node.klass && @show_methods != :methods_none
49
+ if @show_methods == :methods_all || @show_methods == :methods_class
50
+ methods = @class_root_node.klass.singleton_methods
51
+ pretty_print "::", methods
52
+ end
53
+ if @show_methods == :methods_all || @show_methods == :methods_instance
54
+ methods = @class_root_node.klass.instance_methods(false)
55
+ pretty_print "#", methods
56
+ end
57
+ end
58
+ end
59
+
60
+ def show_help
61
+ if @show_help
62
+ puts "Usage: ClassBrowser [class] [switches]
63
+ Where:
64
+ class is a Class or Module name
65
+ switches may be:
66
+ -h: show this message
67
+ -da: show the all descendants of this class
68
+ -di: show the immediate descendants of this class
69
+ -dn: do not show the descendants of this class
70
+ -m: show the Modules included by this Class or Module
71
+ -ma: show all methods of this Class or Module
72
+ -mi: show the instance methods of this Class
73
+ -mc: show the class methods of this Class
74
+ -mn: do not show any methods of this Class or Module
75
+ ClassBrowser with no arguments enters interactive mode
76
+ "
77
+ true
78
+ else
79
+ false
20
80
  end
21
- if argv.include? "-da"
22
- @depth = :depth_all
81
+ end
82
+
83
+ def dump
84
+ if !show_help
85
+ dump_hierarchy
86
+ dump_modules
87
+ dump_methods
88
+ end
89
+ end
90
+
91
+
92
+ def parse_arguments argv
93
+ flags = {
94
+ "-h" => lambda { @show_help = true },
95
+ "-di" => lambda { @depth = :depth_immediate },
96
+ "-da" => lambda { @depth = :depth_all },
97
+ "-dn" => lambda { @depth = :depth_none },
98
+ "-m" => lambda { @show_modules = true },
99
+ "-mn" => lambda { @show_methods = :methods_none },
100
+ "-ma" => lambda { @show_methods = :methods_all },
101
+ "-mi" => lambda { @show_methods = :methods_instance },
102
+ "-mc" => lambda { @show_methods = :methods_class },
103
+ }
104
+
105
+ argv.each do |arg|
106
+ l = flags[arg]
107
+ if l
108
+ l.call
109
+ end
23
110
  end
24
111
 
25
112
  class_name_index = argv.index{ |o| !o.start_with?("-") }
26
113
  if class_name_index
27
114
  class_name = argv[class_name_index]
28
- klass = Object
115
+ klass = nil
29
116
  begin
30
117
  klass = Object.const_get(class_name)
31
118
  rescue
32
- klass = Object
119
+ puts "Unknown class"
120
+ klass = nil
33
121
  end
34
122
  @class_root_node = ClassNode.new klass
35
123
  end
@@ -40,25 +128,31 @@ class ClassBrowser
40
128
  failsafe = 1
41
129
 
42
130
  loop do
43
- args = gets.split(" ")
131
+ print "\n#{@class_root_node.name}> "
132
+ args = gets.split(/\s+/)
44
133
 
45
134
  if args == nil || args.length == 0
46
135
  break
47
136
  end
48
137
 
49
138
  parse_arguments args
50
- dump_hierarchy
139
+ dump
51
140
 
52
141
  failsafe += 1
53
142
  break if failsafe > 100
54
143
  end
55
144
  end
56
145
  end
57
-
58
- #modules = cls.ancestors.select { |a| a.class == Module }.to_s
59
146
 
60
147
  def main
61
148
  browser = ClassBrowser.new
62
- browser.parse_arguments ARGV
63
- browser.dump_hierarchy
149
+ if ARGV.length > 0
150
+ browser.parse_arguments ARGV
151
+ browser.dump
152
+ else
153
+ browser.interactive
154
+ puts "Bye!"
155
+ end
64
156
  end
157
+
158
+ main
data/lib/ClassNode.rb CHANGED
@@ -6,40 +6,44 @@ class ClassNode
6
6
  end
7
7
 
8
8
  def name
9
- @klass.name
9
+ if @klass
10
+ @klass.name
11
+ else
12
+ "*Unknown class*"
13
+ end
10
14
  end
11
15
 
12
16
  def ancestors
13
17
  class_nodes = []
14
- @klass.ancestors[1..-1].each do |c|
15
- if c.class == Class
16
- class_node = ClassNode.new c
17
- class_nodes.insert 0, class_node
18
+ if @klass
19
+ @klass.ancestors[1..-1].each do |c|
20
+ if c.class == Class
21
+ class_node = ClassNode.new c
22
+ class_nodes.insert 0, class_node
23
+ end
18
24
  end
19
25
  end
20
26
  class_nodes
21
27
  end
22
28
 
23
29
  def descendants
24
- klass = @klass
25
-
26
- klasses = ObjectSpace.each_object(Class).select do |c|
27
- c.superclass == klass
28
- end
29
-
30
- klasses.reject! { |c| c.name == nil }
31
-
32
- klasses.sort_by do |c|
33
- c.name
34
- end
35
-
36
30
  class_nodes = []
31
+ if @klass
32
+ klasses = ObjectSpace.each_object(Class).select do |c|
33
+ c.superclass == @klass
34
+ end
37
35
 
38
- klasses.each do |c|
39
- class_node = ClassNode.new c
40
- class_nodes << class_node
41
- end
36
+ klasses.reject! { |c| c.name == nil }
42
37
 
38
+ klasses.sort_by do |c|
39
+ c.name
40
+ end
41
+
42
+ klasses.each do |c|
43
+ class_node = ClassNode.new c
44
+ class_nodes << class_node
45
+ end
46
+ end
43
47
  class_nodes
44
48
  end
45
49
 
@@ -55,6 +55,7 @@ class HierarchyWriter
55
55
  indent
56
56
  end
57
57
 
58
+ # prints the ancestors and descendants of node
58
59
  def self.dump_hierarchy_of node, depth = :depth_all
59
60
  indent = HierarchyWriter::dump_ancestors_of node
60
61
  HierarchyWriter::dump_descendants_of node, indent, depth
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ClassBrowser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Underhill