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.
List of callers in a Padrino application that should be ignored as part of a stack trace.
The version constant for the current version of Padrino.
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
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 65 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
Returns project-wide configuration settings defined in {configure_apps} block.
# File lib/padrino-core.rb, line 108 def apps_configuration @_global_configuration end
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
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
This adds the ablity to instantiate {Padrino.load!} after {Padrino::Application} definition.
# File lib/padrino-core/loader.rb, line 111 def called_from @_called_from || first_caller end
Clear the padrino env.
@return [NilClass]
# File lib/padrino-core/loader.rb, line 85 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
Clears all previously configured middlewares.
@return [Array]
An empty array
# File lib/padrino-core.rb, line 150 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 93 def configure_apps(&block) return unless block_given? @@_global_configurations ||= [] @@_global_configurations << block @_global_configuration = lambda do |app| @@_global_configurations.each do |configuration| app.class_eval(&configuration) end end end
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 194 def dependency_paths @_dependency_paths ||= (dependency_paths_was + Array(module_paths)) end
Helper method that return {PADRINO_ENV}.
@return [Symbol]
The Padrino Environment.
# File lib/padrino-core.rb, line 52 def env @_env ||= PADRINO_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 183 def gem(name, main_module) _,spec = Gem.loaded_specs.find { |spec_name, spec| spec_name == name } gems << spec modules << main_module spec.full_gem_path end
Returns all currently known padrino gems.
@returns [Gem::Specification]
# File lib/padrino-core.rb, line 194 def gems @gems ||= [] end
Inserts a Mounter object into the mounted applications (avoids duplicates)
@param [Padrino::Mounter] mounter
# File lib/padrino-core/mounter.rb, line 218 def insert_mounted_app(mounter) Padrino.mounted_apps.push(mounter) unless Padrino.mounted_apps.include?(mounter) end
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? t = Time.now @_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 Padrino.logger.devel "Loaded Padrino in #{Time.now - t} seconds" end
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
Determines whether Padrino was loaded with {Padrino.load!}.
@return [Boolean]
Specifies whether Padrino was loaded.
# File lib/padrino-core/loader.rb, line 121 def loaded? Thread.current[:padrino_loaded] end
@return [Padrino::Logger]
@example
logger.debug "foo" logger.warn "bar"
# File lib/padrino-core/logger.rb, line 15 def self.logger Padrino::Logger.logger end
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 36 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 140 def middleware @middleware ||= [] end
All loaded Padrino modules.
@returns [<Padrino::Module>]
# File lib/padrino-core.rb, line 202 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 230 def mount(name, options={}) Mounter.new(name, options) end
@return [Array]
the mounted padrino applications (MountedApp objects)
# File lib/padrino-core/mounter.rb, line 209 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 201 def mounted_root(*args) Padrino.root(@mounted_root ||= "", *args) end
Method for reloading required applications and their files.
# File lib/padrino-core/loader.rb, line 100 def reload! return unless Padrino::Reloader.changed? Padrino.before_load.each(&:call) # Run before hooks Padrino::Reloader.reload! # detects the modified files Padrino.after_load.each(&:call) # Run after hooks end
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 149 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 NameError, LoadError => e Padrino.logger.devel "Problem while loading #{file}: #{e.to_s}" 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
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 42 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.*/, '"\&"') 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(Padrino.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 123 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
Concat to +$LOAD_PATH+ the given paths.
@param [Array<String>] paths
The paths to concat.
# File lib/padrino-core/loader.rb, line 204 def set_load_paths(*paths) $:.concat(paths); load_paths.concat(paths) $:.uniq!; load_paths.uniq! 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 166 def use(m, *args, &block) middleware << [m, 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
Generated with the Darkfish Rdoc Generator 2.