Module: Impl

Defined in:
lib/impl.rb

Constant Summary collapse

VERSION =
'1.2'

Class Method Summary collapse

Class Method Details

.direct(base, name) ⇒ Object



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
# File 'lib/impl.rb', line 55

def direct(base, name)
  unless File.exists?(base + 'tags')
    Dir.chdir base do
      system 'ctags -R'
    end
  end
  if /^String#sum/ !~ File.read(base + 'tags')
    Dir.chdir base do
      system 'impl -c .', out: ['tags', 'a']
    end
  end

  x = File.read(base + 'tags').
    each_line.
    select {|l| /^#{Regexp.escape name}\t/ =~ l }.
    map {|l| l.split("\t") }.
    first
  r = Regexp.new(x[2][1..-4].gsub(/[\(\*\)]/) {|x| '\\' << x })
  file = base + x[1]
  content = File.read(file)
  line, i = content.
    each_line.
    with_index.
    detect {|line, _| r =~ line }
  content.each_line.drop(i).take_until {|line| /^}/ =~ line }
end

.generate(ctags, dir) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/impl.rb', line 15

def generate(ctags, dir)
  memo = []
  Dir.glob(dir + '/**/*.c') do |f|
    File.read(f).each_line do |line|
      if /^\s*rb_define_(singleton_)?(?:private_)?method\(rb_.(.*?),\s*"(.*?)",\s*(\w+),(?#\))/ =~ line
        is_singleton, klass, method, func = [!!$1, $2, $3, $4]
        fullmethod = [klass, is_singleton ? '.' : '#', method].join
        ctags.scan(/^#{func}\t(.*)/) do |x|
          memo << "#{fullmethod}\t#{x[0]}"
        end
      end
    end
  end
  memo
end

.helpObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/impl.rb', line 32

def help
  "  impl version \#{VERSION}\n\n  USAGE\n    impl -h:\n             shows this message\n    impl {directory} {method name}\n             shows the file name and line number where {method name} like\n             'Strimg#sum' defined, assuming the Ruby source directory is\n             {directory}.\n    impl -d {directory} {method name}\n    impl --description {directory} {method name}\n             shows the description of {method name} like 'Strimg#sum'\n             directly, assuming the Ruby source directory is {directory}.\n    impl -c {directory}:\n    impl --create {directory}:\n             shows additional information, assuming you already have\n             |tags| and Ruby codes in the current directory.\n  EOH\nend\n"