Class: Ramaze::App
- Inherits:
-
Object
- Object
- Ramaze::App
- Includes:
- Innate::Optioned
- Defined in:
- lib/ramaze/app.rb
Overview
App is the superclass for applications and acts as their prototype when it comes to configuration.
An application consists of options, a location, and a list of objects. The objects are usually Controllers.
The options are inherited, the basics are set in Ramaze.options, from there to Ramaze::App.options, and finally into every instance of App.
This allows to collect Controllers of your application into a common group that can easily be used in other applications, while retaining the original options.
Every instance of App is mapped in AppMap, which is the default location to #call from Rack.
Additionally, every App can have custom locations for root/public/view/layout directories, which allows reuse beyond directory boundaries.
In contrast to Innate, where all Nodes share the same middleware, App also has a subset of middleware that handles serving static files, routes and rewrites.
To indicate that a Controller belongs to a specific application, you can pass a second argument to Controller::map
The App instance will be created for you and if you don’t use any other applications in your code there is nothing else you have to do. Others can now come and simply reuse your code in their own applications.
There is some risk of name collisions if everybody calls their app ‘:wiki`, but given that you only use one foreign app of this kind might give less reason for concern.
If you still try to use two apps with the same name, you have to be careful, loading one first, renaming it, then loading the second one.
The naming of an App has no influence on any other aspects of dispatching or configuration.
Constant Summary collapse
- APP_LIST =
{}
Instance Attribute Summary collapse
-
#location ⇒ Object
Returns the value of attribute location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#url_map ⇒ Object
readonly
Returns the value of attribute url_map.
Class Method Summary collapse
-
.[](name) ⇒ Ramaze::App
Returns the application for the given name.
-
.find_or_create(name, location = nil) ⇒ Ramaze::App
Finds or creates an application for the given name and URI.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Allows the application to be called as a Rack middleware.
-
#initialize(name, location = nil) ⇒ App
constructor
Creates a new instance of the application and sets the name and location.
-
#map(location, object) ⇒ Object
Maps an object to the given URI.
-
#public_roots ⇒ Array
Returns an array containing all the public directories for each root directory.
-
#sync ⇒ Object
Syncs the instance of the current application with Ramaze::AppMap.
-
#to(object) ⇒ Object
Returns a URI to the given object.
-
#to_app ⇒ Rack::Cascade
Converts the application to a Rack compatible class.
Constructor Details
#initialize(name, location = nil) ⇒ App
Creates a new instance of the application and sets the name and location.
105 106 107 108 109 110 111 112 113 |
# File 'lib/ramaze/app.rb', line 105 def initialize(name, location = nil) @name = name.to_sym @url_map = Innate::URLMap.new self.location = location if location APP_LIST[@name] = self @options = self.class..sub(@name) end |
Instance Attribute Details
#location ⇒ Object
Returns the value of attribute location.
71 72 73 |
# File 'lib/ramaze/app.rb', line 71 def location @location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
71 72 73 |
# File 'lib/ramaze/app.rb', line 71 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
71 72 73 |
# File 'lib/ramaze/app.rb', line 71 def @options end |
#url_map ⇒ Object (readonly)
Returns the value of attribute url_map.
71 72 73 |
# File 'lib/ramaze/app.rb', line 71 def url_map @url_map end |
Class Method Details
.[](name) ⇒ Ramaze::App
Returns the application for the given name.
93 94 95 |
# File 'lib/ramaze/app.rb', line 93 def self.[](name) APP_LIST[name.to_sym] end |
.find_or_create(name, location = nil) ⇒ Ramaze::App
Finds or creates an application for the given name and URI.
81 82 83 84 |
# File 'lib/ramaze/app.rb', line 81 def self.find_or_create(name, location = nil) location = '/' if location.nil? && name == :pristine self[name] || new(name, location) end |
Instance Method Details
#call(env) ⇒ Object
Allows the application to be called as a Rack middleware.
143 144 145 |
# File 'lib/ramaze/app.rb', line 143 def call(env) to_app.call(env) end |
#map(location, object) ⇒ Object
Maps an object to the given URI.
169 170 171 |
# File 'lib/ramaze/app.rb', line 169 def map(location, object) url_map.map(location, object) end |
#public_roots ⇒ Array
Returns an array containing all the public directories for each root directory.
193 194 195 196 |
# File 'lib/ramaze/app.rb', line 193 def public_roots roots, publics = [*.roots], [*.publics] roots.map{|root| publics.map{|public| ::File.join(root, public) }}.flatten end |
#sync ⇒ Object
Syncs the instance of the current application with Ramaze::AppMap.
121 122 123 |
# File 'lib/ramaze/app.rb', line 121 def sync AppMap.map(location, self) end |
#to(object) ⇒ Object
Returns a URI to the given object.
180 181 182 183 |
# File 'lib/ramaze/app.rb', line 180 def to(object) return unless mapped = url_map.to(object) [location, mapped].join('/').squeeze('/') end |