Class: Rage::OpenAPI::Collector
- Inherits:
-
Prism::Visitor
- Object
- Prism::Visitor
- Rage::OpenAPI::Collector
- Defined in:
- lib/rage/openapi/collector.rb
Overview
Collect all global comments or comments attached to methods in a class. At this point we don't care whether these are Rage OpenAPI comments or not.
Instance Method Summary collapse
- #dangling_comments ⇒ Object
-
#initialize(comments) ⇒ Collector
constructor
A new instance of Collector.
- #method_comments(method_name) ⇒ Object
- #visit_def_node(node) ⇒ Object
Constructor Details
#initialize(comments) ⇒ Collector
Returns a new instance of Collector.
8 9 10 11 |
# File 'lib/rage/openapi/collector.rb', line 8 def initialize(comments) @comments = comments.dup @method_comments = {} end |
Instance Method Details
#dangling_comments ⇒ Object
13 14 15 |
# File 'lib/rage/openapi/collector.rb', line 13 def dangling_comments @comments end |
#method_comments(method_name) ⇒ Object
17 18 19 |
# File 'lib/rage/openapi/collector.rb', line 17 def method_comments(method_name) @method_comments[method_name.to_s] end |
#visit_def_node(node) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rage/openapi/collector.rb', line 21 def visit_def_node(node) method_comments = [] start_line = node.location.start_line - 1 loop do comment_i = @comments.find_index { |comment| comment.location.start_line == start_line } if comment_i comment = @comments.delete_at(comment_i) method_comments << comment start_line -= 1 end break unless comment end @method_comments[node.name.to_s] = method_comments.reverse # reject comments inside methods @comments.reject! do |comment| comment.location.start_line >= node.location.start_line && comment.location.start_line <= node.location.end_line end end |