def create_route
blocks = []
Effective::CodeWriter.new(resource.routes_file) do |w|
resource.namespaces.each do |namespace|
index = nil
w.within(blocks.last) do
index = w.first { |line, depth| depth == 1 && line == "namespace :#{namespace} do"}
end
index ? (blocks << index) : break
end
content = resource.namespaces[blocks.length..-1].map { |namespace| "namespace :#{namespace} do"} +
[resources].flatten + (['end'] * (resource.namespaces.length - blocks.length))
w.within(blocks.last) do
if content.length == 1 && w.find { |line, depth| depth == 1 && line == content.first }
say_status :identical, content.first, :blue
else
w.insert_after_last(content, content_depth: 0) { |line, depth| depth == 1 && line.start_with?('resources') } ||
w.insert_before_last(content, content_depth: 0) { |line, depth| depth == 1 && line.start_with?('root') } ||
w.insert_before_last(content, content_depth: 0) { |line, depth| line == 'end' }
say_status :route, content.join("\n\t\t")
end
end
end
end