Class: Rdefs

Inherits:
Object
  • Object
show all
Defined in:
lib/rdefs.rb

Defined Under Namespace

Classes: Preprocessor

Constant Summary collapse

CLASS_REGEX =
/\A\s*(?:
class\s | module\s | include[\s\(]
)/x
DEF_REGEX =
/\A\s*
    (?: def\s
| class\s
| module\s
| include\b
| alias(?:_\w+)?\b
| attr_reader\b
| attr_writer\b
| attr_accessor\b
| attr\b
| public\b
| private\b
| protected\b
| module_function\b
)/x

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Rdefs

Returns a new instance of Rdefs.



45
46
47
48
49
50
# File 'lib/rdefs.rb', line 45

def initialize
  @re = DEF_REGEX
  @print_line_number_p = false
  @f = Preprocessor.new(ARGF)
  parse_options
end

Instance Method Details

#balanced?(str) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/rdefs.rb', line 93

def balanced?(str)
  s = str.gsub(/'.*?'/, '').gsub(/".*?"/, '')
  s.count('(') == s.count(')')
end

#getdef(str, f) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/rdefs.rb', line 84

def getdef(str, f)
  until balanced?(str)
    line = f.gets
    break unless line
    str << line
  end
  str
end

#parse_options ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rdefs.rb', line 52

def parse_options
  options = OptionParser.new do |opt|
    opt.banner = "#{File.basename($0)} [-n] [file...]"
    opt.on('--class', 'Show only classes and modules') {
      @re = CLASS_REGEX
    }
    opt.on('-n', '--lineno', 'Prints line number.') {
      @print_line_number_p = true
    }
    opt.on('--help', 'Prints this message and quit.') {
    puts opt.help
      exit 0
    }
  end

  begin
    options.parse!
  rescue OptionParser::ParseError => err
    $stderr.puts err.message
    exit 1
  end
end

#process ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/rdefs.rb', line 75

def process
  while line = @f.gets
    if @re =~ line
      printf '%4d: ', @f.lineno if @print_line_number_p
      print getdef(line, @f)
    end
  end
end