Class: Ld::Project

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_hash = {}, project_root_path = Rails.root.to_s) ⇒ Project

作用 解析一个项目的代码获得结构化的数据



6
7
8
9
10
11
12
13
# File 'lib/ld/project/project.rb', line 6

def initialize table_hash = {}, project_root_path = Rails.root.to_s
  @root = Ld::File.new project_root_path
  @table_hash = table_hash
  @schema = @root.find('db/schema.rb')
  raise "schema.rb文件不存在\n请运行命令(rake db:schema:dump)或手动添加此文件" if @schema.nil?
  raise "schema.rb文件是空的\n请运行命令(rake db:schema:dump)或手动添加此文件" if @schema.lines.size == 0
  parse_project
end

Instance Attribute Details

#controllersObject

Returns the value of attribute controllers.



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

def controllers
  @controllers
end

#modelsObject

Returns the value of attribute models.



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

def models
  @models
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

#routesObject

Returns the value of attribute routes.



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

def routes
  @routes
end

#table_hashObject

Returns the value of attribute table_hash.



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

def table_hash
  @table_hash
end

#tablesObject

Returns the value of attribute tables.



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

def tables
  @tables
end

#viewsObject

Returns the value of attribute views.



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

def views
  @views
end

Instance Method Details

#add_bugObject



131
132
133
# File 'lib/ld/project/project.rb', line 131

def add_bug

end

#delete_rows_indexObject



95
96
97
98
99
100
101
# File 'lib/ld/project/project.rb', line 95

def delete_rows_index
  @routes.rows.delete_at 0
  @tables.rows.delete_at 0
  @models.rows.delete_at 0
  @views.rows.delete_at 0
  @controllers.rows.delete_at 0
end

#parse_projectObject



15
16
17
18
19
20
21
22
# File 'lib/ld/project/project.rb', line 15

def parse_project
  @tables = Ld::Tables.new @root, nil
  @models = Ld::Models.new @root, @tables, @table_hash
  @routes = Ld::Routes.new @root, @models
  @tables = Ld::Tables.new @root, @models
  @views = Ld::Views.new @root, @models
  @controllers = Ld::Controllers.new @root, @models
end

作用 查看模型的相关信息(参数有:relations,fields,tables,routes,views,controllers)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
87
88
89
90
91
92
93
# File 'lib/ld/project/project.rb', line 25

def print model_name, type = :relations
  model_name = model_name.to_s
  if !@models.models.include? model_name
    puts "不存在 #{model_name}"
    return false
  end

  title_str = "#{model_name.camelize}(#{@table_hash[model_name]})"
  type = type.to_sym
  case type
    when :fields
      fs = '字段,字段类型,描述,空约束,默认值,精度位数,limit'.split(',')
      indexs = fs.map{|f| @tables.headings.index(f)}.compact
      rows = []
      @tables.rows.select{|a| a[0]==model_name}
          .each{|arr| rows << indexs.map{|i| arr[i]} }
      puts Terminal::Table.new(
               :title => "#{title_str}:字段解释",
               :headings => fs,
               :rows => rows
           )
    when :relations
      fs = 'has_many,belongs_to,has_one'.split(',')
      indexs = fs.map{|f| @models.headings.index(f)}.compact
      rows = []
      @models.rows.select{|a| a[0]==model_name}.
          each{|arr| rows << indexs.map{|i| arr[i]} }
      puts Terminal::Table.new(
               :title => "#{title_str}:关联关系",
               :headings => fs,
               :rows => rows
           )
    when :routes
      fs = '控制器,action,请求类型,URI,帮助方法'.split(',')
      indexs = fs.map{|f| @routes.headings.index(f)}.compact
      rows = []
      @routes.rows.select{|a| a[0]==model_name}.
          each{|arr| rows << indexs.map{|i| arr[i]} }
      puts Terminal::Table.new(
               :title => "#{title_str}:路由",
               :headings => fs,
               :rows => rows
           )
    when :views
      fs = '文件夹名,行数,文件名,path'.split(',')
      indexs = fs.map{|f| @views.headings.index(f)}.compact
      rows = []
      @views.rows.select{|a| a[0]==model_name}.
          each{|arr| rows << indexs.map{|i| arr[i]} }
      puts Terminal::Table.new(
               :title => "#{title_str}:视图",
               :headings => fs,
               :rows => rows
           )
    when :controllers
      fs = 'action个数,文件行数,所有action'.split(',')
      indexs = fs.map{|f| @controllers.headings.index(f)}.compact
      rows = []
      @controllers.rows.select{|a| a[0]==model_name}.
          each{|arr| rows << indexs.map{|i| arr[i]} }
      puts Terminal::Table.new(
               :title => "#{title_str}:控制器",
               :headings => fs,
               :rows => rows
           )
  end

  true
end

#to_xls(path = {:file_path => "#{@root.path}/project.xls"}) ⇒ Object

作用 将这个项目的代码分析结果保存到excel文件(默认在项目根目录下的project.xls)



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ld/project/project.rb', line 104

def to_xls path = {:file_path => "#{@root.path}/project.xls"}
  Ld::Excel.create path do |excel|
    excel.write_sheet 'routes' do |sheet|
      sheet.set_format({color: :red, font_size: 14, font: '微软雅黑'})
      sheet.set_headings @routes.headings
      sheet.set_rows @routes.rows
    end
    excel.write_sheet 'tables' do |sheet|
      sheet.set_headings @tables.headings
      sheet.set_rows @tables.rows
    end
    excel.write_sheet 'models' do |sheet|
      sheet.set_headings @models.headings
      sheet.set_rows @models.rows
    end
    excel.write_sheet 'views' do |sheet|
      sheet.set_headings @views.headings
      sheet.set_rows @views.rows
    end
    excel.write_sheet 'controllers' do |sheet|
      sheet.set_headings @controllers.headings
      sheet.set_rows @controllers.rows
    end
  end
  #delete_rows_index
end