Module: Padrino

Extended by:
Configuration, Loader
Defined in:
padrino-core/lib/padrino-core/version.rb,
padrino-gen/lib/padrino-gen.rb,
padrino-core/lib/padrino-core.rb,
padrino-cache/lib/padrino-cache.rb,
padrino-admin/lib/padrino-admin.rb,
padrino-mailer/lib/padrino-mailer.rb,
padrino-performance/lib/suites/mem.rb,
padrino-helpers/lib/padrino-helpers.rb,
padrino-performance/lib/suites/json.rb,
padrino-gen/lib/padrino-gen/command.rb,
padrino-core/lib/padrino-core/tasks.rb,
padrino-core/lib/padrino-core/server.rb,
padrino-core/lib/padrino-core/logger.rb,
padrino-core/lib/padrino-core/loader.rb,
padrino-core/lib/padrino-core/module.rb,
padrino-core/lib/padrino-core/router.rb,
padrino-core/lib/padrino-core/caller.rb,
padrino-core/lib/padrino-core/filter.rb,
padrino-core/lib/padrino-core/command.rb,
padrino-core/lib/padrino-core/mounter.rb,
padrino-helpers/lib/padrino/rendering.rb,
padrino-core/lib/padrino-core/reloader.rb,
padrino-mailer/lib/padrino-mailer/mime.rb,
padrino-mailer/lib/padrino-mailer/base.rb,
padrino-mailer/lib/padrino-mailer/helpers.rb,
padrino-support/lib/padrino-support/utils.rb,
padrino-core/lib/padrino-core/application.rb,
padrino-core/lib/padrino-core/path_router.rb,
padrino-gen/lib/padrino-gen/generators/app.rb,
padrino-gen/lib/padrino-gen/generators/cli.rb,
padrino-gen/lib/padrino-gen/generators/task.rb,
padrino-core/lib/padrino-core/reloader/rack.rb,
padrino-core/lib/padrino-core/configuration.rb,
padrino-gen/lib/padrino-gen/generators/model.rb,
padrino-cache/lib/padrino-cache/helpers/page.rb,
padrino-gen/lib/padrino-gen/generators/helper.rb,
padrino-gen/lib/padrino-gen/generators/mailer.rb,
padrino-gen/lib/padrino-gen/generators/runner.rb,
padrino-gen/lib/padrino-gen/generators/plugin.rb,
padrino-admin/lib/padrino-admin/access_control.rb,
padrino-performance/lib/padrino-performance/os.rb,
padrino-gen/lib/padrino-gen/generators/project.rb,
padrino-gen/lib/padrino-gen/generators/actions.rb,
padrino-admin/lib/padrino-admin/generators/orm.rb,
padrino-core/lib/padrino-core/reloader/storage.rb,
padrino-helpers/lib/padrino-helpers/tag_helpers.rb,
padrino-core/lib/padrino-core/path_router/route.rb,
padrino-core/lib/padrino-core/application/flash.rb,
padrino-gen/lib/padrino-gen/generators/migration.rb,
padrino-gen/lib/padrino-gen/generators/component.rb,
padrino-helpers/lib/padrino-helpers/form_helpers.rb,
padrino-cache/lib/padrino-cache/helpers/fragment.rb,
padrino-core/lib/padrino-core/application/routing.rb,
padrino-gen/lib/padrino-gen/generators/controller.rb,
padrino-core/lib/padrino-core/path_router/matcher.rb,
padrino-helpers/lib/padrino/rendering/erb_template.rb,
padrino-helpers/lib/padrino-helpers/number_helpers.rb,
padrino-helpers/lib/padrino-helpers/output_helpers.rb,
padrino-helpers/lib/padrino-helpers/render_helpers.rb,
padrino-core/lib/padrino-core/path_router/compiler.rb,
padrino-admin/lib/padrino-admin/generators/actions.rb,
padrino-helpers/lib/padrino-helpers/format_helpers.rb,
padrino-performance/lib/padrino-performance/version.rb,
padrino-helpers/lib/padrino/rendering/haml_template.rb,
padrino-helpers/lib/padrino/rendering/slim_template.rb,
padrino-cache/lib/padrino-cache/helpers/cache_store.rb,
padrino-cache/lib/padrino-cache/helpers/cache_object.rb,
padrino-admin/lib/padrino-admin/generators/admin_app.rb,
padrino-admin/lib/padrino-admin/helpers/view_helpers.rb,
padrino-admin/lib/padrino-admin/generators/admin_page.rb,
padrino-helpers/lib/padrino/rendering/erubis_template.rb,
padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb,
padrino-helpers/lib/padrino-helpers/form_helpers/errors.rb,
padrino-core/lib/padrino-core/path_router/error_handler.rb,
padrino-helpers/lib/padrino-helpers/translation_helpers.rb,
padrino-helpers/lib/padrino-helpers/form_helpers/options.rb,
padrino-helpers/lib/padrino-helpers/form_helpers/security.rb,
padrino-core/lib/padrino-core/application/show_exceptions.rb,
padrino-core/lib/padrino-core/application/params_protection.rb,
padrino-core/lib/padrino-core/application/application_setup.rb,
padrino-core/lib/padrino-core/mounter/application_extension.rb,
padrino-core/lib/padrino-core/application/authenticity_token.rb,
padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb,
padrino-admin/lib/padrino-admin/helpers/authentication_helpers.rb,
padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb,
padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb,
padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb,
padrino-helpers/lib/padrino-helpers/form_builder/standard_form_builder.rb,
padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb

