Class: DocWriter

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

Overview

This class writes the parsed documenation to the swagger_helper file.

Instance Method Summary collapse

Instance Method Details

#apply_original_indentation(new_documentation, fragment_title) ⇒ Object



38
39
40
41
# File 'lib/doc_writer.rb', line 38

def apply_original_indentation(new_documentation, fragment_title)
  indentation = ' ' * original_indent_level(fragment_title)
  new_documentation.split("\n").join("\n" + indentation)
end

#docs_pathObject



5
6
7
# File 'lib/doc_writer.rb', line 5

def docs_path
  Rails.root.join('spec', 'swagger_helper.rb').to_s
end

#example_hash(fragment_title, old_documentation) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/doc_writer.rb', line 19

def example_hash(fragment_title, old_documentation)
  match = ''
  iterator = 1
  loop do
    regex = /(#{fragment_title})(.*?\}){#{iterator}}/
    new_match_data = old_documentation.match(regex)
    new_match = new_match_data.present? ? new_match_data[0] : nil
    return unless new_match != match

    match = new_match
    begin
      return match if eval("{#{match}}")
    rescue SyntaxError
    end

    iterator += 1
  end
end

#original_indent_level(fragment_title) ⇒ Object



43
44
45
46
# File 'lib/doc_writer.rb', line 43

def original_indent_level(fragment_title)
  file = File.read(docs_path)
  file.match(/\s+(?=#{fragment_title})/)[0].size - 1
end

#write_docs(docs, fragment_title) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/doc_writer.rb', line 9

def write_docs(docs, fragment_title)
  old_documentation = File.read(docs_path)
  old_documentation_fragment = example_hash(fragment_title, old_documentation)
  return if old_documentation_fragment.nil?

  new_documentation_fragment = apply_original_indentation(docs, fragment_title)
  new_documentation = old_documentation.sub(old_documentation_fragment, new_documentation_fragment)
  File.open(docs_path, 'w') { |f| f.write(new_documentation) }
end