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
Attributes
Public Class Methods
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
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
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
Clears all previously configured middlewares.
@return [Array]
An empty array
# File lib/padrino-core.rb, line 147 def clear_middleware! @middleware = [] end
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
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
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
@returns [Gem::Specification]
# File lib/padrino-core.rb, line 189 def gems @gems ||= [] end
Stores global configuration blocks.
# File lib/padrino-core.rb, line 99 def global_configurations @_global_configurations ||= [] end
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
@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
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
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
@returns [<Padrino::Module>]
# File lib/padrino-core.rb, line 195 def modules @modules ||= [] end
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
@return [Array]
the mounted padrino applications (MountedApp objects)
# File lib/padrino-core/mounter.rb, line 245 def mounted_apps @mounted_apps ||= [] end
@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
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
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
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
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.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
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
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
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
# 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
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