Overview

Manages current Padrino version for use in gem generation.

We put this in a separate file so you can get padrino version without include full padrino core.

Defined Under Namespace

Modules: Admin, ApplicationSetup, Cache, Configuration, Flash, Generators, Helpers, Loader, Mailer, Module, ParamsProtection, PathRouter, Performance, Reloader, Rendering, Routing, Tasks, Utils Classes: Application, AuthenticityToken, Filter, Logger, Mounter, Router, Server

Constant Summary

PADRINO_IGNORE_CALLERS =
[
  %r{lib/padrino-.*$},
  %r{/padrino-.*/(lib|bin)},
  %r{/bin/padrino$},
  %r{/sinatra(/(base|main|show_?exceptions))?\.rb$},
  %r{lib/tilt.*\.rb$},
  %r{lib/rack.*\.rb$},
  %r{lib/mongrel.*\.rb$},
  %r{lib/shotgun.*\.rb$},
  %r{bin/shotgun$},
  %r{\(.*\)},
  %r{shoulda/context\.rb$},
  %r{mocha/integration},
  %r{test/unit},
  %r{rake_test_loader\.rb},
  %r{custom_require\.rb$},
  %r{active_support},
  %r{/thor},
  %r{/lib/bundler},
]
VERSION =
'0.13.2'

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Loader

after_load, before_load, called_from, clear!, dependency_paths, load!, loaded?, precompile_all_routes!, reload!, require_dependencies

Methods included from Configuration

config, configure

Class Attribute Details

.mounted_root(*args) ⇒ String

Returns the root to the mounted apps base directory.

Parameters:

  • args (Array)

Returns:

  • (String)

    the root to the mounted apps base directory.



234
235
236
# File 'padrino-core/lib/padrino-core/mounter.rb', line 234

def mounted_root(*args)
  Padrino.root(@mounted_root ||= "", *args)
end

Class Method Details

.add_middleware(router) ⇒ Object

Creates Rack stack with the router added to the middleware chain.



122
123
124
125
126
127
# File 'padrino-core/lib/padrino-core.rb', line 122

def add_middleware(router)
  builder = Rack::Builder.new
  middleware.each{ |mw,args,block| builder.use(mw, *args, &block) }
  builder.run(router)
  builder.to_app
end

.after_load(&block) ⇒ Object



9
10
11
12
13
# File 'padrino-performance/lib/suites/mem.rb', line 9

def after_load(&block)
  @_after_load ||= []
  @_after_load << block if block_given?
  @_after_load
end

.applicationPadrino::Router

The resulting rack builder mapping each 'mounted' application.

Returns:

Raises:

  • (ApplicationLoadError)

    No applications were mounted.



70
71
72
73
74
75
# File 'padrino-core/lib/padrino-core.rb', line 70

def application
  warn 'WARNING! No apps are mounted. Please, mount apps in `config/apps.rb`' unless Padrino.mounted_apps.present?
  router = Padrino::Router.new
  Padrino.mounted_apps.each { |app| app.map_onto(router) }
  middleware.present? ? add_middleware(router) : router
end

.before_load(&block) ⇒ Object



3
4
5
6
7
# File 'padrino-performance/lib/suites/mem.rb', line 3

def before_load(&block)
  @_before_load ||= []
  @_before_load << block if block_given?
  @_before_load
end

.bin(*args) ⇒ Boolean

This method return the correct location of padrino bin or exec it using Kernel#system with the given args.

Examples:

Padrino.bin('start', '-e production')

Parameters:

  • args (Array)

    command or commands to execute

Returns:

  • (Boolean)


16
17
18
19
# File 'padrino-core/lib/padrino-core/command.rb', line 16

def self.bin(*args)
  @_padrino_bin ||= [self.ruby_command, File.expand_path("../../../bin/padrino", __FILE__)]
  args.empty? ? @_padrino_bin : system(args.unshift(@_padrino_bin).join(" "))
end

.bin_gen(*args) ⇒ Object

This method return the correct location of padrino-gen bin or exec it using Kernel#system with the given args.

Examples:

