Class: Ld::Project

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProject

Returns a new instance of Project.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/ld/project.rb', line 5

def initialize
  @@root = Ld::File.new Rails.root.to_s
  system "rake routes > #{@@root.config.path}/routes.txt"
  Ld::Excel.create "#{@@root.config.path}/project.xls" do |excel|
    excel.write_sheet 'routes' do |sheet|
      sheet.set_format({color: :black, font_size: 14, font: '微软雅黑'})
      sheet.set_headings ['控制器', 'action', '请求类型','URI','帮助方法']
      sheet.set_point 'a1'
      sheet.set_rows @@root.config.find('routes.txt').lines
                         .map{|line|
                       arr = line.split(' ')
                       arr.unshift(nil) if arr.size == 3
                       arr
                     }
                         .delete_if{|arr| arr.size == 5 }
                         .map{|row|
                       controller, action = row[3].split('#')
                       type        = row[1]
                       help_method = row[0]
                       uri         = row[2]
                       [controller, action, type, uri, help_method]
                     }
    end

    excel.write_sheet 'models' do |sheet|
      sheet.set_format({color: :black, font_size: 14, font: '微软雅黑'})
      sheet.set_point 'a1'

      @models = @@root.app.models.search_files(/.rb$/)
      @controllers = @@root.app.controllers.search_files(/_controller.rb$/)
      @views = @@root.app.views.search_dirs

      sheet.set_rows @models.map { |model_file|
        model_name    = model_file.name.split('.')[0]
        model_lines   = model_file.lines
        actions_full_name = model_lines.map{|l| l.split('def ')[1] if l.match(/def /)}.compact
        actions = actions_full_name.map{|action| action.split(' ')[0]}
        model_instance = eval("#{model_name.camelize}.new")
        fields = model_instance.attributes.keys


        controller = find_controller model_name.pluralize
        if controller
          controller_lines = controller.lines
          controller_methods = controller_lines.map{|l| l.split('def ')[1] if l.match(/def /)}.compact
        end

        view = find_view model_name.pluralize
        if view
          views = view.search_files(/.html/)
        end

        [
            model_name,             # 模型
            model_name.camelize,     # 类
            model_lines.size,       # 模型lines
            model_file.path,        # 模型文件
            (controller.nil? ? '' : controller.path),     # 控制器文件
            (controller.nil? ? 0 : controller_lines.size),# 控制器lines
            actions.size,           # 模型方法size
            actions.join(','),      # 模型方法
            fields.size,          # 字段size
            fields.join(','),   # 字段
            (views.nil? ? 0 : views.size), # 视图size
            (views.nil? ? '' : views.map{|v| "#{v.name.split('.')[0]}-#{v.path}"}.join(',')), # 视图
            (controller_methods.nil? ? 0 : controller_methods.size),  # action-size
            (controller_methods.nil? ? '' : controller_methods.join(',')) # actions
        ]
      }.sort{|a,b| b[2] <=> a[2]}  # 按 模型文件行数 排序

      sheet.set_row []
      sheet.set_row []
      sheet.set_headings ['模型','类',
                          '模型lines','模型文件',
                          '控制器文件','控制器lines',
                          '模型方法size','模型方法',
                          '字段size','字段',
                          '视图size', '视图',
                          'action-size','actions']
    end
  end
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



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

def app
  @app
end

#controllersObject

Returns the value of attribute controllers.



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

def controllers
  @controllers
end

#modelsObject

Returns the value of attribute models.



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

def models
  @models
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#routesObject

Returns the value of attribute routes.



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

def routes
  @routes
end

#viewsObject

Returns the value of attribute views.



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

def views
  @views
end

#views_dirObject

Returns the value of attribute views_dir.



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

def views_dir
  @views_dir
end

Class Method Details

.get_routesObject



120
121
122
123
124
125
126
127
128
# File 'lib/ld/project.rb', line 120

def self.get_routes
  @@p ||= Ld::Project.new(Rails.root.to_s)
  file = Ld::File.new @@p.config.path + '/routes.txt'
  system "rake routes > #{file.path}"
  file.lines.map{|line| arr = line.split(' '); arr.size == 3 ? arr.unshift(nil) : arr}
  t.headings = ['controller', 'action', 'type']
  arrs.map{|arr| controller,action = arr[3].split('#'); [controller, action, arr[1]]}
      .each{|arr| t.add_row arr}
end

.p(model = :all) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ld/project.rb', line 130

def self.p model = :all
  @@p ||= Ld::Project.new(Rails.root.to_s)

  t = Terminal::Table.new
  case model.to_s
    when 'all'
      t.title = "project:#{@@root.name}"
      t.headings = ['models', 'views', 'controllers', 'routes']
      t.add_row [@@p.models.size, @@p.views.size, @@p.controllers.size, @@p.routes.lines.size]
    when 'models'
      t.title = 'models'
      t.headings = ['name', 'action-size', 'line-size', 'routes']
      @@p.models.map{|f| [f.name.split('.')[0], f.lines.map{|l| l if l.match(/def /)}.compact.size, f.lines.size, nil] }
          .sort{|a,b| b[1]-a[1]}.each{|i| t.add_row i}
    when 'controllers'
      t.title = 'controllers'
      t.headings = ['name', 'action-size', 'line-size']
      @@p.controllers.map{|f| [f.name.split('.')[0], f.lines.map{|l| l if l.match(/def /)}.compact.size, f.lines.size] }
          .sort{|a,b| b[1]-a[1]}.each{|i| t.add_row i}
    when 'views'
      t.title = 'views'
      t.headings = ['name', 'file-size', 'html']
      @@p.app.views.children('shared')
          .map{|f| htmls = f.search(/.html/);[f.name, htmls.size, htmls.map{|f2| f2.name.split('.')[0]}.join(' ')]}
          .sort{|a,b| b[1]-a[1]}
          .each{|arr| t.add_row arr}
    when 'routes'
      file = Ld::File.new @@root.path + '/routes.txt'
      if !file.exist?
        system "rake routes > #{@@root.path + '/routes.txt'}"
      end
      arrs = file.lines.map{|l| lines = l.split(' '); lines.size == 3 ? lines.unshift(nil) : lines}
      arrs.delete_at 0
      t.title = 'routes'
      t.headings = ['controller', 'action', 'type']
      arrs.map{|arr| controller,action = arr[3].split('#'); [controller, action, arr[1]]}
          .each{|arr| t.add_row arr}
    else
      puts '(models/controllers/views)'
      return
  end
  puts t
end

.save_infoObject



115
116
117
118
# File 'lib/ld/project.rb', line 115

def self.save_info
  @@p ||= Ld::Project.new(Rails.root.to_s)

end

Instance Method Details

#camelize(name) ⇒ Object



104
105
106
# File 'lib/ld/project.rb', line 104

def camelize name
  name.camelize
end

#find_controller(model_name) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/ld/project.rb', line 88

def find_controller model_name
  @controllers.each do |c|
    if c.name.split('_controller.rb')[0] == model_name
      return c
    end
  end
  nil
end

#find_view(model_name) ⇒ Object



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

def find_view model_name
  @views.each do |v|
    if v.name == model_name
      return v
    end
  end
  nil
end

#pathObject



110
111
112
# File 'lib/ld/project.rb', line 110

def path
  @@root.path
end