-
Notifications
You must be signed in to change notification settings - Fork 0
/
view_parser.rb
92 lines (77 loc) · 1.97 KB
/
view_parser.rb
1
2
3
4
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
87
88
89
90
91
92
class ViewParser
attr_reader :mocks, :template_stubs, :routes
def self.parse( template_path, options={} )
ViewParser.new( template_path, options )
end
def initialize( template_path, options={} )
@template_path = template_path
@application = RailsApplication.new( options )
parse_template
end
def name
controller_name+"_"+view_name
end
def view_name
template_stem.split("/").last
end
def controller_name
template_stem.split("/").first
end
def template_stem
@template_path =~ /app\/views\/(.*?)\.html\.erb/
return $1
end
def is_partial?
@template_path =~ /\/_|\_/
end
def root_directory
root_directory = ''
root_depth = @template_path.split('/').size - 2
root_depth.times do
root_directory += '/..'
end
root_directory
end
#Not sure this object should know about the test directory.. refactor out?
def path_to_test_file
"test/views/test_#{template_stem}.rb"
end
private
def parse_template
@mocks, @template_stubs, @routes = [], [], []
IO.readlines( @template_path ).each do | line |
@mocks += instance_vars( line )
@template_stubs += helper_method_names( line )
@routes += named_routes( line )
end
@mocks.uniq!
@template_stubs.uniq!
@routes.uniq!
end
def instance_vars( template_line )
mocks = []
template_line.scan(/@(.+?)\b/).flatten.each do | variable_name |
begin
variable_name.classify.constantize
mocks << variable_name
rescue NameError
mocks << variable_name
end
end
mocks
end
def helper_method_names( template_line )
stubs = []
@application.helper_methods.each do | helper_method |
stubs << helper_method if template_line[ helper_method ]
end
stubs
end
def named_routes( template_line )
stubs = []
@application.routes.each do | route|
stubs << route if template_line[ route ]
end
stubs
end
end