Padrino.bin_gen(:app, name.to_s, "-r=#{destination_root}")

Parameters:

  • args. (Array<String>)

    Splat of arguments to pass to padrino-gen.



14
15
16
17
# File 'padrino-gen/lib/padrino-gen/command.rb', line 14

def self.bin_gen(*args)
  @_padrino_gen_bin ||= [Padrino.ruby_command, File.expand_path("../../../bin/padrino-gen", __FILE__)]
  system args.unshift(@_padrino_gen_bin).join(" ")
end

.cacheObject

Returns the caching engine.

Examples:

# with: Padrino.cache = Padrino::Cache.new(:File, :dir => /my/cache/path)
Padrino.cache['val'] = 'test'
Padrino.cache['val'] # => 'test'
Padrino.cache.delete('val')
Padrino.cache.clear


19
20
21
# File 'padrino-cache/lib/padrino-cache.rb', line 19

def cache
  @_cache
end

.cache=(value) ⇒ Object

Set the caching engine.

Examples:

Padrino.cache = Padrino::Cache.new(:LRUHash) # default choice
Padrino.cache = Padrino::Cache.new(:File, :dir => Padrino.root('tmp', app_name.to_s, 'cache')) # Keeps cached values in file
Padrino.cache = Padrino::Cache.new(:Memcached) # Uses default server at localhost
Padrino.cache = Padrino::Cache.new(:Memcached, :server => '127.0.0.1:11211', :exception_retry_limit => 1)
Padrino.cache = Padrino::Cache.new(:Memcached, :backend => memcached_or_dalli_instance)
Padrino.cache = Padrino::Cache.new(:Redis) # Uses default server at localhost
Padrino.cache = Padrino::Cache.new(:Redis, :host => '127.0.0.1', :port => 6379, :db => 0)
Padrino.cache = Padrino::Cache.new(:Redis, :backend => redis_instance)
Padrino.cache = Padrino::Cache.new(:Mongo) # Uses default server at localhost
Padrino.cache = Padrino::Cache.new(:Mongo, :backend => mongo_client_instance)

# You can manage your cache from anywhere in your app:

Padrino.cache['val'] = 'test'
Padrino.cache['val'] # => 'test'
Padrino.cache.delete('val')
Padrino.cache.clear

Parameters:

  • value

    Instance of Moneta store



48
49
50
# File 'padrino-cache/lib/padrino-cache.rb', line 48

def cache=(value)
  @_cache= value
end

.clear_middleware!Array

Clears all previously configured middlewares.

Returns:

  • (Array)

    An empty array



146
147
148
# File 'padrino-core/lib/padrino-core.rb', line 146

def clear_middleware!
  @middleware = []
end

.configure_apps { ... } ⇒ Object

Configure Global Project Settings for mounted apps. These can be overloaded in each individual app's own personal configuration. This can be used like:

Examples:

Padrino.configure_apps do
  enable  :sessions
  disable :raise_errors
end

Yields:

  • The given block will be called to configure each application.



90
91
92
93
# File 'padrino-core/lib/padrino-core.rb', line 90

def configure_apps(&block)
  return  unless block_given?
  global_configurations << block
end

.envSymbol

Helper method that return RACK_ENV.

Returns:

  • (Symbol)

    The Padrino Environment.



57
58
59
# File 'padrino-core/lib/padrino-core.rb', line 57

def env
  @_env ||= RACK_ENV.to_s.downcase.to_sym
end

.gem(name, main_module) ⇒ Object

Registers a gem with padrino. This relieves the caller from setting up loadpaths by itself and enables Padrino to look up apps in gem folder.

The name given has to be the proper gem name as given in the gemspec.

Parameters:

  • name (String)

    The name of the gem being registered.

  • main_module (Module)

    The main module of the gem.



179
180
181
182
183
184
# File 'padrino-core/lib/padrino-core.rb', line 179

def gem(name, main_module)
  _, spec = Gem.loaded_specs.find{|spec_pair| spec_pair[0] == name }
  gems << spec
  modules << main_module
  spec.full_gem_path
end

.gemsObject



188
189
190
# File 'padrino-core/lib/padrino-core.rb', line 188

def gems
  @gems ||= []
end

.global_configurationsObject

Stores global configuration blocks.



98
99
100
# File 'padrino-core/lib/padrino-core.rb', line 98

def global_configurations
  @_global_configurations ||= []
end

.insert_mounted_app(mounter) ⇒ Object

Inserts a Mounter object into the mounted applications (avoids duplicates).

Parameters:



251
252
253
# File 'padrino-core/lib/padrino-core/mounter.rb', line 251

def insert_mounted_app(mounter)
  Padrino.mounted_apps.push(mounter) unless Padrino.mounted_apps.include?(mounter)
end

.loggerPadrino::Logger

Examples:

logger.debug "foo"
logger.warn "bar"

