Sha256: aabfc72abb6172d1cebe828c0a837397060c243de65721cb4f0effd32b71048f

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

require 'parser/current'
require 'ruby2js/filter/angularrb'

module Ruby2JS
  module Filter
    module AngularRoute
      include SEXP

      # 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"}))

      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
    end

    DEFAULTS.push AngularRoute
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby2js-1.2.0 lib/ruby2js/filter/angular-route.rb
ruby2js-1.1.5 lib/ruby2js/filter/angular-route.rb
ruby2js-1.1.4 lib/ruby2js/filter/angular-route.rb
ruby2js-1.1.3 lib/ruby2js/filter/angular-route.rb
ruby2js-1.1.2 lib/ruby2js/filter/angular-route.rb