Files

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

after_load(&block) click to toggle source

Hooks to be called after a load/reload.

@yield []

The given block will be called after Padrino was loaded/reloaded.

@return [Array<Proc>]

The load/reload hooks.

@example

after_load do
  DataMapper.finalize
end
# File lib/padrino-core/loader.rb, line 37
def after_load(&block)
  @_after_load ||= []
  @_after_load << block if block_given?
  @_after_load
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 54
def application
  raise ApplicationLoadError, "At least one app must be mounted!" unless Padrino.mounted_apps && Padrino.mounted_apps.any?
  router = Padrino::Router.new
  Padrino.mounted_apps.each { |app| app.map_onto(router) }

  if middleware.present?
    builder = Rack::Builder.new
    middleware.each { |c,a,b| builder.use(c, *a, &b) }
    builder.run(router)
    builder.to_app
  else
    router
  end
end
apps_configuration() click to toggle source

Returns project-wide configuration settings defined in {configure_apps} block.

# File lib/padrino-core.rb, line 90
def apps_configuration
  @_global_configuration
end
before_load(&block) click to toggle source

Hooks to be called before a load/reload.

@yield []

The given block will be called before Padrino was loaded/reloaded.

@return [Array<Proc>]

The load/reload before hooks.

@example

before_load do
  pre_initialize_something
end
# File lib/padrino-core/loader.rb, line 17
def before_load(&block)
  @_before_load ||= []
  @_before_load << block if block_given?
  @_before_load
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
bundle() click to toggle source

Determines whether the dependencies are locked by Bundler. otherwise return nil

@return [:locked, :unlocked, nil]

Returns +:locked+ if the +Gemfile.lock+ file exists, or +:unlocked+
if only the +Gemfile+ exists.

@deprecated Will be removed in 1.0.0

# File lib/padrino-core.rb, line 125
def bundle
  return :locked   if File.exist?(root('Gemfile.lock'))
  return :unlocked if File.exist?(root("Gemfile"))
end
called_from() click to toggle source

This adds the ablity to instantiate {Padrino.load!} after {Padrino::Application} definition.

# File lib/padrino-core/loader.rb, line 106
def called_from
  @_called_from || first_caller
end
clear!() click to toggle source

Clear the padrino env.

@return [NilClass]

# File lib/padrino-core/loader.rb, line 81
def clear!
  Padrino.clear_middleware!
  Padrino.mounted_apps.clear
  @_load_paths = nil
  @_dependency_paths = nil
  @_global_configuration = nil
  Padrino.before_load.clear
  Padrino.after_load.clear
  Padrino::Reloader.clear!
  Thread.current[:padrino_loaded] = nil
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 82
def configure_apps(&block)
  @_global_configuration = block if block_given?
end
dependency_paths() click to toggle source

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

@return [Array<String>]

The dependencey paths.

@example

Padrino.dependency_paths << "#{Padrino.root}/uploaders/*.rb"
# File lib/padrino-core/loader.rb, line 191
def dependency_paths
  @_dependency_paths_was = [
    "#{root}/config/database.rb", "#{root}/lib/**/*.rb", "#{root}/shared/lib/**/*.rb",
    "#{root}/models/**/*.rb", "#{root}/shared/models/**/*.rb", "#{root}/config/apps.rb"
  ]
  @_dependency_paths ||= @_dependency_paths_was
end
env() click to toggle source

Helper method that return {PADRINO_ENV}.

@return [Symbol]

The Padrino Environment.
# File lib/padrino-core.rb, line 41
def env
  @_env ||= PADRINO_ENV.to_s.downcase.to_sym
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 208
def insert_mounted_app(mounter)
  Padrino.mounted_apps.push(mounter) unless Padrino.mounted_apps.include?(mounter)
end
load!() click to toggle source

Requires necessary dependencies as well as application files from root lib and models.

@return [Boolean]

returns true if Padrino is not already bootstraped otherwise else.
# File lib/padrino-core/loader.rb, line 61
def load!
  return false if loaded?
  @_called_from = first_caller
  Padrino.set_encoding
  Padrino.set_load_paths(*load_paths) # We set the padrino load paths
  Padrino::Logger.setup! # Initialize our logger
  Padrino.require_dependencies("#{root}/config/database.rb", :nodeps => true) # Be sure to don't remove constants from dbs.
  Padrino::Reloader.lock! # Now we can remove constant from here to down
  Padrino.before_load.each(&:call) # Run before hooks
  Padrino.dependency_paths.each { |path| Padrino.require_dependencies(path) }
  Padrino.after_load.each(&:call) # Run after hooks
  Padrino::Reloader.run!
  Thread.current[:padrino_loaded] = true