Returns:



17
18
19
# File 'padrino-core/lib/padrino-core/logger.rb', line 17

def self.logger
  Padrino::Logger.logger
end

.logger=(value) ⇒ Object

Set the padrino logger.

Examples:

using ruby default logger

require 'logger'
Padrino.logger = Logger.new(STDOUT)

using ActiveSupport

require 'active_support/buffered_logger'
Padrino.logger = Buffered.new(STDOUT)

Parameters:

  • value (Object)

    an object that respond to <<, write, puts, debug, warn etc..

Returns:

  • (Object)

    The given value.



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

def self.logger=(value)
  Padrino::Logger.logger = value
end

.middlewareArray<Array<Class, Array, Proc>>

A Rack::Builder object that allows to add middlewares in front of all Padrino applications.

Returns:

  • (Array<Array<Class, Array, Proc>>)

    The middleware classes.



136
137
138
# File 'padrino-core/lib/padrino-core.rb', line 136

def middleware
  @middleware ||= []
end

.modulesObject



194
195
196
# File 'padrino-core/lib/padrino-core.rb', line 194

def modules
  @modules ||= []
end

.mount(name, options = {}) ⇒ Object

Mounts a new sub-application onto Padrino project.

Examples:

Padrino.mount("blog_app").to("/blog")

See Also:

  • Padrino::Mounter#new


263
264
265
# File 'padrino-core/lib/padrino-core/mounter.rb', line 263

def mount(name, options={})
  Mounter.new(name, options)
end

.mounted_appsArray

Returns the mounted padrino applications (MountedApp objects)

Returns:

  • (Array)

    the mounted padrino applications (MountedApp objects)



242
243
244
# File 'padrino-core/lib/padrino-core/mounter.rb', line 242

def mounted_apps
  @mounted_apps ||= []
end

.perf_memusage_commandObject



15
16
17
18
19
20
21
22
23
# File 'padrino-performance/lib/suites/mem.rb', line 15

def perf_memusage_command
  if Performance::OS.mac?
    "vmmap #{$$} | tail -5"
  elsif Performance::OS.linux?
    "pmap #{$$} | tail -1"
  elsif Performance::OS.windows?
    "tasklist /FI \"PID eq #{$$}\""
  end
end

.root(*args) ⇒ String

Helper method for file references.

Examples:

# Referencing a file in config called settings.yml
Padrino.root("config", "settings.yml")
# returns PADRINO_ROOT + "/config/setting.yml"

Parameters:

Returns:

  • (String)

    The absolute path.



47
48
49
# File 'padrino-core/lib/padrino-core.rb', line 47

def root(*args)
  File.expand_path(File.join(PADRINO_ROOT, *args))
end

.ruby_commandString

Return the path to the ruby interpreter taking into account multiple installations and windows extensions.

Returns:

  • (String)

    path to ruby bin executable



28
29
30
31
32
33
34
35
36
37
# File 'padrino-core/lib/padrino-core/command.rb', line 28

def self.ruby_command
  @ruby_command ||= begin
    ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
    ruby << RbConfig::CONFIG['EXEEXT']

    # escape string in case path to ruby executable contain spaces.
    ruby.sub!(/.*\s.*/m, '"\&"')
    ruby
  end
end

.run!(options = {}) ⇒ Object

Runs the Padrino apps as a self-hosted server using: thin, mongrel, or WEBrick in that order.

Examples:

Padrino.run! # with these defaults => host: "127.0.0.1", port: "3000", adapter: the first found
Padrino.run!("0.0.0.0", "4000", "mongrel") # use => host: "0.0.0.0", port: "4000", adapter: "mongrel"


10
11
12
13
# File 'padrino-core/lib/padrino-core/server.rb', line 10

def self.run!(options={})
  Padrino.load!
  Server.start(*detect_application(options))
end

.set_encodingNilClass

Set Encoding.default_internal and Encoding.default_external to Encoding::UFT_8.

Please note that in 1.9.2 with some template engines like haml you should turn off Encoding.default_internal to prevent problems.

Returns:

  • (NilClass)

See Also:



113
114
115
116
117
# File 'padrino-core/lib/padrino-core.rb', line 113

def set_encoding
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
  nil
end

.use(mw, *args) { ... } ⇒ Object

Convenience method for adding a Middleware to the whole padrino app.

Parameters:

  • m (Class)

    The middleware class.

  • args (Array)

    The arguments for the middleware.

Yields:

  • The given block will be passed to the initialized middleware.



162
163
164
# File 'padrino-core/lib/padrino-core.rb', line 162

def use(mw, *args, &block)
  middleware << [mw, args, block]
end

.versionString

The current Padrino version.

Returns:

  • (String)

    The version number.



17
18
19
# File 'padrino-core/lib/padrino-core/version.rb', line 17

def self.version
  VERSION
end