Class: RestApiDoc::ResourceDoc

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes ResourceDoc.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/restapi_doc/resource_doc.rb', line 12

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

Instance Attribute Details

#class_blockObject (readonly)

Returns the value of attribute class_block.



9
10
11
# File 'lib/restapi_doc/resource_doc.rb', line 9

def class_block
  @class_block
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



9
10
11
# File 'lib/restapi_doc/resource_doc.rb', line 9

def controller_name
  @controller_name
end

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/restapi_doc/resource_doc.rb', line 9

def description
  @description
end

#function_blocksObject (readonly)

Returns the value of attribute function_blocks.



9
10
11
# File 'lib/restapi_doc/resource_doc.rb', line 9

def function_blocks
  @function_blocks
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/restapi_doc/resource_doc.rb', line 9

def name
  @name
end

#resource_headerObject (readonly)

Returns the value of attribute resource_header.



9
10
11
# File 'lib/restapi_doc/resource_doc.rb', line 9

def resource_header
  @resource_header
end

#resource_locationObject (readonly)

Returns the value of attribute resource_location.



9
10
11
# File 'lib/restapi_doc/resource_doc.rb', line 9

def resource_location
  @resource_location
end

#resource_methodsObject (readonly)

Returns the value of attribute resource_methods.



9
10
11
# File 'lib/restapi_doc/resource_doc.rb', line 9

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/restapi_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"
        end
      when /class/ # keep track of whether a resource or an api is being annotated
        in_class = true
        if !current_api_block.nil?
        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 /def /
        if !current_api_block.nil?
          current_api_block.defname = line.gsub('def ', '').strip
          
          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
      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_apidocObject

parse the controller



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

def parse_apidoc
  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
  end
  if !@class_block.nil?
    @description = @class_block.content
  end
  
  @resource_methods.each do | req_m |
      req_m << @function_blocks.select{|f| f.defname == req_m[0]}.first
  end
end