Module: Padrino

Defined in:
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/version.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/tasks.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/caller.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/loader.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/logger.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/module.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/router.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/server.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/command.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/mounter.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/cli/base.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/reloader.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/cli/adapter.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/support_lite.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/flash.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/tag_helpers.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/number_helpers.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/render_helpers.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/rendering.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/breadcrumb_helpers.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/translation_helpers.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/showexceptions.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/erb_handler.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/haml_handler.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/slim_handler.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/abstract_handler.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/standard_form_builder.rb,
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb

Overview

Make slim works with sinatra/padrino

Defined Under Namespace

Modules: Cli, Flash, Helpers, Module, Reloader, Rendering, Routing, Tasks Classes: Application, ApplicationLoadError, ApplicationSetupError, Filter, Logger, Mounter, Router, Server, ShowExceptions, Utils

Constant Summary collapse

PADRINO_IGNORE_CALLERS =

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

[
  %r{lib/padrino-.*$},                             # all padrino code
  %r{/padrino-.*/(lib|bin)},                       # all padrino code
  %r{/bin/padrino$},                               # all padrino code
  %r{/sinatra(/(base|main|showexceptions))?\.rb$}, # all sinatra code
  %r{lib/tilt.*\.rb$},                             # all tilt code
  %r{lib/rack.*\.rb$},                             # all rack code
  %r{lib/mongrel.*\.rb$},                          # all mongrel code
  %r{lib/shotgun.*\.rb$},                          # all shotgun lib
  %r{bin/shotgun$},                                # shotgun binary
  %r{\(.*\)},                                      # generated code
  %r{shoulda/context\.rb$},                        # shoulda hacks
  %r{mocha/integration},                           # mocha hacks
  %r{test/unit},                                   # test unit hacks
  %r{rake_test_loader\.rb},                        # rake hacks
  %r{custom_require\.rb$},                         # rubygems require hacks
  %r{active_support},                              # active_support require hacks
  %r{/thor}                                        # thor require hacks
]
VERSION =

The version constant for the current version of Padrino.

'0.11.2'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.mounted_root(*args) ⇒ String

Returns the root to the mounted apps base directory.

Parameters:

  • args (Array)

Returns:

  • (String)

    the root to the mounted apps base directory



201
202
203
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/mounter.rb', line 201

def mounted_root(*args)
  Padrino.root(@mounted_root ||= "", *args)
end

Class Method Details

.after_load { ... } ⇒ Array<Proc>

Hooks to be called after a load/reload.

Examples:

after_load do
  DataMapper.finalize
end

Yields:

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

Returns:

  • (Array<Proc>)

    The load/reload hooks.



37
38
39
40
41
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/loader.rb', line 37

def after_load(&block)
  @_after_load ||= []
  @_after_load << block if block_given?
  @_after_load
end

.applicationPadrino::Router

The resulting rack builder mapping each ‘mounted’ application.

Returns:

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.apps_configurationObject

Returns project-wide configuration settings defined in configure_apps block.



108
109
110
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb', line 108

def apps_configuration
  @_global_configuration
end

.before_load { ... } ⇒ Array<Proc>

Hooks to be called before a load/reload.

Examples:

before_load do
  pre_initialize_something
end

Yields:

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

Returns:

  • (Array<Proc>)

    The load/reload before hooks.



17
18
19
20
21
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/loader.rb', line 17

def before_load(&block)
  @_before_load ||= []
  @_before_load << block if block_given?
  @_before_load
end

.bin(*args) ⇒ Boolean

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

Examples:

Padrino.bin('start', '-e production')

Parameters:

  • args (Array)

    command or commands to execute

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.called_fromObject

This adds the ablity to instantiate load! after Application definition.



111
112
113
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/loader.rb', line 111

def called_from
  @_called_from || first_caller
end

.clear!NilClass

Clear the padrino env.

Returns:

  • (NilClass)


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.clear_middleware!Array

Clears all previously configured middlewares.

Returns:

  • (Array)

    An empty array



150
151
152
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb', line 150

def clear_middleware!
  @middleware = []
end

.configure_apps { ... } ⇒ Object

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

Examples:

Padrino.configure_apps do
  enable  :sessions
  disable :raise_errors
end

Yields:

  • The given block will be called to configure each application.



93
94
95
96
97
98
99
100
101
102
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.dependency_pathsArray<String>

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

Examples:

Padrino.dependency_paths << "#{Padrino.root}/uploaders/*.rb"

Returns:

  • (Array<String>)

    The dependencey paths.



194
195
196
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/loader.rb', line 194

def dependency_paths
  @_dependency_paths ||= (dependency_paths_was + Array(module_paths))
end

.envSymbol

Helper method that return PADRINO_ENV.

Returns:

  • (Symbol)

    The Padrino Environment.



52
53
54
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb', line 52

def env
  @_env ||= PADRINO_ENV.to_s.downcase.to_sym
end

.gem(name, main_module) ⇒ Object

Registers a gem with padrino. This relieves the caller from setting up loadpaths by himself 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.

