Class: MarkdownRubyDocumentation::ClassLevelComment

Inherits:
Object
  • Object
show all
Includes:
TemplateParser::Parsing
Defined in:
lib/markdown_ruby_documentation/class_level_comment.rb

Constant Summary

Constants included from TemplateParser::Parsing

TemplateParser::Parsing::CLASS_MACROS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateParser::Parsing

#extract_dsl_comment, #extract_dsl_comment_from_method, #get_line_number, #insert_method_name, #look_for_class_macro_comment, #parse_erb, #ruby_class_meth_comment, #ruby_class_meth_source_location, #source_location, #strip_comment_hash, #when_only_start, #when_start_and_end

Constructor Details

#initialize(subject) ⇒ ClassLevelComment

Returns a new instance of ClassLevelComment.



5
6
7
# File 'lib/markdown_ruby_documentation/class_level_comment.rb', line 5

def initialize(subject)
  @subject = subject
end

Instance Attribute Details

#subjectObject (readonly)

Returns the value of attribute subject.



3
4
5
# File 'lib/markdown_ruby_documentation/class_level_comment.rb', line 3

def subject
  @subject
end

Instance Method Details

#call(interface) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/markdown_ruby_documentation/class_level_comment.rb', line 9

def call(interface)
  _method = interface.reject do |_, meth|
    meth[:method_object].is_a?(NullMethod) || meth[:method_object].visibility != :native || meth[:method_object].to_proc.source_location.first.include?(RUBY_VERSION)
  end.first
  if _method
    filename, lineno = _method[1][:method_object].to_proc.source_location
    comment = extract_dsl_comment(strip_comment_hash(comment(filename, lineno)+"\n"))
    interface[:class_level_comment] = { text: comment }
  end
  interface
end

#comment(filename, lineno) ⇒ Object



21
22
23
24
25
# File 'lib/markdown_ruby_documentation/class_level_comment.rb', line 21

def comment(filename, lineno)
  method_to_top_of_file = File.read(filename).split("\n")[0..(lineno-1)]
  subject_start_line = find_start_of_subject(method_to_top_of_file, filename)
  extract_last_comment(method_to_top_of_file[0..(subject_start_line-1)].reverse).reverse.join("\n")
end

#extract_last_comment(lines) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/markdown_ruby_documentation/class_level_comment.rb', line 38

def extract_last_comment(lines)
  buffer = []

  lines.each do |line|
    # Add any line that is a valid ruby comment,
    # but clear as soon as we hit a non comment line.
    if line =~ /^\s*#/
      buffer << line.lstrip
    else
      return buffer
    end
  end

  buffer
end

#find_start_of_subject(method_to_top_of_file, filename) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/markdown_ruby_documentation/class_level_comment.rb', line 27

def find_start_of_subject(method_to_top_of_file, filename)
  method_to_top_of_file.each_with_index do |line, index|
    if line =~ /#{subject.class.to_s.downcase} #{subject.name.split("::").last}/
      return index
    end
  end
  puts /#{subject.class} #{subject.name.split("::").last}/
  puts method_to_top_of_file
  raise "class #{subject.name} not found in #{filename}"
end