Class: Padrino::Mounter

### Home Classes Methods

### In Files

- lib/padrino-core/mounter.rb

### Parent

Object

### Namespace

- CLASS Padrino::Mounter::MounterException

### Methods

- ::new
- #==
- #app_constant
- #ensure_app_file!
- #ensure_app_object!
- #host
- #locate_app_file
- #locate_app_object
- #map_onto
- #named_routes
- #routes
- #to

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Application
- Padrino::ApplicationLoadError
- Padrino::ApplicationSetupError
- Padrino::Cli
- Padrino::Cli::Adapter
- Padrino::Cli::Base
- Padrino::Erubis
- Padrino::Erubis::SafeBufferEnhancer
- Padrino::Erubis::SafeBufferTemplate
- Padrino::Erubis::Template
- Padrino::Filter
- Padrino::Flash
- Padrino::Flash::Helpers
- Padrino::Flash::Storage
- Padrino::Logger
- Padrino::Logger::Colorize
- Padrino::Logger::Extensions
- Padrino::Logger::Rack
- Padrino::Module
- Padrino::Mounter
- Padrino::Mounter::MounterException
- Padrino::Reloader
- Padrino::Reloader::Rack
- Padrino::Rendering
- Padrino::Rendering::ClassMethods
- Padrino::Rendering::InstanceMethods
- Padrino::Rendering::SafeTemplate
- Padrino::Rendering::TemplateNotFound
- Padrino::Router
- Padrino::Routing
- Padrino::Routing::ClassMethods
- Padrino::Routing::InstanceMethods
- Padrino::Routing::Parent
- Padrino::Routing::UnrecognizedException
- Padrino::Server
- Padrino::ShowExceptions
- Padrino::Tasks
- Padrino::Utils
- HttpRouter
- HttpRouter::Node
- HttpRouter::Node::Path
- HttpRouter::Request
- HttpRouter::Route
- Haml
- Haml::Helpers
- Haml::Util
- Sinatra
- Sinatra::Request
- Slim
- Slim::Template
- Tilt
- Tilt::HamlTemplate
- FileSet
- Kernel
- Module
- Object
- ObjectSpace
- PadrinoTasks
- String
- SupportLite

No matching classes.

# Padrino::Mounter

Represents a particular mounted padrino application Stores the name of the application (app folder name) and url mount path

@example

    Mounter.new("blog_app", :app_class => "Blog").to("/blog")
    Mounter.new("blog_app", :app_file => "/path/to/blog/app.rb").to("/blog")

### Attributes

app_class[RW]
app_file[RW]
app_host[RW]
app_obj[RW]
app_root[RW]
name[RW]
uri_root[RW]

### Public Class Methods

new(name, options={}) click to toggle source

@param [String, Padrino::Application] name

    The app name or the {Padrino::Application} class

@param [Hash] options @option options [Symbol] :app_class (Detected from name) @option options [Symbol] :app_file (Automatically detected) @option options [Symbol] :app_obj (Detected) @option options [Symbol] :app_root (Directory of :app_file) @option options [Symbol] :gem The gem to load the app from (Detected from name)

    # File lib/padrino-core/mounter.rb, line 27
    def initialize(name, options={})
      @name      = name.to_s
      @app_class = options[:app_class] || @name.camelize
      @gem       = options[:gem]       || @app_class.split("::").first.underscore
      @app_file  = options[:app_file]  || locate_app_file
      @app_obj   = options[:app_obj]   || app_constant || locate_app_object
      ensure_app_file! || ensure_app_object!
      @app_root  = options[:app_root]  || File.dirname(@app_file)
      @uri_root  = "/"
      Padrino::Reloader.exclude_constants << @app_class
    end

### Public Instance Methods

==(other) click to toggle source

Makes two Mounters equal if they have the same name and uri_root

@param [Padrino::Mounter] other

    # File lib/padrino-core/mounter.rb, line 123
    def ==(other)
      other.is_a?(Mounter) && self.app_class == other.app_class && self.uri_root == other.uri_root
    end

app_constant() click to toggle source

@return [Padrino::Application]

    the class object for the app if defined, nil otherwise

    # File lib/padrino-core/mounter.rb, line 131
    def app_constant
      klass = Object
      for piece in app_class.split("::")
        piece = piece.to_sym
        if klass.const_defined?(piece)
          klass = klass.const_get(piece)
        else
          return
        end
      end
      klass
    end

host(mount_host) click to toggle source

Registers the mounted application onto Padrino for the given host

