Class: RapiDoc::ResourceDoc

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

Overview

ResourceDoc holds the information a resource contains. It parses the class header and also the method documentation, which will be contained in MethodDoc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, resource_location, controller_location, options = {}) ⇒ ResourceDoc

Initializes ResourceDoc.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rapi_doc/resource_doc.rb', line 15

def initialize(name, resource_location, controller_location, options = {})
  @name = name
  @class_block = nil
  @function_blocks = []
  @resource_methods = []
  @resource_header = ""
  @standard_methods = options[:standard_methods] || [:put, :post, :get, :delete]
  @resource_location = resource_location
  @controller_location = controller_location
  @controller_name = File.basename(controller_location)
end

Instance Attribute Details

#class_blockObject (readonly)

Returns the value of attribute class_block.



12
13
14
# File 'lib/rapi_doc/resource_doc.rb', line 12

def class_block
  @class_block
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



12
13
14
# File 'lib/rapi_doc/resource_doc.rb', line 12

def controller_name
  @controller_name
end

#function_blocksObject (readonly)

Returns the value of attribute function_blocks.



12
13
14
# File 'lib/rapi_doc/resource_doc.rb', line 12

def function_blocks
  @function_blocks
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/rapi_doc/resource_doc.rb', line 12

def name
  @name
end

#resource_headerObject (readonly)

Returns the value of attribute resource_header.



12
13
14
# File 'lib/rapi_doc/resource_doc.rb', line 12

def resource_header
  @resource_header
end

#resource_locationObject (readonly)

Returns the value of attribute resource_location.



12
13
14
# File 'lib/rapi_doc/resource_doc.rb', line 12

def resource_location
  @resource_location
end

#resource_methodsObject (readonly)

Returns the value of attribute resource_methods.



12
13
14
# File 'lib/rapi_doc/resource_doc.rb', line 12

def resource_methods
  @resource_methods
end

Class Method Details

.parse_controller_doc(name, lines) ⇒ Object

This method parses the doc



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rapi_doc/resource_doc.rb', line 44

def self.parse_controller_doc(name, lines)
  current_api_block = nil
  current_scope = :none
  in_class = false
  class_block = nil
  function_blocks = []
  order = 1
  lines.each_with_index do |line, line_no|
    line.gsub!(/^ *#/, '') # strip the starting '#' on the line
    case line
      when /=begin apidoc/
        # if we get apidoc tag inside class definition, then they are for a method
        current_scope = !in_class ? :class : :function
        current_api_block = MethodDoc.new(name, current_scope, order)
      when /=end/
        if current_api_block.nil?
          raise ParsingException, "#{line_no} - No starttag for '=end' found"
        else
          case current_scope
            when :class
              class_block = current_api_block
            when :function
              function_blocks << current_api_block
            else
              raise ParsingException, "logic error: unknown current scope #{current_scope}"
          end
          current_api_block = nil
          current_scope = :none
          order += 1
        end
      when /class/ # keep track of whether a resource or an api is being annotated
        in_class = true
      else
        if current_api_block # process lines only if they are apidoc comments
          current_scope = current_api_block.process_line(line, current_scope)
        end
    end
  end
  [class_block, function_blocks]
end

Instance Method Details

#parse_apidoc!(class_template, method_template) ⇒ Object

parse the controller



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rapi_doc/resource_doc.rb', line 28

def parse_apidoc!(class_template, method_template)
  lines = IO.readlines(@controller_location)
  begin
    @class_block, @function_blocks = ResourceDoc.parse_controller_doc(@name, lines)
  rescue ParsingException => ex
    puts "error #{ex} while parsing #{@controller_location}"
    exit
  else
    @resource_header = Haml::Engine.new(class_template).render(@class_block) unless class_block.nil?
    @resource_methods = @function_blocks.each_with_index.collect do |method_block, i|
      Haml::Engine.new(method_template).render(method_block)
    end
  end
end