Class: Restapi::Extractor::ActionDescriptionUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/restapi/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.



136
137
138
139
# File 'lib/restapi/extractor/writer.rb', line 136

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

Instance Method Details

#generated?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/restapi/extractor/writer.rb', line 141

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

#old_headerObject



167
168
169
170
171
172
173
174
# File 'lib/restapi/extractor/writer.rb', line 167

def old_header
  return @old_header if defined? @old_header
  @old_header = lines_above_method[/^\s*?#{Regexp.escape(Restapi.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



163
164
165
# File 'lib/restapi/extractor/writer.rb', line 163

def update(new_header)
  overwrite_header(new_header)
end

#update_apis(apis) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/restapi/extractor/writer.rb', line 145

def update_apis(apis)
  new_header = ""
  new_header << Restapi.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?(Restapi.configuration.generated_doc_disclaimer) ||
      line =~ /^api/
  end.join
  overwrite_header(new_header)
end

#update_generated_description(desc) ⇒ Object



156
157
158
159
160
161
# File 'lib/restapi/extractor/writer.rb', line 156

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

#write!Object



176
177
178
179
# File 'lib/restapi/extractor/writer.rb', line 176

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