class JsRailsRoutes::Routes
Public Class Methods
new(text)
click to toggle source
# File lib/js-rails-routes.rb, line 6 def initialize(text) @text = text end
Public Instance Methods
create_javascript(with_link_to = true)
click to toggle source
# File lib/js-rails-routes.rb, line 10 def create_javascript(with_link_to = true) result = <<EOF // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // // This file is auto-generated by js-rails-routes #{VERSION} // // DO NOT EDIT BY HAND!! // EOF if with_link_to result += generate_link_to end lines = @text.split("\n") lines.each do |line| # clear leading whitespace line.sub!(/^\s+/, '') (name, method, format, action) = line.split(/\s+/) # action will only contain something if there were at least four segments # to split. This is a little ass-backwards. Really when action contains no # non-whitespace, what it really means is that name contained no non-whitespace. if action =~ /\S/ # Get rid of the (.:format) business at the end of each format format.gsub!(/\(.*\)/, '') # Get rid of the leading slash format.sub!(/^\//, '') # Split it into its parts parts = format.split("/") args = [] parts.each do |part| if part =~ /^:/ args.push part.sub(/^:/, '') end end if args.empty? # This is the easy case. There are no parameters we need to deal with, just # a simple path result += <<EOF var #{name}_path = function() { return "/#{format}"; } EOF else # The trickier case. We want to accept values as args. The first arg could # be a hash object, containing all the information, or we could have several # primitive arguments that appear in the order that they do in the format result += <<EOF var #{name}_path = function(#{ args.join(", ") }) { if (#{args[0]} instanceof Object) { EOF result += ' return ' components = [] parts.each do |part| components.push '"/"' if part =~ /^:/ components.push args[0] + '["' + part.sub(/^:/, '') + '"]' else components.push '"' + part + '"' end end result += components.join(" + ") + ";\n" result += <<EOF } else { EOF result += ' return ' components = [] parts.each do |part| components.push '"/"' if part =~ /^:/ components.push part.sub(/^:/, '') else components.push '"' + part + '"' end end result += components.join(' + ') + ";\n" result += <<EOF } } EOF end end end result end
generate_link_to()
click to toggle source
# File lib/js-rails-routes.rb, line 111 def generate_link_to # This is a fairly silly method. # It just regurgitates javascript I've written elsewhere by hand. File.read File.expand_path("../js-rails-routes/js/link_to.js", __FILE__) end