Class: Ld::Models
- Inherits:
-
Object
- Object
- Ld::Models
- Defined in:
- lib/ld/project/models.rb
Instance Attribute Summary collapse
-
#headings ⇒ Object
Returns the value of attribute headings.
-
#models ⇒ Object
Returns the value of attribute models.
-
#rows ⇒ Object
Returns the value of attribute rows.
Instance Method Summary collapse
- #get_relations(lines) ⇒ Object
-
#initialize(root, tables) ⇒ Models
constructor
A new instance of Models.
- #parse ⇒ Object
Constructor Details
#initialize(root, tables) ⇒ Models
Returns a new instance of Models.
4 5 6 7 8 |
# File 'lib/ld/project/models.rb', line 4 def initialize root, tables @root = root @tables = tables parse end |
Instance Attribute Details
#headings ⇒ Object
Returns the value of attribute headings.
2 3 4 |
# File 'lib/ld/project/models.rb', line 2 def headings @headings end |
#models ⇒ Object
Returns the value of attribute models.
2 3 4 |
# File 'lib/ld/project/models.rb', line 2 def models @models end |
#rows ⇒ Object
Returns the value of attribute rows.
2 3 4 |
# File 'lib/ld/project/models.rb', line 2 def rows @rows end |
Instance Method Details
#get_relations(lines) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ld/project/models.rb', line 38 def get_relations lines relations = {has_many:[],belongs_to:[],has_one:[]} lines.each do |line| arr = line.split(' ') if arr[0] == 'has_many' relations[:has_many] << arr[1].split(':')[1] elsif arr[0] == 'belongs_to' relations[:belongs_to] << arr[1].split(':')[1] elsif arr[0] == 'has_one' relations[:has_one] << arr[1].split(':')[1] end end relations end |
#parse ⇒ Object
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 |
# File 'lib/ld/project/models.rb', line 10 def parse rows = [] model_files = @root.app.models.search_files(/.rb$/).map{|m| m if @tables.tables.include?(m.name.split('.')[0].pluralize) }.compact @rows = model_files.map{ |file| name = file.name.split('.')[0] lines = file.lines methods_full = lines.map{|l| l.split('def ')[1] if l.match(/def /)}.compact methods = methods_full.map{|method_full| method_full.split(' ')[0]} instance = name.camelize.constantize.new data_count = 0#name.camelize.constantize.count fields = instance.attributes.keys relations = get_relations file.lines [ name,name.pluralize,name.camelize,data_count,lines.size,file.path,methods.size, methods.join(','),fields.join(','),fields.size, relations[:has_many].size,relations[:belongs_to].size,relations[:has_one].size, relations[:has_many].join(','),relations[:belongs_to].join(','),relations[:has_one].join(',') ] }.compact.sort{|a,b| b[2] <=> a[2]} # 按 模型文件行数 排序 @models = @rows.map{|arr| arr[0]}.uniq @headings = ['模型名','表名','类','数据数量','行数','path', '方法数', '所有方法', '所有字段','字段数量', 'has_many个数','belongs_to个数','has_one个数', 'has_many','belongs_to','has_one'] end |