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
|
# File 'lib/model-visualizer/model-parser.rb', line 11
def parse(root)
unless Dir.exists? File.join(root, 'app/models/')
abort 'app/models/ directory does not exist! Run from or pass the root directory of your Rails project.'
end
files = Dir[File.join(root, 'app/models/**/*.rb')]
curr_model = nil
for file in files
IO.foreach(file) do |line|
if line.include? 'class_name'
next
end
line.strip!
if !(/^class ([[:alpha:]]+) < ActiveRecord::Base/.match(line)).nil?
name = /^class ([[:alpha:]]+) < ActiveRecord::Base/.match(line)[1]
curr_model = Model.new(name)
@models[name] = curr_model
elsif !(/^has_many :(\w+)/.match(line)).nil?
model_neighbor = /^has_many :(\w+)/.match(line)[1]
curr_model.add_has_many fix_case(model_neighbor)
elsif !(/^has_one :(\w+)/.match(line)).nil?
model_neighbor = /^has_one :(\w+)/.match(line)[1]
curr_model.add_has_one fix_case(model_neighbor)
elsif !(/^has_and_belongs_to_many :(\w+)/.match(line)).nil?
model_neighbor = /^has_and_belongs_to_many :(\w+)/.match(line)[1]
curr_model.add_has_and_belongs_to_many fix_case(model_neighbor)
elsif !(/^belongs_to :(\w+)/.match(line)).nil?
model_neighbor = /^belongs_to :(\w+)/.match(line)[1]
curr_model.add_belongs_to fix_case(model_neighbor)
end
end
end
end
|