Class: Ld::Tables

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, models) ⇒ Tables

Returns a new instance of Tables.



5
6
7
8
9
# File 'lib/ld/project/tables.rb', line 5

def initialize root, models
  @root = root
  @models = models
  parse
end

Instance Attribute Details

#headingsObject

Returns the value of attribute headings.



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

def headings
  @headings
end

#rowsObject

Returns the value of attribute rows.



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

def rows
  @rows
end

#tablesObject

Returns the value of attribute tables.



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

def tables
  @tables
end

Instance Method Details

#parseObject



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
# File 'lib/ld/project/tables.rb', line 11

def parse
  tables = {}
  read_flag = false
  table = ""
  @root.find('db/schema.rb').lines.each do |l|
    if l.split(' ')[0] == 'end'
      read_flag = false
    end
    if l.split(' ')[0] == 'create_table'
      read_flag = true
      table = l.split('"')[1]
      tables[table] = []
    end
    if read_flag
      tables[table] << l
    end
  end
  rows = []
  tables.each do |table_name, lines|
    model_class = parse_class table_name
    lines.delete_at(0)
    lines.each do |line|
      hash = parse_line line
      if hash[:type] != 'index'
        if @models
          @model_name = @models.models.include?(table_name.singularize) ? table_name.singularize : nil
        end
        rows << [@model_name,table_name,hash[:field],hash[:type],hash[:comment],hash[:null],hash[:default],hash[:precision],hash[:limit]]
      end
    end
  end
  @headings = ['所属模型','表名','字段','字段类型','描述','空约束','默认值','精度位数','limit']
  @rows = rows.sort{|a,b| b[1] <=> a[1]}
  @tables = tables.keys
end

#parse_class(table_name) ⇒ Object



73
74
75
76
77
78
# File 'lib/ld/project/tables.rb', line 73

def parse_class table_name
  model_class = table_name.singularize.camelize.constantize
  return model_class
rescue
  return nil
end

#parse_line(line) ⇒ 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
71
# File 'lib/ld/project/tables.rb', line 47

def parse_line line
  arr = line.split(' ')
  hash = {}
  if arr[0].match(/^t./)
    hash[:type]    = arr[0].split('.')[1]
    i = nil
    if hash[:type] == 'index'
      hash[:name]    = arr[i + 1] if i = arr.index('name:')
      hash[:unique]    = arr[i + 1] if i = arr.index('unique:')
      hash[:using]    = arr[i + 1] if i = arr.index('using:')
    else
      hash[:field]    = line.split('"')[1]
      hash[:null]    = arr[i + 1] if i = arr.index('name:')
      hash[:limit] = arr[i + 1] if i = arr.index('limit:')
      hash[:comment] = arr[i + 1].split('"')[1] if i = arr.index('comment:')
      hash[:default] = arr[i + 1] if i = arr.index('default:')
      hash[:precision] = arr[i + 1] if i = arr.index('precision:')
      hash[:precision] = arr[i + 1] if i = arr.index('precision:')
    end
    return hash
  else

  end
  return false
end