Class: Httpdoc::RubyCommentParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ RubyCommentParser

Returns a new instance of RubyCommentParser.



85
86
87
# File 'lib/httpdoc/parser.rb', line 85

def initialize(file_name)
  @file_name = file_name
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



154
155
156
# File 'lib/httpdoc/parser.rb', line 154

def controller
  @controller
end

Instance Method Details

#parseObject



89
90
91
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
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
# File 'lib/httpdoc/parser.rb', line 89

def parse
  reset
  File.open(@file_name) do |file|
    file.readlines.each do |line|
      case @state
        when :top
          case line
            when /\A\s*##(.*)/
              @buffer << $1
              @buffer << "\n"
              @state = :class_doc
          end
        when :class_doc
          case line
            when /\A\s*#+\s?(.*)/
              @buffer << $1
              @buffer << "\n"
            when /(^|\s*)class \w/
              unless @buffer.empty?                    
                @controller = ControllerDocParser.parse(@buffer)
                @buffer = ''
              end                  
              @state = :class_def
          end              
        when :class_def
          case line
            when /^\s*([A-Z_][A-Z0-9_]*)\s*=\s*(.*)/
              if @controller
                @controller.constants[$1] = $2
              end
            when /\A\s*##(.*)/
              @buffer << $1
              @state = :method_def
          end
        when :method_def
          case line
            when /\A\s*#+\s?(.*)/
              @buffer << $1
              @buffer << "\n"
            when /\A\s*def ([^\s#\(]+)/
              name = $1
              unless @buffer.empty?
                if @controller
                  ActionDocParser.parse(name, @buffer, @controller.actions)
                end
                @buffer = ''
              end
              @state = :class_def
            else
              unless @buffer.empty?
                if @controller
                  ActionDocParser.parse(nil, @buffer, @controller.actions)
                end
                @buffer = ''
              end
              @state = :class_def
          end
      end
    end
  end
  if @controller
    @controller.url ||= $1 if @file_name =~ /(\w+)_controller\./
  end
end