Sha256: 4e077824a9532dd73b717957c46fc947ddc16c66b6a76dd33afdd6b3235a53e7

Contents?: true

Size: 1009 Bytes

Versions: 24

Compression:

Stored size: 1009 Bytes

Contents

module SinatraMore
  module RoutingHelpers
    # Used to retrieve the full url for a given named route alias from the named_paths data
    # Accepts parameters which will be substituted into the url if necessary
    # url_for(:accounts) => '/accounts'
    # url_for(:account, :id => 5) => '/account/5'
    # url_for(:admin, show, :id => 5, :name => "demo") => '/admin/path/5/demo'
    def url_for(*route_name)
      values = route_name.extract_options!
      mapped_url = self.class.named_paths[route_name] || self.class.named_paths[route_name.dup.unshift(self.class.app_name)]
      raise SinatraMore::RouteNotFound.new("Route alias #{route_name.inspect} is not mapped to a url") unless mapped_url
      result_url = String.new(File.join(self.class.uri_root, mapped_url))
      result_url.scan(%r{/?(:\S+?)(?:/|$)}).each do |placeholder|
        value_key = placeholder[0][1..-1].to_sym
        result_url.gsub!(Regexp.new(placeholder[0]), values[value_key].to_s)
      end
      result_url
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
sinatra_more-0.3.43 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.42 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.41 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.40 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.39 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.38 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.37 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.36 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.35 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.34 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.33 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.32 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.31 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.30 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.29 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.28 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.27 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.26 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.25 lib/sinatra_more/routing_plugin/routing_helpers.rb
sinatra_more-0.3.24 lib/sinatra_more/routing_plugin/routing_helpers.rb