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

作用 空的实例方法,Ld::Document的大部分功能都由实例提供(个人习惯调用对象方法而不是类方法)



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
  @class_name = file.split('.rb')[0].camelize
  @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
        @doc[arr.delete_at(0)] = {
            params:arr.join(' '),
            notes:notes
        }
      end
    end
  end
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



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

def class_name
  @class_name
end

#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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ld/document/document.rb', line 42

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"]
  docs.map do |doc|
    arr << "## #{doc.class_name}"
    arr << "```ruby"
    doc.doc.each do |k, v|
      v[:notes].each do |note|
        arr << "#{note[:title]}:#{note[:note]}"
      end
      arr << "#{k} #{v[:params]}"
    end
    arr << "```"
  end
  File.open readme_path,'w' do |file|
    arr.each do |line|
      arr.puts line
    end
    file.colse
  end
end

Instance Method Details

#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