Sinatra::Base
Subclasses of this become independent Padrino applications (stemming from Sinatra::Application) These subclassed applications can be easily mounted into other Padrino applications as well.
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 138 def dependencies @_dependencies ||= [ "urls.rb", "config/urls.rb", "mailers/*.rb", "mailers.rb", "controllers/**/*.rb", "controllers.rb", "helpers/**/*.rb", "helpers.rb" ].map { |file| Dir[File.join(self.root, file)] }.flatten end
# File lib/padrino-core/application.rb, line 24 def inherited(base) # @private logger.devel "Setup #{base}" 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) super(base) # Loading the subclass inherited method end
@return [Array]
directory that need to be added to +$LOAD_PATHS+ from this application
# File lib/padrino-core/application.rb, line 123 def load_paths @_load_paths ||= %(models lib mailers controllers helpers).map { |path| File.join(self.root, path) } end
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 159 def prerequisites @_prerequisites ||= [] end
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 49 def reload! logger.devel "Reloading #{self}" @_dependencies = nil # Reset dependencies reset! # Reset sinatra app reset_router! # Reset all routes Padrino.require_dependencies(self.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
Resets application routes to only routes not defined by the user
@return [TrueClass]
@example
MyApp.reset_routes!
# File lib/padrino-core/application.rb, line 71 def reset_routes! reset_router! default_routes! true end
Returns the routes of our app.
@example
MyApp.routes
# File lib/padrino-core/application.rb, line 83 def routes router.routes end
Run the Padrino app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)
# File lib/padrino-core/application.rb, line 113 def run!(options={}) return unless Padrino.load! Padrino.mount(self.to_s).to("/") Padrino.run!(options) end
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 93 def setup_application! return if @_configured self.require_dependencies self.default_filters! self.default_routes! self.default_errors! if defined?(I18n) I18n.load_path << self.locale_path I18n.reload! end @_configured = true @_configured end
Defines default settings for Padrino application
# File lib/padrino-core/application.rb, line 167 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, "images") } set :protection, false # Padrino specific set :uri_root, "/" set :app_name, self.to_s.underscore.to_sym set :default_builder, 'StandardFormBuilder' set :flash, defined?(Sinatra::Flash) || defined?(Rack::Flash) set :authentication, false # Padrino locale set :locale_path, Proc.new { Dir[File.join(self.root, "/locale/**/*.{rb,yml}")] } # Load the Global Configurations class_eval(&Padrino.apps_configuration) if Padrino.apps_configuration end
This log errors for production environments
# File lib/padrino-core/application.rb, line 219 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
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 207 def default_filters! before do unless @_content_type @_content_type = :html response['Content-Type'] = 'text/html;charset=utf-8' end end end
We need to add almost __sinatra__ images.
# File lib/padrino-core/application.rb, line 194 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
Returns the logger for this application.
@return [Padrino::Logger] Logger associated with this app.
# File lib/padrino-core/application.rb, line 18 def logger Padrino.logger end
Generated with the Darkfish Rdoc Generator 2.