Created
July 10, 2009 08:24
-
-
Save kematzy/144323 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # An example of how to use mounted apps and stil have acces to full paths within the apps. | |
| # | |
| # Created by Kematzy [ 2009-07-10 ] | |
| require 'rubygems' | |
| require 'sinatra/base' | |
| # set the root of the whole app | |
| APP_ROOT = File.dirname(File.expand_path(__FILE__)) unless defined?(APP_ROOT) | |
| require 'sinatra/base' | |
| # simple Sinatra::Extension | |
| module Sinatra | |
| module MountedAtExample | |
| module Helpers | |
| ## | |
| # Simple method that generates the path to the app. | |
| # | |
| # ==== Examples | |
| # | |
| # path_app => '/' | |
| # path_app('path') => '/path' | |
| # path_app(:path) => '/path' | |
| # | |
| # # with app_mounted_at = '/blog' | |
| # path_app(:path) => '/blog/path' | |
| # | |
| def path_app(path='/') | |
| path = path.nil? ? '/' : path | |
| path = (path.to_s[0,1] == '/') ? path : "/#{path}" | |
| "#{self.class.app_mounted_at}#{path}" | |
| end | |
| end #/ Helpers | |
| def self.registered(app) | |
| app.helpers MountedAtExample::Helpers | |
| app.set :app_mounted_at, '/' | |
| end | |
| end #/ MountedAtExample | |
| end #/ Sinatra | |
| ## This is a free standing App | |
| class MyApp < Sinatra::Base | |
| register(Sinatra::MountedAtExample) | |
| set :public, "#{APP_ROOT}/public" | |
| set :views, "#{APP_ROOT}/views" | |
| get '/' do | |
| @html ||= "Hello from MyApp" | |
| %Q{ | |
| <p>#{@html}, I'm mounted at [#{self.class.app_mounted_at}]</p> | |
| <p>Checkout My<a href=/blog/>Blog</a> as well</p> | |
| <p>or My<a href=/gallery/>Gallery</a>instead</p> | |
| } | |
| end | |
| get '/another-page' do | |
| %Q{Great!! It works <br>Back to <a href="#{path_app}" title="link to home">MyApp Home</a>} | |
| end | |
| end #/class MyApp | |
| module MyComplexApp | |
| class App < Sinatra::Base | |
| register(Sinatra::MountedAtExample) # sets the app_mounted_at for all instances of App | |
| set :views, "views" | |
| get '/' do | |
| %Q{ | |
| <p>#{self.class.name} is mounted at [#{self.class.app_mounted_at}] </p> | |
| } | |
| end | |
| # just dumps the available methods of MyApp, so that you can easily see the routes defined | |
| get '/methods' do | |
| "#{self.methods.sort.join(",\n<br>")}" | |
| end | |
| end | |
| class Blog < App | |
| set :views, "views/blog" | |
| get '/' do | |
| %Q{<p>hello from the Blog (mounted at [#{self.class.app_mounted_at}])</p> | |
| <p>read some <a href="#{path_app(:articles)}" title="link to articles">articles</a></p> | |
| <p>or go back to <a href="/" title="link to MyApp">MyApp</a></p> | |
| } | |
| end | |
| get '/articles' do | |
| %Q{Great!! It works <br>Back to <a href="#{path_app}" title="link to home">Blog Home</a>} | |
| end | |
| end #/class Blog | |
| # class Gallery < Sinatra::Base | |
| class Gallery < App | |
| get '/' do | |
| %Q{ | |
| <p>hello from the Gallery (mounted at [#{self.class.app_mounted_at}])</p> | |
| <p>view some <a href="#{path_app(:pictures)}" title="link to pictures">pictures</a></p> | |
| <p>or go back to <a href="/" title="link to MyApp">MyApp</a></p> | |
| } | |
| end | |
| get '/pictures' do | |
| %Q{Great!! It works as well <br>Back to <a href="#{path_app}" title="link to home">Gallery Home</a> } | |
| end | |
| end #/ Gallery | |
| end #/module MyComplexApp | |
| map "/gallery" do | |
| MyComplexApp::Gallery.app_mounted_at = '/gallery' | |
| run MyComplexApp::Gallery | |
| end | |
| map "/blog" do | |
| MyComplexApp::Blog.app_mounted_at = '/blog' | |
| run MyComplexApp::Blog | |
| end | |
| map "/" do | |
| MyApp.app_mounted_at = '/' # no need it's working anyway | |
| run MyApp | |
| end | |
| ## Just an example of how you can create a base app and then just extend it with | |
| class ExtendedApp < MyApp | |
| get '/' do | |
| # crude way of showing that you can override some value and then just load the page | |
| @html = "Greetings from ExtendedApp" | |
| super | |
| end | |
| end | |
| map "/extended" do | |
| ExtendedApp.app_mounted_at = '/extended' | |
| run ExtendedApp | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment