Class: Padrino::Application

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dependenciesArray

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

Examples:

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

Returns:

  • (Array)

    list of path globs to load as dependencies



143
144
145
146
147
148
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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_pathsArray

Returns directory that need to be added to $LOAD_PATHS from this application.

Returns:

  • (Array)

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



128
129
130
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application.rb', line 128

def load_paths
  @_load_paths ||= %w[models lib mailers controllers helpers].map { |path| File.join(settings.root, path) }
end

.prerequisitesObject

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

Examples:

Adding a custom perequisite

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


164
165
166
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application.rb', line 164

def prerequisites
  @_prerequisites ||= []
end

.reload!TrueClass

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.

Examples:

MyApp.reload!

Returns:

  • (TrueClass)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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!TrueClass

Resets application routes to only routes not defined by the user

Examples:

MyApp.reset_routes!

Returns:

  • (TrueClass)


76
77
78
79
80
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application.rb', line 76

def reset_routes!
  reset_router!
  default_routes!
  true
end

.routesObject

Returns the routes of our app.

Examples:

MyApp.routes


88
89
90
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application.rb', line 88

def routes
  router.routes
end

.run!(options = {}) ⇒ Object

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

See Also:



118
119
120
121
122
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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!TrueClass

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

Returns:

  • (TrueClass)


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

Instance Method Details

#loggerPadrino::Logger

Returns the logger for this application.

Returns:



23
24
25
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application.rb', line 23

def logger
  Padrino.logger
end