Class: ClassBrowser
- Inherits:
-
Object
- Object
- ClassBrowser
- Defined in:
- lib/ClassBrowser.rb
Instance Attribute Summary collapse
-
#class_root_node ⇒ Object
readonly
Returns the value of attribute class_root_node.
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#show_help ⇒ Object
readonly
Returns the value of attribute show_help.
-
#show_methods ⇒ Object
readonly
Returns the value of attribute show_methods.
-
#show_modules ⇒ Object
readonly
Returns the value of attribute show_modules.
Instance Method Summary collapse
- #dump ⇒ Object
- #dump_hierarchy ⇒ Object
- #dump_methods ⇒ Object
- #dump_modules ⇒ Object
-
#initialize(root_class = Object) ⇒ ClassBrowser
constructor
A new instance of ClassBrowser.
- #interactive ⇒ Object
- #parse_arguments(argv) ⇒ Object
- #pretty_print(prefix, methods, suffix = "") ⇒ Object
Constructor Details
#initialize(root_class = Object) ⇒ ClassBrowser
Returns a new instance of ClassBrowser.
11 12 13 14 15 16 17 |
# File 'lib/ClassBrowser.rb', line 11 def initialize root_class = Object @class_root_node = ClassNode.new root_class @show_help = false @depth = :depth_immediate @show_modules = false @show_methods = :methods_none end |
Instance Attribute Details
#class_root_node ⇒ Object (readonly)
Returns the value of attribute class_root_node.
5 6 7 |
# File 'lib/ClassBrowser.rb', line 5 def class_root_node @class_root_node end |
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
7 8 9 |
# File 'lib/ClassBrowser.rb', line 7 def depth @depth end |
#show_help ⇒ Object (readonly)
Returns the value of attribute show_help.
6 7 8 |
# File 'lib/ClassBrowser.rb', line 6 def show_help @show_help end |
#show_methods ⇒ Object (readonly)
Returns the value of attribute show_methods.
9 10 11 |
# File 'lib/ClassBrowser.rb', line 9 def show_methods @show_methods end |
#show_modules ⇒ Object (readonly)
Returns the value of attribute show_modules.
8 9 10 |
# File 'lib/ClassBrowser.rb', line 8 def show_modules @show_modules end |
Instance Method Details
#dump ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/ClassBrowser.rb', line 83 def dump if !show_help dump_hierarchy dump_modules dump_methods end end |
#dump_hierarchy ⇒ Object
19 20 21 |
# File 'lib/ClassBrowser.rb', line 19 def dump_hierarchy HierarchyWriter::dump_hierarchy_of @class_root_node, @depth end |
#dump_methods ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ClassBrowser.rb', line 47 def dump_methods if @class_root_node.klass && @show_methods != :methods_none if @show_methods == :methods_all || @show_methods == :methods_class methods = @class_root_node.klass.singleton_methods pretty_print "::", methods end if @show_methods == :methods_all || @show_methods == :methods_instance methods = @class_root_node.klass.instance_methods(false) pretty_print "#", methods end end end |
#dump_modules ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/ClassBrowser.rb', line 39 def dump_modules if @class_root_node.klass && @show_modules modules = @class_root_node.klass.ancestors.select { |a| a.class == Module } pretty_print "[", modules, "]" end end |
#interactive ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ClassBrowser.rb', line 126 def interactive failsafe = 1 loop do print "\n#{@class_root_node.name}> " args = gets.split(/\s+/) if args == nil || args.length == 0 break end parse_arguments args dump failsafe += 1 break if failsafe > 100 end end |
#parse_arguments(argv) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ClassBrowser.rb', line 92 def parse_arguments argv flags = { "-h" => lambda { @show_help = true }, "-di" => lambda { @depth = :depth_immediate }, "-da" => lambda { @depth = :depth_all }, "-dn" => lambda { @depth = :depth_none }, "-m" => lambda { @show_modules = true }, "-mn" => lambda { @show_methods = :methods_none }, "-ma" => lambda { @show_methods = :methods_all }, "-mi" => lambda { @show_methods = :methods_instance }, "-mc" => lambda { @show_methods = :methods_class }, } argv.each do |arg| l = flags[arg] if l l.call end end class_name_index = argv.index{ |o| !o.start_with?("-") } if class_name_index class_name = argv[class_name_index] klass = nil begin klass = Object.const_get(class_name) rescue puts "Unknown class" klass = nil end @class_root_node = ClassNode.new klass end end |
#pretty_print(prefix, methods, suffix = "") ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ClassBrowser.rb', line 23 def pretty_print prefix, methods, suffix = "" methods.sort_by { |m| m.to_s } width = ENV['COLUMNS'].to_i col = 0 methods.each do |m| msg = '%-24.24s' % "#{prefix}#{m.to_s}#{suffix}" col += msg.length if col >= width print "\n" col = msg.length end print msg end print "\n" end |