Skip to content

Instantly share code, notes, and snippets.

@gabe
Created June 28, 2009 19:56

Revisions

  1. gabe revised this gist Jul 16, 2009. 1 changed file with 14 additions and 12 deletions.
    26 changes: 14 additions & 12 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -64,19 +64,21 @@ def inherited(subclass)
    # Initialize the URI Template collection for the application
    # being extended, and add a Sinatra helper for expanding
    # templates, by name.
    def self.extended(base)
    base.reset_uri_templates!
    base.helpers do
    def uri(name, *args)
    template = self.class.uri_templates[name]
    if template.nil?
    return nil
    end
    args[0] ||= {}
    uri = template.expand(*args)
    uri.path = request.script_name + uri.path
    uri
    def self.extended(extendee)
    extendee.reset_uri_templates!
    extendee.helpers(Helpers)
    end

    module Helpers
    def uri(name, *args)
    template = self.class.uri_templates[name]
    if template.nil?
    return nil
    end
    args[0] ||= {}
    uri = template.expand(*args)
    uri.path = request.script_name + uri.path
    uri
    end
    end
    end
  2. gabe revised this gist Jun 29, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ module Sinatra
    #
    # == Example
    #
    # class MyApplication < Sinatra::Application
    # class MyApplication < Sinatra::Base
    # extend Juxt::Sinatra::URITemplates
    #
    # uri :index, '/'
  3. gabe revised this gist Jun 29, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ module Sinatra
    # == Example
    #
    # class MyApplication < Sinatra::Application
    # extend Juxt::Sinatra::UriTemplates
    # extend Juxt::Sinatra::URITemplates
    #
    # uri :index, '/'
    # uri :entry, '/entries/{id}'
    @@ -28,7 +28,7 @@ module Sinatra
    # "The URI of this entry is #{uri(:entry, 'id' => params[:id])}"
    # end
    # end
    module UriTemplates
    module URITemplates
    def reset_uri_templates!
    @uri_templates = {}
    end
  4. gabe created this gist Jun 28, 2009.
    84 changes: 84 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    require 'addressable/template'

    module Juxt
    module Sinatra
    # Allows for declared, named URI Templates in Sinatra applications,
    # for both routing and URI generation.
    #
    # The URI generation method (included as a Sinatra helper) modifies
    # the path of generated URIs with request.script_name, meaning the
    # URIs generated will still be valid if multiple Sinatra applications
    # are hosted in the same application, using Rack::Builder.
    #
    # Requires Addressable (http://github.com/sporkmonger/addressable/tree/master).
    #
    # == Example
    #
    # class MyApplication < Sinatra::Application
    # extend Juxt::Sinatra::UriTemplates
    #
    # uri :index, '/'
    # uri :entry, '/entries/{id}'
    #
    # get :index do
    # "Hello"
    # end
    #
    # get :entry do
    # "The URI of this entry is #{uri(:entry, 'id' => params[:id])}"
    # end
    # end
    module UriTemplates
    def reset_uri_templates!
    @uri_templates = {}
    end

    # Declare a named URI Template for use in this application, and
    # in subclasses of this application.
    def uri(name, template)
    @uri_templates[name] = Addressable::Template.new(template)
    end

    # Gets the merged URI Template collection for this application,
    # and all superclasses.
    def uri_templates
    if superclass.respond_to?(:uri_templates)
    superclass.uri_templates.merge(@uri_templates)
    else
    @uri_templates
    end
    end

    private

    # Override Sinatra route compilation (dirty).
    def compile(path)
    super(uri_templates[path] || path)
    end

    def inherited(subclass)
    subclass.reset_uri_templates!
    super
    end

    # Initialize the URI Template collection for the application
    # being extended, and add a Sinatra helper for expanding
    # templates, by name.
    def self.extended(base)
    base.reset_uri_templates!
    base.helpers do
    def uri(name, *args)
    template = self.class.uri_templates[name]
    if template.nil?
    return nil
    end
    args[0] ||= {}
    uri = template.expand(*args)
    uri.path = request.script_name + uri.path
    uri
    end
    end
    end
    end
    end
    end