Module: Rapidoc::TemplatesGenerator

Included in:
Rapidoc
Defined in:
lib/rapidoc/templates_generator.rb

Overview

This module let generate index file and actions files from templates.

Instance Method Summary collapse

Instance Method Details

#create_action_template(template, action_doc) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rapidoc/templates_generator.rb', line 53

def create_action_template( template, action_doc )
  result = template.call( :info => rapidoc_config, :action => action_doc )
  resource = action_doc.resource.split('/').last
  action = action_doc.action
  dir_name = File.dirname(actions_dir("#{resource}"))
  dir_name += "/#{resource}"
  unless File.directory?(dir_name)
    FileUtils.mkdir_p(dir_name)
  end
  File.open( actions_dir("#{resource}/#{action}.html"), 'w' ) { |file| file.write result }
end

#generate_actions_templates(resources_doc) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rapidoc/templates_generator.rb', line 41

def generate_actions_templates( resources_doc )
  resources_doc.each do |resource|
    if resource.actions_doc
      resource.actions_doc.each do |action_doc|
        if action_doc.has_controller_info
          create_action_template( get_action_template, action_doc )
        end
      end
    end
  end
end

#generate_index_template(resources_doc) ⇒ Object



8
9
10
11
12
# File 'lib/rapidoc/templates_generator.rb', line 8

def generate_index_template( resources_doc )
  template = get_index_template
  result = template.call( :info => rapidoc_config, :resources => resources_doc )
  File.open( target_dir("index.html"), 'w' ) { |file| file.write result }
end

#get_action_templateObject



65
66
67
68
69
# File 'lib/rapidoc/templates_generator.rb', line 65

def get_action_template
  template = IO.read( gem_templates_dir('action.html.hbs') )
  handlebars = Handlebars::Context.new
  handlebars.compile( template )
end

#get_index_templateObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/rapidoc/templates_generator.rb', line 14

def get_index_template
  template = IO.read( gem_templates_dir('index.html.hbs') )
  handlebars = Handlebars::Context.new

  handlebars.register_helper('method_label') do |this, context, block|
    get_method_label( block.fn(context) )
  end

  handlebars.compile( template )
end

#get_method_label(method) ⇒ Object

Get bootstrap label for a method



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rapidoc/templates_generator.rb', line 26

def get_method_label( method )
  case method
  when 'GET'
    'label label-info'
  when 'POST'
    'label label-success'
  when 'PUT'
    'label label-warning'
  when 'DELETE'
    'label label-important'
  else
    'label'
  end
end