Module: Ruby2JS::Filter::AngularRoute

Includes:
SEXP
Defined in:
lib/ruby2js/filter/angular-route.rb

Instance Method Summary collapse

Methods included from SEXP

#s

Instance Method Details

#on_case(node) ⇒ Object

input:

case $routeProvider
when '/path'
templateUrl = 'partials/path.html'
else
redirectTo '/path'
end

output:

AppName.config(["$routeProvider", function($routeProvider) {
$routeProvider.when("/path", {templateUrl: 'partials/path.html'}).
otherwise({redirectTo: "/path"}))


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
# File 'lib/ruby2js/filter/angular-route.rb', line 22

def on_case(node)
  rp = :$routeProvider
  return super unless @ngApp and node.children.first == s(:gvar, rp)
  @ngAppUses << :ngRoute
  code = s(:lvar, rp)

  hash = proc do |pairs|
    if pairs.length == 1 and pairs.first.type == :begin
      pairs = pairs.first.children
    end
    s(:hash, *pairs.map {|pair| 
      if pair.type == :send
        s(:pair, s(:sym, pair.children[1]), pair.children[2])
      else
        s(:pair, s(:sym, pair.children[0]), pair.children[1])
      end
    })
  end

  node.children[1..-2].each do |child|
    code = s(:send, code, :when, child.children.first,
      hash[child.children[1..-1]])
  end

  if node.children.last
    code = s(:send, code, :otherwise, 
      hash[node.children[-1..-1]])
  end

  s(:send, s(:lvar, @ngApp), :config, s(:array, s(:str, rp.to_s),
    s(:block, 
      s(:send, nil, :proc), s(:args, s(:arg, rp)), code)))
end