Class: Padrino::Application

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
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

.callers_to_ignoreObject



38
39
40
# File 'lib/padrino-core/application.rb', line 38

def callers_to_ignore
  @callers_to_ignore ||= super + PADRINO_IGNORE_CALLERS
end

.default(option, *args, &block) ⇒ Object



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

def default(option, *args, &block)
  set(option, *args, &block) unless respond_to?(option)
end

.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



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/padrino-core/application.rb', line 132

def dependencies
  [
    'urls.rb',
    'config/urls.rb',
    'mailers/*.rb',
    'mailers.rb',
    'controllers/**/*.rb',
    'controllers.rb',
    'helpers/**/*.rb',
    'helpers.rb',
  ].flat_map{ |file| Dir.glob(File.join(settings.root, file)) }
end

.inherited(base) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/padrino-core/application.rb', line 30

def inherited(base)
  begun_at = Time.now
  super(base)
  base.prerequisites.replace(self.prerequisites.dup)
  base.default_configuration!
  logger.devel :setup, begun_at, base
end

.layout_path(layout) ⇒ Object

Returns an absolute path of application layout.

Examples:

Admin.layout_path :application #=> "/home/user/test/admin/views/layouts/application"


105
106
107
# File 'lib/padrino-core/application.rb', line 105

def layout_path(layout)
  view_path("layouts/#{layout}")
end

.prerequisitesObject

An array of file to load before your app.rb, basically are files which 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 prerequisite

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


160
161
162
# File 'lib/padrino-core/application.rb', line 160

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)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/padrino-core/application.rb', line 53

def reload!
  logger.devel "Reloading application #{settings}"
  reset!
  reset_router!
  Padrino.require_dependencies(settings.app_file, :force => true)
  require_dependencies
  default_routes
  default_errors
  I18n.reload! if defined?(I18n)
  true
end

.require_dependenciesObject (protected)

Requires all files within the application load paths.



173
174
175
# File 'lib/padrino-core/application.rb', line 173

def require_dependencies
  Padrino.require_dependencies(dependencies, :force => true)
end

.reset_routes!TrueClass

Resets application routes to only routes not defined by the user.

Examples:

MyApp.reset_routes!

Returns:

  • (TrueClass)


73
74
75
76
77
# File 'lib/padrino-core/application.rb', line 73

def reset_routes!
  reset_router!
  default_routes
  true
end

.routesObject

Returns the routes of our app.

Examples:

MyApp.routes


85
86
87
# File 'lib/padrino-core/application.rb', line 85

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:



115
116
117
118
119
# File 'lib/padrino-core/application.rb', line 115

def run!(options={})
  return unless Padrino.load!
  Padrino.mount(settings.to_s).to('/')
  Padrino.run!(options)
end

.view_path(view) ⇒ Object

Returns an absolute path of view in application views folder.

Examples:

Admin.view_path 'users/index' #=> "/home/user/test/admin/views/users/index"


95
96
97
# File 'lib/padrino-core/application.rb', line 95

def view_path(view)
  File.expand_path(view, views)
end

Instance Method Details

#loggerPadrino::Logger

Returns the logger for this application.

Returns:



25
26
27
# File 'lib/padrino-core/application.rb', line 25

def logger
  Padrino.logger
end