Class: Ld::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/ld/document/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Document

作用 读一个rb文件生成api数据



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ld/document/document.rb', line 6

def initialize file
  @doc = {}
  @lines = file.lines
  @lines.each_with_index do |line, i|
    arr = line.split(' ')
    if arr.delete_at(0) == 'def'
      notes = get_notes(@lines, i)
      if notes.size > 0
        method = arr.delete_at(0)
        @doc[method] = {
            params:arr.join(' '),
            notes:notes
        }
      end
    end
  end
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



3
4
5
# File 'lib/ld/document/document.rb', line 3

def doc
  @doc
end

Class Method Details

.write_readme(readme_path = '/Users/liudong/ruby/my_gems/ld/README2.md') ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ld/document/document.rb', line 47

def self.write_readme readme_path = '/Users/liudong/ruby/my_gems/ld/README2.md'
  docs = Ld::File.open('/Users/liudong/ruby/my_gems/ld/lib/ld').search_regexp(/.rb$/).map{|f| Ld::Document.new f}
  arr = ["## API\n\n\n"]
  docs.each do |doc|
    if !doc.doc.empty?
      arr << "### #{doc.class_name}"
      # arr << "```ruby"
      doc.doc.each do |k, v|
        arr << "* `#{k} #{v[:params]}`"
        v[:notes].each do |note|
          arr << " * #{note[:title]}:#{note[:note]}"
        end
        arr << ""
      end
      # arr << "```"
    end
  end
  File.open readme_path,'w' do |file|
    arr.each do |line|
      file.puts line
    end
    file.close
  end
end

Instance Method Details

#class_nameObject



38
39
40
41
42
43
44
45
# File 'lib/ld/document/document.rb', line 38

def class_name
  @lines.each do |line|
    if line.split(' ')[0] =='class'
      return line.split(' ')[1]
    end
  end
  return nil
end

#get_notes(lines, i) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ld/document/document.rb', line 24

def get_notes lines, i
  notes = []
  (i-1).downto(0) do |j|
    arr = lines[j].split(' ')
    if arr[0] == '#='
      notes << {title:arr[1], note:arr[2..(arr.size)].join(' ')}
    else
      return notes
    end
  end
  notes
end