Sha256: c66f061e5156e22c6fc2bb2f62bd2152e7d8b3386b55a5115d22020dd970f0ee

Contents?: true

Size: 950 Bytes

Versions: 1

Compression:

Stored size: 950 Bytes

Contents

# encoding: UTF-8
require 'active_support/core_ext/string/inflections'

module AssetPipelineRoutes
  module JsFunctionHelper
    extend self

    def route_to_anonymous_function route, style = :js
      function_arguments = route.scan(/:(\w+)/).flatten.map { |param|
        param.camelcase(:lower)
      }
      url_parts = route.split(/:\w+/).map{ |fragment| "'#{fragment}'" }
      function = if style == :js
        <<-JS
          (function() {
            return function (#{function_arguments.join ', '}) {
              return #{url_parts.zip(function_arguments).flatten.reject{ |part| part.nil? }.join(' + ')}
            };
          }).call(this);
        JS
      elsif style == :coffee
        <<-COFFEESCRIPT
          (-> (#{function_arguments.join ', '}) -> #{url_parts.zip(function_arguments).flatten.reject{ |part| part.nil? }.join(' + ')})(this)
        COFFEESCRIPT
      end
      function.gsub(/\s+/,' ').strip
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asset_pipeline_routes-0.0.4 lib/asset_pipeline_routes/js_function_helper.rb