Parameters:

  • name (String)

    The name of the gem being registered.

  • main_module (Module)

    The main module of the gem.



183
184
185
186
187
188
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.gemsObject

Returns all currently known padrino gems.



194
195
196
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb', line 194

def gems
  @gems ||= []
end

.insert_mounted_app(mounter) ⇒ Object

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

Parameters:



218
219
220
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/mounter.rb', line 218

def insert_mounted_app(mounter)
  Padrino.mounted_apps.push(mounter) unless Padrino.mounted_apps.include?(mounter)
end

.load!Boolean

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

Returns:

  • (Boolean)

    returns true if Padrino is not already bootstraped otherwise else.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.load_pathsArray<String>

The used $LOAD_PATHS from Padrino.

Returns:

  • (Array<String>)

    The load paths used by Padrino.



49
50
51
52
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/loader.rb', line 49

def load_paths
  @_load_paths_was = %w(lib models shared).map { |path| Padrino.root(path) }
  @_load_paths ||= @_load_paths_was
end

.loaded?Boolean

Determines whether Padrino was loaded with load!.

Returns:

  • (Boolean)

    Specifies whether Padrino was loaded.



121
122
123
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/loader.rb', line 121

def loaded?
  Thread.current[:padrino_loaded]
end

.loggerPadrino::Logger

Examples:

logger.debug "foo"
logger.warn "bar"

Returns:



15
16
17
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/logger.rb', line 15

def self.logger
  Padrino::Logger.logger
end

.logger=(value) ⇒ Object

Set the padrino logger

Examples:

using ruby default logger

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

using ActiveSupport

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

Parameters:

  • value (Object)

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

Returns:

  • (Object)

    the given value



36
37
38
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/logger.rb', line 36

def self.logger=(value)
  Padrino::Logger.logger = value
end

.middlewareArray<Array<Class, Array, Proc>>

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

Returns:

  • (Array<Array<Class, Array, Proc>>)

    The middleware classes.



140
141
142
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb', line 140

def middleware
  @middleware ||= []
end

.modulesObject

All loaded Padrino modules.



202
203
204
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb', line 202

def modules
  @modules ||= []
end

.mount(name, options = {}) ⇒ Object

Mounts a new sub-application onto Padrino project

Examples:

Padrino.mount("blog_app").to("/blog")

See Also:

  • Padrino::Mounter#new


230
231
232
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/mounter.rb', line 230

def mount(name, options={})
  Mounter.new(name, options)
end

.mounted_appsArray

Returns the mounted padrino applications (MountedApp objects).

Returns:

  • (Array)

    the mounted padrino applications (MountedApp objects)



209
210
211
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/mounter.rb', line 209

def mounted_apps
  @mounted_apps ||= []
end

.reload!Object

Method for reloading required applications and their files.



100
101
102
103
104
105
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.require_dependencies(*paths) ⇒ Object

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.

Examples:

For require all our app libs we need to do:

require_dependencies("#{Padrino.root}/lib/**/*.rb")

Parameters:

  • paths (Array<String>)

    The paths to require.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.root(*args) ⇒ String

Helper method for file references.

Examples:

# Referencing a file in config called settings.yml
Padrino.root("config", "settings.yml")
# returns PADRINO_ROOT + "/config/setting.yml"

Parameters:

Returns:

  • (String)

    The absolute path.



42
43
44
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb', line 42

def root(*args)
  File.expand_path(File.join(PADRINO_ROOT, *args))
end

.ruby_commandString

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

Returns:

  • (String)

    path to ruby bin executable



28
29
30
31
32
33
34
35
36
37
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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 = {}) ⇒ Object

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

Examples:

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"


10
11
12
13
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/server.rb', line 10

def self.run!(options={})
  Padrino.load!
  Server.start(Padrino.application, options)
end

.set_encodingNilClass

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.

Returns:

  • (NilClass)

See Also:



123
124
125
126
127
128
129
130
131
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

.set_load_paths(*paths) ⇒ Object

Concat to $LOAD_PATH the given paths.

Parameters:

  • paths (Array<String>)

    The paths to concat.



204
205
206
207
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/loader.rb', line 204

def set_load_paths(*paths)
  $:.concat(paths); load_paths.concat(paths)
  $:.uniq!; load_paths.uniq!
end

.use(m, *args) { ... } ⇒ Object

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

Parameters:

  • m (Class)

    The middleware class.

  • args (Array)

    The arguments for the middleware.

Yields:

  • The given block will be passed to the initialized middleware.



166
167
168
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core.rb', line 166

def use(m, *args, &block)
  middleware << [m, args, block]
end

.versionString

The current Padrino version.

Returns:

  • (String)

    The version number.



17
18
19
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/version.rb', line 17

def self.version
  VERSION
end

Instance Method Details

#RUBY_IGNORE_CALLERSObject

Add rubinius (and hopefully other VM implementations) ignore patterns …



27
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/caller.rb', line 27

PADRINO_IGNORE_CALLERS.concat(RUBY_IGNORE_CALLERS)