Sha256: 0f004cdb87336c02eb50b88ca5077e92a1350dab88eb6b42cae7ec3ad261ce7e

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Rapidoc

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

    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

    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 bootstrap label for a method
    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

    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

    def create_action_template( template, action_doc )
      result = template.call( :info => rapidoc_config, :action => action_doc )
      File.open( actions_dir("#{action_doc.file}.html"), 'w' ) { |file| file.write result }
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rapidoc-0.0.6 lib/rapidoc/templates_generator.rb
rapidoc-0.0.5 lib/rapidoc/templates_generator.rb
rapidoc-0.0.4 lib/rapidoc/templates_generator.rb