Class: BitClust::Subcommands::MethodsCommand

Inherits:
BitClust::Subcommand show all
Includes:
CrossRubyUtils
Defined in:
lib/bitclust/subcommands/methods_command.rb

Constant Summary collapse

ORDER =
{ '.' => 1, '#' => 2, '::' => 3 }

Instance Method Summary collapse

Methods inherited from BitClust::Subcommand

#align_progress_bar_title, #error, #help, #option_error, #srcdir_root

Constructor Details

#initializeMethodsCommand

Returns a new instance of MethodsCommand.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bitclust/subcommands/methods_command.rb', line 14

def initialize
  super
  @requires = []
  @verbose = false
  @version = RUBY_VERSION
  @mode = :list
  @target = nil
  @parser.banner = "Usage: #{File.basename($0, '.*')} [-r<lib>] <classname>"
  @parser.on('-r LIB', 'Requires library LIB') {|lib|
    @requires.push lib
  }
  @parser.on('-v', '--verbose', "Prints each ruby's version") {
    @verbose = true
  }
  @parser.on('--diff=RDFILE', 'RD file name') {|path|
    @mode = :diff
    @target = path
  }
  @parser.on('-c', '') {
    @content = true
    require 'bitclust/ridatabase'
  }
  @parser.on('--ruby=[VER]', "The version of Ruby interpreter"){|version|
    @version = version
  }
  @parser.on('--ri-database', 'The path of ri database'){|path|
    @ri_path = path
  }
end

Instance Method Details

#crossrubyutils_sort_entries(ents) ⇒ Object



99
100
101
# File 'lib/bitclust/subcommands/methods_command.rb', line 99

def crossrubyutils_sort_entries(ents)
  ents.sort_by {|m| m_order(m) }
end

#defined_methods(ruby, classname) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bitclust/subcommands/methods_command.rb', line 110

def defined_methods(ruby, classname)
  req = @requires.map {|lib| "-r#{lib}" }.join(' ')
  avoid_tracer = ""
  avoid_tracer = "Tracer.off" if @requires.include?("tracer")
  case classname
  when 'Object'
    script = <<-SCRIPT
      c = #{classname}
      c.singleton_methods(false).each do |m|
        puts "#{classname}.\#{m}"
      end
      c.instance_methods(true).each do |m|
        puts "#{classname}\\#\#{m}"
      end
    SCRIPT
  when 'Kernel'
    script = <<-SCRIPT
      c = #{classname}
      c.singleton_methods(true).each do |m|
        puts "#{classname}.\#{m}"
      end
      ( c.private_instance_methods(false) && c.methods(false) ).each do |m|
        puts "#{classname}\\#\#{m}"
      end
      Object::constants.delete_if{|c| cl = Object.const_get(c).class; cl == Class or cl == Module }.each do |m|
        puts "#{classname}::\#{m}"
      end
      global_variables.each do |m|
        puts "#{classname}\#{m}"
      end
    SCRIPT
  when 'ARGF'
    script = <<-SCRIPT
      c = #{classname}.class
      c.singleton_methods(false).each do |m|
        puts "\#{c.to_s}.\#{m}"
      end
      c.instance_methods(false).each do |m|
        puts "\#{c.to_s}\\#\#{m}"
      end
      c.ancestors.map {|mod| mod.constants }.inject {|r,n| r - n }.each do |m|
        puts "\#{c.to_s}::\#{m}"
      end
    SCRIPT
  else
    script = <<-SCRIPT
      #{avoid_tracer}
      c = #{classname}
      c.singleton_methods(false).each do |m|
        puts "#{classname}.\#{m}"
      end
      c.instance_methods(false).each do |m|
        puts "#{classname}\\#\#{m}"
      end
      c.ancestors.map {|mod| mod.constants }.inject {|r,n| r-n }.each do |m|
        puts "#{classname}::\#{m}"
      end
    SCRIPT
  end
  `#{ruby} #{req} -e '#{script}'`.split
end

#exec(argv, options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bitclust/subcommands/methods_command.rb', line 49

def exec(argv, options)
  classname = argv[0]
  case @mode
  when :list
    print_crossruby_table {|ruby| defined_methods(ruby, classname) }
  when :diff
    unless ruby = get_ruby(@version)
      raise "Not found Ruby interpreter of the given version"
    end
    keys = defined_methods(ruby, classname)
    lib = RRDParser.parse_stdlib_file(@target, { 'version' => @version })
    c = lib.fetch_class(classname)
    list0 = lib.classes.find_all{|c0| /\A#{classname}\b/o =~ c0.name }
    list0 = c.entries + list0
    list = list0.map {|ent| ent.labels.map {|n| expand_mf(n) } }.flatten
    if @content
      ri = @ri_path ? RiDatabase.open(@ri_path, nil) : RiDatabase.open_system_db
      ri.current_class = c.name
      mthds = ( ri.singleton_methods + ri.instance_methods )
      fmt = Formatter.new
      (keys - list).sort.each do |name|
        mthd = mthds.find{|m| name == m.fullname }
        if mthd
          puts fmt.method_info(mthd.entry)
        else
          name = name.sub(/\A\w+#/, '')
          puts "--- #{name}\n\#@todo\n\n"
        end
      end
    else
      (keys - list).sort.each do |name|
        puts "-#{name}"
      end
      (list - keys).sort.each do |name|
        puts "+#{name}"
      end
    end
  else
    raise "must not happen: #{mode.inspect}"
  end
end

#expand_mf(n) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/bitclust/subcommands/methods_command.rb', line 91

def expand_mf(n)
  if /\.\#/ =~ n
    [n.sub(/\.\#/, '.'), n.sub(/\.\#/, '#')]
  else
    n
  end
end

#m_order(m) ⇒ Object



105
106
107
108
# File 'lib/bitclust/subcommands/methods_command.rb', line 105

def m_order(m)
  m, t = *m.reverse.split(/(\#|\.|::)/, 2)
  [ORDER[t] || 0, m.reverse]
end

#parse(argv) ⇒ Object



44
45
46
47
# File 'lib/bitclust/subcommands/methods_command.rb', line 44

def parse(argv)
  super
  option_error("wrong number of arguments") unless argv.size == 1
end