Sha256: fdda3c67337bbb50fb46591f23f289ce7a5f02a68803983e3bea0318210c3535

Contents?: true

Size: 938 Bytes

Versions: 2

Compression:

Stored size: 938 Bytes

Contents

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

module AssetPipelineRoutes
  module JsFunctionHelper
    def self.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

2 entries across 2 versions & 1 rubygems

Version Path
asset_pipeline_routes-0.1.1 lib/asset_pipeline_routes/js_function_helper.rb
asset_pipeline_routes-0.1.0 lib/asset_pipeline_routes/js_function_helper.rb