end
load_paths() click to toggle source

The used +$LOAD_PATHS+ from Padrino.

@return [Array<String>]

The load paths used by Padrino.
# File lib/padrino-core/loader.rb, line 49
def load_paths
  @_load_paths_was = %(lib models shared).map { |path| Padrino.root(path) }
  @_load_paths ||= @_load_paths_was
end
loaded?() click to toggle source

Determines whether Padrino was loaded with {Padrino.load!}.

@return [Boolean]

Specifies whether Padrino was loaded.
# File lib/padrino-core/loader.rb, line 116
def loaded?
  Thread.current[:padrino_loaded]
end
logger() click to toggle source

@return [Padrino::Logger]

@example

logger.debug "foo"
logger.warn "bar"
# File lib/padrino-core/logger.rb, line 15
def self.logger
  Padrino::Logger.setup! if Thread.current[:padrino_logger].nil?
  Thread.current[:padrino_logger]
end
logger=(value) click to toggle source

Set the padrino logger

@param [Object] value

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

@return [Object]

the given value

@example using ruby default logger

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

@example using ActiveSupport

require 'active_support/buffered_logger'
Padrino.logger = Buffered.new(STDOUT)
# File lib/padrino-core/logger.rb, line 37
def self.logger=(value)
  Thread.current[:padrino_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
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 220
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 199
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 191
def mounted_root(*args)
  Padrino.root(@mounted_root ||= "", *args)
end
reload!() click to toggle source

Method for reloading required applications and their files.

# File lib/padrino-core/loader.rb, line 96
def reload!
  Padrino.before_load.each(&:call) # Run before hooks
  Padrino::Reloader.reload! # detects the modified files
  Padrino.after_load.each(&:call) # Run after hooks
end
require_dependencies(*paths) click to toggle source

Attempts to require all dependency libs that we need. If you use this method we can perform correctly a Padrino.reload! Another good thing that this method are dependency check, for example:

# models
#  \-- a.rb => require something of b.rb
#  \-- b.rb

In the example above if we do:

Dir["/models/*.rb"].each { |r| require r }

we get an error, because we try to require first a.rb that need something of b.rb.

With this method we don’t have this problem.

@param [Array<String>] paths

The paths to require.

@example For require all our app libs we need to do:

require_dependencies("#{Padrino.root}/lib/**/*.rb")
# File lib/padrino-core/loader.rb, line 144
def require_dependencies(*paths)
  options = paths.extract_options!

  # Extract all files to load
  files = paths.flatten.map { |path| Dir[path] }.flatten.uniq.sort

  while files.present?
    # List of errors and failed files
    errors, failed = [], []

    # We need a size to make sure things are loading
    size_at_start = files.size

    # Now we try to require our dependencies, we dup files
    # so we don't perform delete on the original array during
    # iteration, this prevent problems with rubinus
    files.dup.each do |file|
      begin
        Padrino::Reloader.safe_load(file, options.dup)
        files.delete(file)
      rescue LoadError => e
        errors << e
        failed << file
      rescue NameError => e
        errors << e
        failed << file
      rescue Exception => e
        raise e
      end
    end

    # Stop processing if nothing loads or if everything has loaded
    raise errors.last if files.size == size_at_start && files.present?
    break if files.empty?
  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 31
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.*/, '"\&"')
    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: "localhost", port: "3000", adapter: the first found
Padrino.run!("localhost", "4000", "mongrel") # use => host: "0.0.0.0", port: "3000", adapter: "mongrel"
# File lib/padrino-core/server.rb, line 10
def self.run!(options={})
  Padrino.load!
  Server.start(Padrino.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 105
def set_encoding
  if RUBY_VERSION < '1.9'
    $KCODE='u'
  else
    Encoding.default_external = Encoding::UTF_8
    Encoding.default_internal = Encoding::UTF_8
  end
  nil
end
set_load_paths(*paths) click to toggle source

Concat to +$LOAD_PATH+ the given paths.

@param [Array<String>] paths

The paths to concat.
# File lib/padrino-core/loader.rb, line 205
def set_load_paths(*paths)
  $:.concat(paths); load_paths.concat(paths)
  $:.uniq!; load_paths.uniq!
end
use(m, *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(m, *args, &block)
  middleware << [m, 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.