Sha256: 82f67060030ecc6bf27ac25189bb29447e2d2c5789de818f77a6a3bac3eb3326

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

# encoding: UTF-8
module AssetPipelineRoutes
  class Path
    DEFAULT_REPLACEMENT = '{{\1}}'

    attr_accessor :route

    def self.proc_for_route route
      proc { |id_replacement = DEFAULT_REPLACEMENT, *args|
        proc { |route, replacement, *mapping|
          Path.new(route.path.ast.to_s).build replacement, *mapping
        }.curry[route].call(id_replacement, *args)
      }
    end

    def initialize route_str
      @route = route_str
    end

    def apply_format options = {}
      @route = @route.sub(/\(\.:\w+\)/,format(options))
    end

    def build *args
      apply_format args.extract_options!
      replacements(*args).inject(@route) { |route, param|
        route.sub(/:(\w+)/, param.to_s)
      }
    end

    def format options
      str = options.fetch(:format) { '' }
      str = ".#{str}" unless str.empty? && str[0] != '.'
      str
    end

    def default_replacements
      Array.new(number_of_replacements, DEFAULT_REPLACEMENT)
    end

    def replacements *args
      defaults = default_replacements
      defaults[0...args.length] = args
      defaults
    end

    def number_of_replacements
      @route.scan(/:(\w+)/).inject(0) { |sum, n| sum + 1 }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
asset_pipeline_routes-0.1.1 lib/asset_pipeline_routes/path.rb
asset_pipeline_routes-0.1.0 lib/asset_pipeline_routes/path.rb
asset_pipeline_routes-0.0.4 lib/asset_pipeline_routes/path.rb