Class: YardDocExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/salad/generate_gem_help.rb

Instance Method Summary collapse

Constructor Details

#initialize(lib_filepath, test_dir = nil) ⇒ YardDocExtractor

Returns a new instance of YardDocExtractor.



44
45
46
47
48
49
# File 'lib/salad/generate_gem_help.rb', line 44

def initialize(lib_filepath, test_dir = nil)
  @lib_filepath = lib_filepath
  @test_dir = test_dir
  @content = File.read(lib_filepath) if File.exist?(lib_filepath)
  @test_content = load_test_files(test_dir)
end

Instance Method Details

#extract_all_methodsObject



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
# File 'lib/salad/generate_gem_help.rb', line 51

def extract_all_methods
  return {} unless @content

  methods = {}
  lines = @content.split("\n")
  i = 0

  while i < lines.size
    line = lines[i]

    # Find method definition
    if line.match?(/^\s*def\s+self\.(\w+)/)
      method_name = line.match(/def\s+self\.(\w+)/)[1]

      # Look backwards for docstring
      docstring_lines = []
      j = i - 1
      while j >= 0 && lines[j].match?(/^\s*#/)
        docstring_lines.unshift(lines[j])
        j -= 1
      end

      docstring_text = docstring_lines.join("\n")
      data = parse_docstring(docstring_text)

      # Extract examples from test files
      data[:examples] = extract_examples_for_method(method_name)

      methods[method_name.to_sym] = data
    end

    i += 1
  end

  methods
end

#load_test_files(test_dir) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/salad/generate_gem_help.rb', line 88

def load_test_files(test_dir)
  return "" unless test_dir && File.directory?(test_dir)

  test_content = ""
  Dir.glob("#{test_dir}/**/*_test.rb").each do |file|
    test_content += File.read(file) + "\n"
  end
  test_content
end