@param [String] mount_host

    Host name

@example

    Mounter.new("blog_app").to("/blog").host("blog.padrino.org")
    Mounter.new("blog_app").host("blog.padrino.org")
    Mounter.new("catch_all").host(/.*\.padrino.org/)

    # File lib/padrino-core/mounter.rb, line 65
    def host(mount_host)
      @app_host = mount_host
      Padrino.insert_mounted_app(self)
      self
    end

map_onto(router) click to toggle source

Maps Padrino application onto a Padrino::Router For use in constructing a Rack application

@param [Padrino::Router]

@return [Padrino::Router]

@example

    @app.map_onto(router)

    # File lib/padrino-core/mounter.rb, line 82
    def map_onto(router)
      app_data, app_obj = self, @app_obj
      app_obj.set :uri_root,       app_data.uri_root
      app_obj.set :app_name,       app_data.name
      app_obj.set :app_file,       app_data.app_file unless ::File.exist?(app_obj.app_file)
      app_obj.set :root,           app_data.app_root unless app_data.app_root.blank?
      app_obj.set :public_folder,  Padrino.root('public', app_data.uri_root) unless File.exists?(app_obj.public_folder)
      app_obj.set :static,         File.exist?(app_obj.public_folder) if app_obj.nil?
      app_obj.setup_application! # Initializes the app here with above settings.
      router.map(:to => app_obj, :path => app_data.uri_root, :host => app_data.app_host)
    end

named_routes() click to toggle source

Returns the basic route information for each named route

@return [Array]

    Array of routes

    # File lib/padrino-core/mounter.rb, line 107
    def named_routes
      app_obj.routes.map { |route|
        name_array     = "(#{route.name.to_s.split("_").map { |piece| %Q[:#{piece}] }.join(", ")})"
        request_method = route.request_methods.first
        next if route.name.blank? || request_method == 'HEAD'
        original_path = route.original_path.is_a?(Regexp) ? route.original_path.inspect : route.original_path
        full_path = File.join(uri_root, original_path)
        OpenStruct.new(:verb => request_method, :identifier => route.name, :name => name_array, :path => full_path)
      }.compact
    end

routes() click to toggle source

Returns the route objects for the mounted application

    # File lib/padrino-core/mounter.rb, line 97
    def routes
      app_obj.routes
    end

to(mount_url) click to toggle source

Registers the mounted application onto Padrino

@param [String] mount_url

    Path where we mount the app

@example

    Mounter.new("blog_app").to("/blog")

    # File lib/padrino-core/mounter.rb, line 48
    def to(mount_url)
      @uri_root  = mount_url
      Padrino.insert_mounted_app(self)
      self
    end

### Protected Instance Methods

ensure_app_file!() click to toggle source

Raises an exception unless app_file is located properly

    # File lib/padrino-core/mounter.rb, line 178
    def ensure_app_file!
      message = "Unable to locate source file for app '#{app_class}', try with :app_file => '/path/app.rb'"
      raise MounterException, message unless @app_file
    end

ensure_app_object!() click to toggle source

Raises an exception unless app_obj is defined properly

    # File lib/padrino-core/mounter.rb, line 186
    def ensure_app_object!
      message = "Unable to locate app for '#{app_class}', try with :app_class => 'MyAppClass'"
      raise MounterException, message unless @app_obj
    end

locate_app_file() click to toggle source

Returns the determined location of the mounted application main file

    # File lib/padrino-core/mounter.rb, line 159
    def locate_app_file
      candidates  = []
      candidates << app_constant.app_file if app_constant.respond_to?(:app_file) && File.exist?(app_constant.app_file.to_s)
      candidates << Padrino.first_caller if File.identical?(Padrino.first_caller.to_s, Padrino.called_from.to_s)
      candidates << Padrino.mounted_root(name.downcase, "app.rb")
      simple_name = name.split("::").last.downcase
      mod_name = name.split("::")[0..-2].join("::")
      Padrino.modules.each do |mod|
        if mod.name == mod_name
          candidates << mod.root(simple_name, "app.rb")
        end
      end
      candidates << Padrino.root("app", "app.rb")
      candidates.find { |candidate| File.exist?(candidate) }
    end

locate_app_object() click to toggle source

Locates and requires the file to load the app constant

    # File lib/padrino-core/mounter.rb, line 148
    def locate_app_object
      @_app_object ||= begin
        ensure_app_file!
        Padrino.require_dependencies(app_file)
        app_constant
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.