Class: Padrino::Mounter

Inherits:
Object
  • Object
show all
Defined in:
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/mounter.rb

Overview

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

Examples:

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

Defined Under Namespace

Classes: MounterException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Mounter

Returns a new instance of Mounter.

Parameters:

Options Hash (options):

  • :app_class (Symbol) — default: Detected from name
  • :app_file (Symbol) — default: Automatically detected
  • :app_obj (Symbol) — default: Detected
  • :app_root (Symbol) — default: Directory of :app_file
  • :gem (Symbol)

    The gem to load the app from (Detected from name)



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

Instance Attribute Details

#app_classObject

Returns the value of attribute app_class.



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

def app_class
  @app_class
end

#app_fileObject

Returns the value of attribute app_file.



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

def app_file
  @app_file
end

#app_hostObject

Returns the value of attribute app_host.



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

def app_host
  @app_host
end

#app_objObject

Returns the value of attribute app_obj.



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

def app_obj
  @app_obj
end

#app_rootObject

Returns the value of attribute app_root.



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

def app_root
  @app_root
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#uri_rootObject

Returns the value of attribute uri_root.



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

def uri_root
  @uri_root
end

Instance Method Details

#==(other) ⇒ Object

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

Parameters:



123
124
125
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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_constantPadrino::Application

Returns the class object for the app if defined, nil otherwise.

Returns:



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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) ⇒ Object

Registers the mounted application onto Padrino for the given host

Examples:

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/)

Parameters:

  • mount_host (String)

    Host name



65
66
67
68
69
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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) ⇒ Padrino::Router

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

Examples:

@app.map_onto(router)

Parameters:

Returns:



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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_routesArray

Returns the basic route information for each named route

Returns:

  • (Array)

    Array of routes



107
108
109
110
111
112
113
114
115
116
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/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

#routesObject

Returns the route objects for the mounted application



97
98
99
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/mounter.rb', line 97

def routes
  app_obj.routes
end

#to(mount_url) ⇒ Object

Registers the mounted application onto Padrino

Examples:

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

Parameters:

  • mount_url (String)

    Path where we mount the app



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

def to(mount_url)
  @uri_root  = mount_url
  Padrino.insert_mounted_app(self)
  self
end