Class: JsRailsRoutes::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/js-rails-routes.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Routes

Returns a new instance of Routes.



6
7
8
# File 'lib/js-rails-routes.rb', line 6

def initialize(text)
  @text = text      
end

Instance Method Details

#create_javascript(with_link_to = true) ⇒ Object



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/js-rails-routes.rb', line 10

def create_javascript(with_link_to = true)
  result = "// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n//\n// This file is auto-generated by js-rails-routes \#{VERSION}\n//\n// DO NOT EDIT BY HAND!!\n//\n   \n"

  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 += "\nvar \#{name}_path = function() {\n  return \"/\#{format}\";\n}\n"
      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 += "\nvar \#{name}_path = function(\#{ args.join(\", \") }) {\n  if (\#{args[0]} instanceof Object) {\n"
        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 += "  } else {\n"

        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 += "  }\n}\n"
      end
    end
  end
  
  result
end


111
112
113
114
115
116
# 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