Class: Volt::ComponentTemplates
Defined Under Namespace
Modules: Handlers
Instance Method Summary
collapse
Methods included from Handlers
extended, extensions, handler_for_extension, register_template_handler, registered_template_handler
Constructor Details
#initialize(component_path, component_name, client = true) ⇒ ComponentTemplates
client is if we are generating for the client or backend
49
50
51
52
53
|
# File 'lib/volt/server/component_templates.rb', line 49
def initialize(component_path, component_name, client = true)
@component_path = component_path
@component_name = component_name
@client = client
end
|
Instance Method Details
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/volt/server/component_templates.rb', line 55
def code
code = generate_routes_code + generate_view_code
if @client
code << generate_controller_code + generate_model_code +
generate_tasks_code + generate_initializers_code
end
code
end
|
#generate_controller_code ⇒ Object
126
127
128
129
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
|
# File 'lib/volt/server/component_templates.rb', line 126
def generate_controller_code
code = ''
controllers_path = "#{@component_path}/controllers/"
views_path = "#{@component_path}/views/"
implicit_controllers = Dir["#{views_path}*"].sort.map do |path|
path.split('/').tap {|v| v[-2] = 'controllers' }.join('/') + '_controller.rb'
end
explicit_controllers = Dir["#{controllers_path}*_controller.rb"].sort
controllers = (implicit_controllers + explicit_controllers).uniq
controllers.each do |path|
if File.exists?(path)
code << File.read(path) + "\n\n"
else
end
end
code
end
|
#generate_initializers_code ⇒ Object
202
203
204
205
206
207
|
# File 'lib/volt/server/component_templates.rb', line 202
def generate_initializers_code
code = "\nrequire_tree '#{@component_path}/config/initializers/'\n"
code << "require_tree '#{@component_path}/config/initializers/client'\n"
code
end
|
#generate_model_code ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/volt/server/component_templates.rb', line 157
def generate_model_code
code = ''
models_path = "#{@component_path}/models/"
Dir["#{models_path}*.rb"].sort.each do |model_path|
code << File.read(model_path) + "\n\n"
model_name = model_path.match(/([^\/]+)[.]rb$/)[1]
end
code
end
|
#generate_routes_code ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/volt/server/component_templates.rb', line 170
def generate_routes_code
code = ''
routes_path = "#{@component_path}/config/routes.rb"
if File.exist?(routes_path)
code << "#{page_reference}.add_routes do\n"
code << "\n" + File.read(routes_path) + "\n"
code << "end\n\n"
end
code
end
|
#generate_tasks_code ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/volt/server/component_templates.rb', line 183
def generate_tasks_code
Task.known_handlers.map do |handler|
klass_parts = handler.name.split('::')
parts = ["class #{klass_parts.pop} < Volt::Task; end"]
klass_parts.reverse_each do |kpart|
parts.unshift("module #{kpart}")
parts.push('end')
end
parts.join("\n")
end.join "\n" end
|
#generate_view_code ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/volt/server/component_templates.rb', line 74
def generate_view_code
code = ''
views_path = "#{@component_path}/views/"
exts = Handlers.extensions
Dir["#{views_path}*/*.{#{exts.join(',')}}"].sort.each do |view_path|
path_parts = view_path.scan(/([^\/]+)\/([^\/]+)\/[^\/]+\/([^\/]+)[.](html|email)$/)
component_name, controller_name, view, _ = path_parts[0]
format = File.extname(view_path).downcase.delete('.').to_sym
template_path = view_path[views_path.size..-1].gsub(/[.](#{exts.join('|')})$/, '')
template_path = "#{@component_name}/#{template_path}"
file_contents = File.read(view_path)
if handler = ComponentTemplates.handler_for_extension(format)
file_contents = handler.call(file_contents)
all_templates = ViewParser.new(file_contents, template_path)
binding_initializers = []
all_templates.templates.each_pair do |name, template|
binding_code = []
if template['bindings']
template['bindings'].each_pair do |key, value|
binding_code << "#{key.inspect} => [#{value.join(', ')}]"
end
end
binding_code = "{#{binding_code.join(', ')}}"
code << "#{page_reference}.add_template(#{name.inspect}, #{template['html'].inspect}, #{binding_code})\n"
end
end
end
code
end
|
#page_reference ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/volt/server/component_templates.rb', line 66
def page_reference
if @client
'$page'
else
'page'
end
end
|