module Tennpipes
Manages current Tennpipes
version for use in gem generation.
We put this in a separate file so you can get tennpipes version without include full tennpipes core.
Constants
Attributes
Public Class Methods
Creates Rack stack with the router added to the middleware chain.
# File lib/tennpipes-base.rb, line 120 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 [Tennpipes::Router]
The router for the application.
@raise [ApplicationLoadError]
No applications were mounted.
# File lib/tennpipes-base.rb, line 68 def application warn 'WARNING! No apps are mounted. Please, mount apps in `config/apps.rb`' unless Tennpipes.mounted_apps.present? router = Tennpipes::Router.new Tennpipes.mounted_apps.each { |app| app.map_onto(router) } middleware.present? ? add_middleware(router) : router end
This method return the correct location of tennpipes bin or exec it using Kernel#system with the given args.
@param [Array] args
command or commands to execute
@return [Boolean]
@example
Tennpipes.bin('start', '-e production')
# File lib/tennpipes-base/command.rb, line 16 def self.bin(*args) @_tennpipes_bin ||= [self.ruby_command, File.expand_path("../../../bin/tennpipes", __FILE__)] args.empty? ? @_tennpipes_bin : system(args.unshift(@_tennpipes_bin).join(" ")) end
Clears all previously configured middlewares.
@return [Array]
An empty array
# File lib/tennpipes-base.rb, line 144 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
Tennpipes.configure_apps do enable :sessions disable :raise_errors end
# File lib/tennpipes-base.rb, line 88 def configure_apps(&block) return unless block_given? global_configurations << block end
Helper method that return {RACK_ENV}.
@return [Symbol]
The Tennpipes Environment.
# File lib/tennpipes-base.rb, line 55 def env @_env ||= RACK_ENV.to_s.downcase.to_sym end
Registers a gem with tennpipes. This relieves the caller from setting up loadpaths by itself and enables Tennpipes
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/tennpipes-base.rb, line 177 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/tennpipes-base.rb, line 186 def gems @gems ||= [] end
Stores global configuration blocks.
# File lib/tennpipes-base.rb, line 96 def global_configurations @_global_configurations ||= [] end
Inserts a Mounter
object into the mounted applications (avoids duplicates).
@param [Tennpipes::Mounter] mounter
# File lib/tennpipes-base/mounter.rb, line 292 def insert_mounted_app(mounter) Tennpipes.mounted_apps.push(mounter) unless Tennpipes.mounted_apps.include?(mounter) end
@return [Tennpipes::Logger]
@example
logger.debug "foo" logger.warn "bar"
# File lib/tennpipes-base/logger.rb, line 17 def self.logger Tennpipes::Logger.logger end
Set the tennpipes 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' Tennpipes.logger = Logger.new(STDOUT)
@example using ActiveSupport
require 'active_support/buffered_logger' Tennpipes.logger = Buffered.new(STDOUT)
# File lib/tennpipes-base/logger.rb, line 38 def self.logger=(value) Tennpipes::Logger.logger = value end
A Rack::Builder object that allows to add middlewares in front of all Tennpipes
applications.
@return [Array<Array<Class, Array, Proc>>]
The middleware classes.
# File lib/tennpipes-base.rb, line 134 def middleware @middleware ||= [] end
@returns [<Tennpipes::Module>]
# File lib/tennpipes-base.rb, line 192 def modules @modules ||= [] end
Mounts a new sub-application onto Tennpipes
project.
@see Tennpipes::Mounter#new
@example
Tennpipes.mount("blog_app").to("/blog")
# File lib/tennpipes-base/mounter.rb, line 304 def mount(name, options={}) Mounter.new(name, options) end
@return [Array]
the mounted tennpipes applications (MountedApp objects)
# File lib/tennpipes-base/mounter.rb, line 283 def mounted_apps @mounted_apps ||= [] end
@param [Array] args
@return [String]
the root to the mounted apps base directory.
# File lib/tennpipes-base/mounter.rb, line 275 def mounted_root(*args) Tennpipes.root(@mounted_root ||= "", *args) end
Helper method for file references.
@param [Array<String>] args
The directories to join to {TENNPIPES_ROOT}.
@return [String]
The absolute path.
@example
# Referencing a file in config called settings.yml Tennpipes.root("config", "settings.yml") # returns TENNPIPES_ROOT + "/config/setting.yml"
# File lib/tennpipes-base.rb, line 45 def root(*args) File.expand_path(File.join(TENNPIPES_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/tennpipes-base/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 Tennpipes
apps as a self-hosted server using: thin, mongrel, or WEBrick in that order.
@example
Tennpipes.run! # with these defaults => host: "127.0.0.1", port: "3000", adapter: the first found Tennpipes.run!("0.0.0.0", "4000", "mongrel") # use => host: "0.0.0.0", port: "4000", adapter: "mongrel"
# File lib/tennpipes-base/server.rb, line 10 def self.run!(options={}) Tennpipes.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/tennpipes-base.rb, line 111 def set_encoding Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 nil end
Convenience method for adding a Middleware to the whole tennpipes 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/tennpipes-base.rb, line 160 def use(mw, *args, &block) middleware << [mw, args, block] end
The current Tennpipes
version.
@return [String]
The version number.
# File lib/tennpipes-base/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/tennpipes-base/caller.rb, line 47 def self.caller_files caller(1). map { |line| line.split(/:(?=\d|in )/)[0,2] }. reject { |file,line| TENNPIPES_IGNORE_CALLERS.any? { |pattern| file =~ pattern } }. map { |file,line| file } end
# File lib/tennpipes-base/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 [Tennpipes.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/tennpipes-base/caller.rb, line 36 def self.first_caller caller_files.first end