Class: Apipie::Extractor::ActionDescriptionUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie/extractor/writer.rb

Defined Under Namespace

Classes: ActionNotFound, ControllerNotFound

Instance Method Summary collapse

Constructor Details

#initialize(controller, action) ⇒ ActionDescriptionUpdater

Returns a new instance of ActionDescriptionUpdater.



236
237
238
239
# File 'lib/apipie/extractor/writer.rb', line 236

def initialize(controller, action)
  @controller = controller
  @action = action
end

Instance Method Details

#generated?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/apipie/extractor/writer.rb', line 241

def generated?
  old_header.include?(Apipie.configuration.generated_doc_disclaimer)
end

#old_headerObject



267
268
269
270
271
272
273
274
# File 'lib/apipie/extractor/writer.rb', line 267

def old_header
  return @old_header if defined? @old_header
  @old_header = lines_above_method[/^\s*?#{Regexp.escape(Apipie.configuration.generated_doc_disclaimer)}.*/m]
  @old_header ||= lines_above_method[/^\s*?\b(api|params|error|example)\b.*/m]
  @old_header ||= ""
  @old_header.sub!(/\A\s*\n/,"")
  @old_header = align_indented(@old_header)
end

#update(new_header) ⇒ Object



263
264
265
# File 'lib/apipie/extractor/writer.rb', line 263

def update(new_header)
  overwrite_header(new_header)
end

#update_apis(apis) ⇒ Object



245
246
247
248
249
250
251
252
253
254
# File 'lib/apipie/extractor/writer.rb', line 245

def update_apis(apis)
  new_header = ""
  new_header << Apipie.configuration.generated_doc_disclaimer << "\n" if generated?
  new_header << generate_apis_code(apis)
  new_header << ensure_line_breaks(old_header.lines).reject do |line|
    line.include?(Apipie.configuration.generated_doc_disclaimer) ||
      line =~ /^api/
  end.join
  overwrite_header(new_header)
end

#update_generated_description(desc) ⇒ Object



256
257
258
259
260
261
# File 'lib/apipie/extractor/writer.rb', line 256

def update_generated_description(desc)
  if generated? || old_header.empty?
    new_header = generate_code(desc)
    overwrite_header(new_header)
  end
end

#write!Object



276
277
278
279
# File 'lib/apipie/extractor/writer.rb', line 276

def write!
  File.open(controller_path, "w") { |f| f << @controller_content }
  @changed=false
end