module Padrino

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.

Constants

PADRINO_IGNORE_CALLERS

List of callers in a Padrino application that should be ignored as part of a stack trace.

VERSION

The version constant for the current version of Padrino.

Attributes

mounted_root[W]

Public Class Methods

add_middleware(router) click to toggle source

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

# File lib/padrino-core.rb, line 123
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
application() click to toggle source

The resulting rack builder mapping each 'mounted' application.

@return [Padrino::Router]

The router for the application.

@raise [ApplicationLoadError]

No applications were mounted.
# File lib/padrino-core.rb, line 71
def application
  warn 'WARNING! No apps are mounted. Please, mount apps in `config/apps.rb`' if Padrino.mounted_apps.empty?
  router = Padrino::Router.new
  Padrino.mounted_apps.each { |app| app.map_onto(router) }
  middleware.empty? ? router : add_middleware(router)
end
bin(*args) click to toggle source

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

@param [Array] args

command or commands to execute

@return [Boolean]

@example

Padrino.bin('start', '-e production')
# File 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
clear_middleware!() click to toggle source

Clears all previously configured middlewares.

@return [Array]

An empty array
# File lib/padrino-core.rb, line 147
def clear_middleware!
  @middleware = []
end
configure_apps(&block) click to toggle source

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

@yield []

The given block will be called to configure each application.

@example

Padrino.configure_apps do
  enable  :sessions
  disable :raise_errors
end
# File lib/padrino-core.rb, line 91
def configure_apps(&block)
  return  unless block_given?
  global_configurations << block
end
env() click to toggle source

Helper method that return {RACK_ENV}.

@return [Symbol]

The Padrino Environment.
# File lib/padrino-core.rb, line 58
def env
  @_env ||= RACK_ENV.to_s.downcase.to_sym
end
gem(name, main_module) click to toggle source

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.

@param [String] name

The name of the gem being registered.

@param [Module] main_module

The main module of the gem.

@returns The root path of the loaded gem

# File lib/padrino-core.rb, line 180
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
gems() click to toggle source

@returns [Gem::Specification]

# File lib/padrino-core.rb, line 189
def gems
  @gems ||= []
end
global_configurations() click to toggle source

Stores global configuration blocks.

# File lib/padrino-core.rb, line 99
def global_configurations
  @_global_configurations ||= []
end
insert_mounted_app(mounter) click to toggle source

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

@param [Padrino::Mounter] mounter

# File lib/padrino-core/mounter.rb, line 254
def insert_mounted_app(mounter)
  Padrino.mounted_apps.push(mounter) unless Padrino.mounted_apps.include?(mounter)
end
logger() click to toggle source

@return [Padrino::Logger]

@example

logger.debug "foo"
logger.warn "bar"
# File lib/padrino-core/logger.rb, line 17
def self.logger
  Padrino::Logger.logger
end
logger=(value) click to toggle source

Set the padrino logger.

@param [Object] value

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

@return [Object]

The given value.

@example using ruby default logger

require 'logger'
new_logger = ::Logger.new(STDOUT)
new_logger.extend(Padrino::Logger::Extensions)
Padrino.logger = new_logger

@example using ActiveSupport

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

@example using custom logger class

require 'logger'
class CustomLogger < ::Logger
  include Padrino::Logger::Extensions
end
Padrino.logger = CustomLogger.new(STDOUT)
# File lib/padrino-core/logger.rb, line 47
def self.logger=(value)
  Padrino::Logger.logger = value
end
middleware() click to toggle source

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

@return [Array<Array<Class, Array, Proc>>]

The middleware classes.
# File lib/padrino-core.rb, line 137
def middleware
  @middleware ||= []
end
modules() click to toggle source

@returns [<Padrino::Module>]

# File lib/padrino-core.rb, line 195
def modules
  @modules ||= []
end
mount(name, options={}) click to toggle source

Mounts a new sub-application onto Padrino project.

@see Padrino::Mounter#new

@example

Padrino.mount("blog_app").to("/blog")
# File lib/padrino-core/mounter.rb, line 266
def mount(name, options={})
  Mounter.new(name, options)
