Class: Padrino::Application

### Home Classes Methods

### In Files

- lib/padrino-core/application.rb

### Parent

Sinatra::Base

### Methods

- ::default_configuration!
- ::default_errors!
- ::default_filters!
- ::default_routes!
- ::dependencies
- ::inherited
- ::load_paths
- ::prerequisites
- ::reload!
- ::require_dependencies
- ::reset_routes!
- ::routes
- ::run!
- ::setup_application!
- #logger

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Application
- Padrino::ApplicationLoadError
- Padrino::ApplicationSetupError
- Padrino::Cli
- Padrino::Cli::Adapter
- Padrino::Cli::Base
- Padrino::Erubis
- Padrino::Erubis::SafeBufferEnhancer
- Padrino::Erubis::SafeBufferTemplate
- Padrino::Erubis::Template
- Padrino::Filter
- Padrino::Flash
- Padrino::Flash::Helpers
- Padrino::Flash::Storage
- Padrino::Logger
- Padrino::Logger::Colorize
- Padrino::Logger::Extensions
- Padrino::Logger::Rack
- Padrino::Module
- Padrino::Mounter
- Padrino::Mounter::MounterException
- Padrino::Reloader
- Padrino::Reloader::Rack
- Padrino::Rendering
- Padrino::Rendering::ClassMethods
- Padrino::Rendering::InstanceMethods
- Padrino::Rendering::SafeTemplate
- Padrino::Rendering::TemplateNotFound
- Padrino::Router
- Padrino::Routing
- Padrino::Routing::ClassMethods
- Padrino::Routing::InstanceMethods
- Padrino::Routing::Parent
- Padrino::Routing::UnrecognizedException
- Padrino::Server
- Padrino::ShowExceptions
- Padrino::Tasks
- Padrino::Utils
- HttpRouter
- HttpRouter::Node
- HttpRouter::Node::Path
- HttpRouter::Request
- HttpRouter::Route
- Haml
- Haml::Helpers
- Haml::Util
- Sinatra
- Sinatra::Request
- Slim
- Slim::Template
- Tilt
- Tilt::HamlTemplate
- FileSet
- Kernel
- Module
- Object
- ObjectSpace
- PadrinoTasks
- String
- SupportLite

No matching classes.

# Padrino::Application

Subclasses of this become independent Padrino applications (stemming from Sinatra::Application) These subclassed applications can be easily mounted into other Padrino applications as well.

### Public Class Methods

dependencies() click to toggle source

Returns default list of path globs to load as dependencies Appends custom dependency patterns to the be loaded for your Application

@return [Array]

    list of path globs to load as dependencies

@example

    MyApp.dependencies << "#{Padrino.root}/uploaders/**/*.rb"
    MyApp.dependencies << Padrino.root('other_app', 'controllers.rb')

    # File lib/padrino-core/application.rb, line 143
    def dependencies
      [
        'urls.rb', 'config/urls.rb', 'mailers/*.rb', 'mailers.rb',
        'controllers/**/*.rb', 'controllers.rb', 'helpers/**/*.rb', 'helpers.rb'
      ].map { |file| Dir[File.join(settings.root, file)] }.flatten
    end

inherited(base) click to toggle source

    # File lib/padrino-core/application.rb, line 29
    def inherited(base) # @private
      begun_at = Time.now
      CALLERS_TO_IGNORE.concat(PADRINO_IGNORE_CALLERS)
      base.default_configuration!
      base.prerequisites.concat([
        File.join(base.root, '/models.rb'),
        File.join(base.root, '/models/**/*.rb'),
        File.join(base.root, '/lib.rb'),
        File.join(base.root, '/lib/**/*.rb')
      ]).uniq!
      Padrino.require_dependencies(base.prerequisites)
      logger.devel :setup, begun_at, base
      super(base) # Loading the subclass inherited method
    end

load_paths() click to toggle source

@return [Array]

    directory that need to be added to +$LOAD_PATHS+ from this application

    # File lib/padrino-core/application.rb, line 128
    def load_paths
      @_load_paths ||= ]models lib mailers controllers helpers].map { |path| File.join(settings.root, path) }
    end

prerequisites() click to toggle source

An array of file to load before your app.rb, basically are files wich our app depends on.

By default we look for files:

    # List of default files that we are looking for:
    yourapp/models.rb
    yourapp/models/**/*.rb
    yourapp/lib.rb
    yourapp/lib/**/*.rb

@example Adding a custom perequisite

    MyApp.prerequisites << Padrino.root('my_app', 'custom_model.rb')

    # File lib/padrino-core/application.rb, line 164
    def prerequisites
      @_prerequisites ||= []
    end

reload!() click to toggle source

Reloads the application files from all defined load paths

