Class: Cdoc::CDoc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCDoc

Returns a new instance of CDoc.



121
122
123
124
125
# File 'lib/cdoc.rb', line 121

def initialize
  @files = Rake::FileList.new('app/controllers/**/*.rb')
  @files = @files.select { |file| file.end_with?('_controller.rb') }
  @doc = DocString.new
end

Instance Attribute Details

#docstringObject

Returns the value of attribute docstring.



119
120
121
# File 'lib/cdoc.rb', line 119

def docstring
  @docstring
end

#filesObject

Returns the value of attribute files.



119
120
121
# File 'lib/cdoc.rb', line 119

def files
  @files
end

Instance Method Details

#extract_documentation(file) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cdoc.rb', line 153

def extract_documentation(file)
  begin
    lines = File.readlines(file)
  rescue => e
    puts "#{e.class}: Error reading file #{file}. Skip"
    return
  end

  docs  = []
  doclines = []
  recording = false

  lines.each do |line|
    if !recording && (line.strip == '#doc')
      recording = true
      next
    end

    if recording
      line = line.strip

      if line.empty?
        next
      elsif line.start_with?('#')
        doclines << line.strip.sub('#', '')
      else
        recording = false
        docs << doclines.join("\n")
        doclines = []
      end
    end
  end

  if recording && (doclines.length != 0)
    recording = false
    docs << doclines.join("\n")
    doclines = []
  end

  docs
end

#generateObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cdoc.rb', line 133

def generate
  @doc.title('Chillr API Documentaion')

  @file_groups = files.group_by { |f| File.basename(f, '_controller.rb') }

  @sidebar = sidebar_section(@file_groups.keys)

  @file_groups.each do |group, files|
    @doc.section(group.capitalize)
    files.each do |file|
      docs = extract_documentation(file)
      docs.each do |doc|
        @doc.subsection(doc)
      end
    end
  end

  @doc.finish
end


127
128
129
130
131
# File 'lib/cdoc.rb', line 127

def sidebar_section(headers)
  headers.each do |title|
    title.capitalize
  end
end