Class: Tennpipes::Mounter
- Inherits:
-
Object
- Object
- Tennpipes::Mounter
- Defined in:
- lib/tennpipes-base/mounter.rb
Overview
Represents a particular mounted Tennpipes application. Stores the name of the application (app folder name) and url mount path.
Defined Under Namespace
Classes: ApplicationWrapper, 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 ⇒ Tennpipes::Application
The class object for the app if defined, nil otherwise.
-
#host(mount_host) ⇒ Object
Registers the mounted application onto Tennpipes for the given host.
-
#initialize(name, options = {}) ⇒ Mounter
constructor
A new instance of Mounter.
-
#map_onto(router) ⇒ Tennpipes::Router
Maps Tennpipes application onto a Tennpipes::Router.
-
#named_routes ⇒ Array
Returns the basic route information for each named route.
-
#routes ⇒ Object
Returns the route objects for the mounted application.
- #tennpipes_application? ⇒ Boolean
-
#to(mount_url) ⇒ Object
Registers the mounted application onto Tennpipes.
Constructor Details
#initialize(name, options = {}) ⇒ Mounter
Returns a new instance of Mounter.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/tennpipes-base/mounter.rb', line 79 def initialize(name, ={}) @name = name.to_s @app_class = [:app_class] || @name.camelize @gem = [:gem] || @app_class.split("::").first.underscore @app_file = [:app_file] || locate_app_file @app_obj = [:app_obj] || app_constant || locate_app_object ensure_app_file! || ensure_app_object! @app_obj = ApplicationWrapper.new(@app_obj, ) unless tennpipes_application? @app_root = [:app_root] || (@app_obj.respond_to?(:root) && @app_obj.root || File.dirname(@app_file)) @uri_root = "/" @cascade = [:cascade] ? true == [:cascade] ? DEFAULT_CASCADE.dup : Array([:cascade]) : [] Tennpipes::Reloader.exclude_constants << @app_class end |
Instance Attribute Details
#app_class ⇒ Object
Returns the value of attribute app_class.
66 67 68 |
# File 'lib/tennpipes-base/mounter.rb', line 66 def app_class @app_class end |
#app_file ⇒ Object
Returns the value of attribute app_file.
66 67 68 |
# File 'lib/tennpipes-base/mounter.rb', line 66 def app_file @app_file end |
#app_host ⇒ Object
Returns the value of attribute app_host.
66 67 68 |
# File 'lib/tennpipes-base/mounter.rb', line 66 def app_host @app_host end |
#app_obj ⇒ Object
Returns the value of attribute app_obj.
66 67 68 |
# File 'lib/tennpipes-base/mounter.rb', line 66 def app_obj @app_obj end |
#app_root ⇒ Object
Returns the value of attribute app_root.
66 67 68 |
# File 'lib/tennpipes-base/mounter.rb', line 66 def app_root @app_root end |
#cascade ⇒ Object
Returns the value of attribute cascade.
66 67 68 |
# File 'lib/tennpipes-base/mounter.rb', line 66 def cascade @cascade end |
#name ⇒ Object
Returns the value of attribute name.
66 67 68 |
# File 'lib/tennpipes-base/mounter.rb', line 66 def name @name end |
#uri_root ⇒ Object
Returns the value of attribute uri_root.
66 67 68 |
# File 'lib/tennpipes-base/mounter.rb', line 66 def uri_root @uri_root end |
Instance Method Details
#==(other) ⇒ Object
Makes two Mounters equal if they have the same name and uri_root.
197 198 199 |
# File 'lib/tennpipes-base/mounter.rb', line 197 def ==(other) other.is_a?(Mounter) && self.app_class == other.app_class && self.uri_root == other.uri_root end |
#app_constant ⇒ Tennpipes::Application
Returns the class object for the app if defined, nil otherwise.
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/tennpipes-base/mounter.rb', line 205 def app_constant klass = Object for piece in app_class.split("::") piece = piece.to_sym if klass.const_defined?(piece, false) klass = klass.const_get(piece) else return end end klass end |
#host(mount_host) ⇒ Object
Registers the mounted application onto Tennpipes for the given host.
125 126 127 128 129 |
# File 'lib/tennpipes-base/mounter.rb', line 125 def host(mount_host) @app_host = mount_host Tennpipes.insert_mounted_app(self) self end |
#map_onto(router) ⇒ Tennpipes::Router
Maps Tennpipes application onto a Tennpipes::Router. For use in constructing a Rack application.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/tennpipes-base/mounter.rb', line 142 def map_onto(router) app_data = self app_obj = @app_obj if tennpipes_application? app_obj.set :uri_root, app_data.uri_root app_obj.set :app_name, app_data.app_obj.app_name.to_s 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, Tennpipes.root('public', app_data.uri_root) unless File.exist?(app_obj.public_folder) app_obj.set :static, File.exist?(app_obj.public_folder) if app_obj.nil? app_obj.set :cascade, app_data.cascade else app_obj.uri_root = app_data.uri_root app_obj.public_folder = Tennpipes.root('public', app_data.uri_root) unless File.exist?(app_obj.public_folder) end 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 ⇒ Array
Returns the basic route information for each named route.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/tennpipes-base/mounter.rb', line 174 def named_routes app_obj.routes.map { |route| 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})" 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 ⇒ Object
Returns the route objects for the mounted application.
164 165 166 |
# File 'lib/tennpipes-base/mounter.rb', line 164 def routes app_obj.routes end |
#tennpipes_application? ⇒ Boolean
93 94 95 96 97 |
# File 'lib/tennpipes-base/mounter.rb', line 93 def tennpipes_application? @app_obj.ancestors.include?(Tennpipes::Application) rescue NameError false end |
#to(mount_url) ⇒ Object
Registers the mounted application onto Tennpipes.
108 109 110 111 112 |
# File 'lib/tennpipes-base/mounter.rb', line 108 def to(mount_url) @uri_root = mount_url Tennpipes.insert_mounted_app(self) self end |