This method is used from our Padrino Reloader during development mode in order to reload the source files.

@return [TrueClass]

@example

    MyApp.reload!

    # File lib/padrino-core/application.rb, line 55
    def reload!
      logger.devel "Reloading #{settings}"
      reset! # Reset sinatra app
      reset_router! # Reset all routes
      Padrino.require_dependencies(settings.app_file, :force => true) # Reload the app file
      require_dependencies # Reload dependencies
      default_filters!     # Reload filters
      default_routes!      # Reload default routes
      default_errors!      # Reload our errors
      I18n.reload! if defined?(I18n) # Reload also our translations
      true
    end

reset_routes!() click to toggle source

Resets application routes to only routes not defined by the user

@return [TrueClass]

@example

    MyApp.reset_routes!

    # File lib/padrino-core/application.rb, line 76
    def reset_routes!
      reset_router!
      default_routes!
      true
    end

routes() click to toggle source

Returns the routes of our app.

@example

    MyApp.routes

    # File lib/padrino-core/application.rb, line 88
    def routes
      router.routes
    end

run!(options={}) click to toggle source

Run the Padrino app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)

@see Padrino::Server#start

    # File lib/padrino-core/application.rb, line 118
    def run!(options={})
      return unless Padrino.load!
      Padrino.mount(settings.to_s).to('/')
      Padrino.run!(options)
    end

setup_application!() click to toggle source

Setup the application by registering initializers, load paths and logger Invoked automatically when an application is first instantiated

@return [TrueClass]

    # File lib/padrino-core/application.rb, line 98
    def setup_application!
      return if @_configured
      settings.require_dependencies
      settings.default_filters!
      settings.default_routes!
      settings.default_errors!
      if defined?(I18n)
        I18n.load_path << settings.locale_path
        I18n.reload!
      end
      @_configured = true
      @_configured
    end

### Protected Class Methods

default_configuration!() click to toggle source

Defines default settings for Padrino application

    # File lib/padrino-core/application.rb, line 172
    def default_configuration!
      # Overwriting Sinatra defaults
      set :app_file, File.expand_path(caller_files.first || $0) # Assume app file is first caller
      set :environment, Padrino.env
      set :reload, Proc.new { development? }
      set :logging, Proc.new { development? }
      set :method_override, true
      set :sessions, false
      set :public_folder, Proc.new { Padrino.root('public', uri_root) }
      set :views, Proc.new { File.join(root, 'views') }
      set :images_path, Proc.new { File.join(public_folder, 'images') }
      set :protection, true
      # Haml specific
      set :haml, { :ugly => (Padrino.env == :production) } if defined?(Haml)
      # Padrino specific
      set :uri_root, '/'
      set :app_name, settings.to_s.underscore.to_sym
      set :default_builder, 'StandardFormBuilder'
      set :authentication, false
      # Padrino locale
      set :locale_path, Proc.new { Dir[File.join(settings.root, '/locale/**/*.{rb,yml}')] }
      # Authenticity token
      set :protect_from_csrf, false
      set :allow_disabled_csrf, false
      # Load the Global Configurations
      class_eval(&Padrino.apps_configuration) if Padrino.apps_configuration
    end

default_errors!() click to toggle source

This log errors for production environments

    # File lib/padrino-core/application.rb, line 228
    def default_errors!
      configure :production do
        error ::Exception do
          boom = env['sinatra.error']
          logger.error ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n ")
          response.status = 500
          content_type 'text/html'
          '<h1>Internal Server Error</h1>'
        end unless errors.has_key?(::Exception)
      end
    end

default_filters!() click to toggle source

This filter it's used for know the format of the request, and automatically set the content type.

    # File lib/padrino-core/application.rb, line 216
    def default_filters!
      before do
        unless @_content_type
          @_content_type = :html
          response['Content-Type'] = 'text/html;charset=utf-8'
        end
      end
    end

default_routes!() click to toggle source

We need to add almost __sinatra__ images.

    # File lib/padrino-core/application.rb, line 203
    def default_routes!
      configure :development do
        get '*__sinatra__/:image.png' do
          content_type :png
          filename = File.dirname(__FILE__) + "/images/#{params[:image]}.png"
          send_file filename
        end
      end
    end

require_dependencies() click to toggle source

Requires all files within the application load paths

    # File lib/padrino-core/application.rb, line 243
    def require_dependencies
      Padrino.set_load_paths(*load_paths)
      Padrino.require_dependencies(dependencies, :force => true)
    end

### Public Instance Methods

logger() click to toggle source

Returns the logger for this application.

@return [Padrino::Logger] Logger associated with this app.

    # File lib/padrino-core/application.rb, line 23
    def logger
      Padrino.logger
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.