end
mounted_apps() click to toggle source

@return [Array]

the mounted padrino applications (MountedApp objects)
# File lib/padrino-core/mounter.rb, line 245
def mounted_apps
  @mounted_apps ||= []
end
mounted_root(*args) click to toggle source

@param [Array] args

@return [String]

the root to the mounted apps base directory.
# File lib/padrino-core/mounter.rb, line 237
def mounted_root(*args)
  Padrino.root(@mounted_root ||= "", *args)
end
replace_with_binstub(executable) click to toggle source

Replaces the current process with it's binstub.

# File lib/padrino-core/cli/binstub.rb, line 5
def self.replace_with_binstub(executable)
  begin
    return if Bundler.definition.missing_specs.empty?
  rescue NameError, NoMethodError, Bundler::GemfileNotFound
  end

  project_root = Dir.pwd
  until project_root.empty?
    break if File.file?(File.join(project_root, 'Gemfile'))
    project_root = project_root.rpartition('/').first
  end

  if %w(Gemfile .components).all? { |file| File.file?(File.join(project_root, file)) }
    binstub = File.join(project_root, 'bin', executable)
    if File.file?(binstub)
      exec Gem.ruby, binstub, *ARGV
    else
      puts 'Please run `bundle install --binstubs` from your project root to generate bundle-specific executables'
      exit!
    end
  end
end
root(*args) click to toggle source

Helper method for file references.

@param [Array<String>] args

The directories to join to {PADRINO_ROOT}.

@return [String]

The absolute path.

@example

# Referencing a file in config called settings.yml
Padrino.root("config", "settings.yml")
# returns PADRINO_ROOT + "/config/setting.yml"
# File lib/padrino-core.rb, line 48
def root(*args)
  File.expand_path(File.join(PADRINO_ROOT, *args))
end
ruby_command() click to toggle source

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

@return [String]

path to ruby bin executable
# File 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={}) click to toggle source

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

@example

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"
# File lib/padrino-core/server.rb, line 10
def self.run!(options={})
  Padrino.load!
  Server.start(*detect_application(options))
end
set_encoding() click to toggle source

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.

@see github.com/rtomayko/tilt/issues/75

@return [NilClass]

# File lib/padrino-core.rb, line 114
def set_encoding
  # remove after 0.15
  warn 'Warning! Padrino.set_encoding is deprecated. Padrino no longer manages ruby default encodings'
  nil
end
use(mw, *args, &block) click to toggle source

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

@param [Class] m

The middleware class.

@param [Array] args

The arguments for the middleware.

@yield []

The given block will be passed to the initialized middleware.
# File lib/padrino-core.rb, line 163
def use(mw, *args, &block)
  middleware << [mw, args, block]
end
version() click to toggle source

The current Padrino version.

@return [String]

The version number.
# File lib/padrino-core/version.rb, line 17
def self.version
  VERSION
end

Private Class Methods

caller_files() click to toggle source

Like +Kernel#caller+ but excluding certain magic entries and without line / method information; the resulting array contains filenames only.

@return [Array<String>]

The files of the calling methods.
# File lib/padrino-core/caller.rb, line 48
def self.caller_files
  caller(1).
    map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
    reject { |file,line| PADRINO_IGNORE_CALLERS.any? { |pattern| file =~ pattern } }.
    map    { |file,line| file }
end
detect_application(options) click to toggle source
# File lib/padrino-core/server.rb, line 19
def self.detect_application(options)
  default_config_file = 'config.ru'
  if (config_file = options.delete(:config)) || File.file?(default_config_file)
    config_file ||= default_config_file
    fail "Rack config file `#{config_file}` must have `.ru` extension" unless config_file =~ /\.ru$/
    rack_app, rack_options = Rack::Builder.parse_file(config_file)
    [rack_app, rack_options.merge(options)]
  else
    [Padrino.application, options]
  end
end
first_caller() click to toggle source

The filename for the file that is the direct caller (first caller).

@return [String]

The file the caller method exists in.
# File lib/padrino-core/caller.rb, line 37
def self.first_caller
  caller_files.first
end