Class: Padrino::Mounter
- Inherits:
-
Object
- Object
- Padrino::Mounter
- Defined in:
- lib/padrino-core/mounter.rb,
lib/padrino-core/mounter/application_extension.rb
Overview
Represents a particular mounted Padrino application. Stores the name of the application (app folder name) and url mount path.
Defined Under Namespace
Modules: ApplicationExtension Classes: MounterException
Constant Summary collapse
- DEFAULT_CASCADE =
[404, 405]
Instance Attribute Summary collapse
-
#app_class ⇒ Object
Returns the value of attribute app_class.
-
#app_file ⇒ Object
Returns the value of attribute app_file.
-
#app_host ⇒ Object
Returns the value of attribute app_host.
-
#app_obj ⇒ Object
Returns the value of attribute app_obj.
-
#app_root ⇒ Object
Returns the value of attribute app_root.
-
#cascade ⇒ Object
Returns the value of attribute cascade.
-
#name ⇒ Object
Returns the value of attribute name.
-
#uri_root ⇒ Object
Returns the value of attribute uri_root.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Makes two Mounters equal if they have the same name and uri_root.
-
#app_constant ⇒ Padrino::Application
The class object for the app if defined, nil otherwise.
-
#ensure_app_file! ⇒ Object
protected
Raises an exception unless app_file is located properly.
-
#ensure_app_object! ⇒ Object
protected
Raises an exception unless app_obj is defined properly.
-
#host(mount_host) ⇒ Object
Registers the mounted application onto Padrino for the given host.
-
#initialize(name, options = {}) ⇒ Mounter
constructor
A new instance of Mounter.
-
#locate_app_file ⇒ Object
protected
Returns the determined location of the mounted application main file.
-
#locate_app_object ⇒ Object
protected
Locates and requires the file to load the app constant.
-
#map_onto(router) ⇒ Padrino::Router
Maps Padrino application onto a Padrino::Router.
-
#named_routes ⇒ Array
Returns the basic route information for each named route.
- #padrino_application? ⇒ Boolean
-
#routes ⇒ Object
Returns the route objects for the mounted application.
-
#to(mount_url) ⇒ Object
Registers the mounted application onto Padrino.
Constructor Details
#initialize(name, options = {}) ⇒ Mounter
Returns a new instance of Mounter.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/padrino-core/mounter.rb', line 30 def initialize(name, = {}) @name = name.to_s @app_class = [:app_class] || Inflections.camelize(@name) @gem = [:gem] || Inflections.underscore(@app_class.split('::').first) @app_file = [:app_file] || locate_app_file @app_obj = [:app_obj] || app_constant || locate_app_object ensure_app_file! || ensure_app_object! unless padrino_application? @app_obj.extend ApplicationExtension @app_obj. = end @app_root = [:app_root] || (@app_obj.respond_to?(:root) && @app_obj.root || File.dirname(@app_file)) @uri_root = '/' @cascade = DEFAULT_CASCADE.dup if [:cascade] == true @cascade ||= [:cascade] ? Array([:cascade]) : [] Padrino::Reloader.exclude_constants << @app_class end |
Instance Attribute Details
#app_class ⇒ Object
Returns the value of attribute app_class.
17 18 19 |
# File 'lib/padrino-core/mounter.rb', line 17 def app_class @app_class end |
#app_file ⇒ Object
Returns the value of attribute app_file.
17 18 19 |
# File 'lib/padrino-core/mounter.rb', line 17 def app_file @app_file end |
#app_host ⇒ Object
Returns the value of attribute app_host.
17 18 19 |
# File 'lib/padrino-core/mounter.rb', line 17 def app_host @app_host end |
#app_obj ⇒ Object
Returns the value of attribute app_obj.
17 18 19 |
# File 'lib/padrino-core/mounter.rb', line 17 def app_obj @app_obj end |
#app_root ⇒ Object
Returns the value of attribute app_root.
17 18 19 |
# File 'lib/padrino-core/mounter.rb', line 17 def app_root @app_root end |
#cascade ⇒ Object
Returns the value of attribute cascade.
17 18 19 |
# File 'lib/padrino-core/mounter.rb', line 17 def cascade @cascade end |
#name ⇒ Object
Returns the value of attribute name.
17 18 19 |
# File 'lib/padrino-core/mounter.rb', line 17 def name @name end |
#uri_root ⇒ Object
Returns the value of attribute uri_root.
17 18 19 |
# File 'lib/padrino-core/mounter.rb', line 17 def uri_root @uri_root end |
Instance Method Details
#==(other) ⇒ Object
Makes two Mounters equal if they have the same name and uri_root.
161 162 163 |
# File 'lib/padrino-core/mounter.rb', line 161 def ==(other) other.is_a?(Mounter) && self.app_class == other.app_class && self.uri_root == other.uri_root end |
#app_constant ⇒ Padrino::Application
Returns the class object for the app if defined, nil otherwise.
169 170 171 172 173 174 175 176 |
# File 'lib/padrino-core/mounter.rb', line 169 def app_constant app_class.split('::').inject(Object) do |klass, piece| piece = piece.to_sym break unless klass.const_defined?(piece, false) klass.const_get(piece) end end |
#ensure_app_file! ⇒ Object (protected)
Raises an exception unless app_file is located properly.
213 214 215 216 |
# File 'lib/padrino-core/mounter.rb', line 213 def ensure_app_file! = "Unable to locate source file for app '#{app_class}', try with app_file: '/path/app.rb'" raise MounterException, unless @app_file end |
#ensure_app_object! ⇒ Object (protected)
Raises an exception unless app_obj is defined properly.
221 222 223 224 |
# File 'lib/padrino-core/mounter.rb', line 221 def ensure_app_object! = "Unable to locate app for '#{app_class}', try with app_class: 'MyAppClass'" raise MounterException, unless @app_obj end |
#host(mount_host) ⇒ Object
Registers the mounted application onto Padrino for the given host.
83 84 85 86 87 |
# File 'lib/padrino-core/mounter.rb', line 83 def host(mount_host) @app_host = mount_host Padrino.insert_mounted_app(self) self end |
#locate_app_file ⇒ Object (protected)
Returns the determined location of the mounted application main file.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/padrino-core/mounter.rb', line 194 def locate_app_file app_const = app_constant candidates = [] candidates << app_const.app_file if app_const.respond_to?(:app_file) 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 { |mod| candidates << mod.root(simple_name, 'app.rb') if mod.name == mod_name } candidates << Padrino.root('app', 'app.rb') candidates.find { |candidate| File.exist?(candidate) } end |
#locate_app_object ⇒ Object (protected)
Locates and requires the file to load the app constant.
183 184 185 186 187 188 189 |
# File 'lib/padrino-core/mounter.rb', line 183 def locate_app_object @_app_object ||= begin ensure_app_file! Padrino.require_dependencies(app_file) app_constant end end |
#map_onto(router) ⇒ Padrino::Router
Maps Padrino application onto a Padrino::Router. For use in constructing a Rack application.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/padrino-core/mounter.rb', line 100 def map_onto(router) app_data = self app_obj = @app_obj uri_root = app_data.uri_root public_folder_exists = File.exist?(app_obj.public_folder) if padrino_application? app_obj.set :uri_root, uri_root app_obj.set :app_name, app_obj.app_name.to_s app_obj.set :root, app_data.app_root app_obj.set :public_folder, Padrino.root('public', uri_root) unless public_folder_exists app_obj.set :static, public_folder_exists app_obj.set :cascade, app_data.cascade else app_obj.cascade = app_data.cascade app_obj.uri_root = uri_root app_obj.public_folder = Padrino.root('public', uri_root) unless public_folder_exists end app_obj.setup_application! # Initializes the app here with above settings. router.map(to: app_obj, path: uri_root, host: app_data.app_host) end |
#named_routes ⇒ Array
Returns the basic route information for each named route.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/padrino-core/mounter.rb', line 136 def named_routes return [] unless app_obj.respond_to?(:routes) app_obj.routes.map do |route| request_method = route.request_methods.first next if !route.name || request_method == 'HEAD' route_name = route.name.to_s route_name = if route.controller route_name.split(' ', 2).map { |name| ":#{name}" }.join(', ') else ":#{route_name}" end name_array = "(#{route_name})" 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) end.compact end |
#padrino_application? ⇒ Boolean
51 52 53 54 55 |
# File 'lib/padrino-core/mounter.rb', line 51 def padrino_application? @app_obj.ancestors.include?(Padrino::Application) rescue NameError false end |
#routes ⇒ Object
Returns the route objects for the mounted application.
126 127 128 |
# File 'lib/padrino-core/mounter.rb', line 126 def routes app_obj.routes end |
#to(mount_url) ⇒ Object
Registers the mounted application onto Padrino.
66 67 68 69 70 |
# File 'lib/padrino-core/mounter.rb', line 66 def to(mount_url) @uri_root = mount_url Padrino.insert_mounted_app(self) self end |