Class: ApiDocsCommand

Inherits:
MoreUtils show all
Defined in:
lib/commands/api_docs.rb

Class Method Summary collapse

Methods inherited from MoreUtils

flag_lookup, gem_version, get_args, get_file_str, get_flags, root, this_dir, versions, write_file

Class Method Details

.last_end_index(arr) ⇒ Object



53
54
55
56
57
58
# File 'lib/commands/api_docs.rb', line 53

def last_end_index arr
  arr.each_with_index.inject(0) do |acc, (e, i)|
    acc = i if /(namespace)/.match(e) && acc == 0
    acc
  end
end

.run(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/commands/api_docs.rb', line 7

def run(*args)
  lookup = flag_lookup(args)

  version = versions(lookup)
  return version if !version
  
  regex = '{{ DOC_PATH }}'
  doc_regex = '{{ DOC_NAME }}'

  doc_name = version == 6 ? "javascript_pack_tag 'documentation'" : "javascript_include_tag 'application'"

  # Add Initializers
  doc_str = version.to_i == 6 ? '/../javascript/packs/documentation.js' : version.to_i == 5 ? '/../assets/javascripts/documentation.js' : ''
  adi_template = get_file_str("#{this_dir}/../templates/api_documentation_initializer.txt")
  write_file("#{root}/config/initializers/api_documentation_js.rb", adi_template)

  # Add Controllers
  dc_template = get_file_str("#{this_dir}/../templates/documentation_controller.txt")
  write_file("#{root}/app/controllers/documentation_controller.rb", dc_template)

  # Add Service
  system("mkdir -p #{root}/app/services")
  aps_template = get_file_str("#{this_dir}/../templates/api_documentation_service.txt")
  aps_template = aps_template.gsub(regex, doc_str)
  write_file("#{root}/app/services/api_documentation_service.rb", aps_template)

  # Add Views
  system("mkdir -p #{root}/app/views/documentation")
  dihtml_template = get_file_str("#{this_dir}/../templates/documentation.index.erb.txt")
  write_file("#{root}/app/views/documentation/index.html.erb", dihtml_template)

  dlhtml_template = get_file_str("#{this_dir}/../templates/documentation.layout.erb.txt")
  dlhtml_template = dlhtml_template.gsub(doc_regex, doc_name)
  write_file("#{root}/app/views/layouts/documentation.html.erb", dlhtml_template)

  # Add Routes
  routes_template = get_file_str("#{this_dir}/../templates/routes_documentation.txt")
  routes_file = get_file_str("#{root}/config/routes.rb")
  routes_arr = routes_file.split("\n")
  last_end_line = last_end_index(routes_arr)
  new_routes = routes_arr.slice(0, last_end_line).join("\n") + "\n#{routes_template}\n" + routes_arr.slice(last_end_line, routes_arr.length).join("\n")
  write_file("#{root}/config/routes.rb", new_routes)
  
  puts "Added Automatic API documentation to your project."
end

.wrong_version_errorObject



60
61
62
63
# File 'lib/commands/api_docs.rb', line 60

def wrong_version_error
  puts 'Must provide a version option'
  return nil
end