Module: Spider::App::ClassMethods
- Defined in:
- lib/spiderfw/app.rb
Overview
Methods set on the App main module. All instance attributes will be set to default values, but can be overridden by the module by setting the corresponding instance variable. Example:
module MyApp
include Spider::App
@controller = :MyController
@label = 'My Application'
end
Instance Attribute Summary collapse
-
#gettext_dirs ⇒ Array
readonly
A list of directories to look for translations.
-
#gettext_domain ⇒ String
readonly
Gettext domain of the app.
-
#gettext_extensions ⇒ Array
readonly
File extensions to parse for translations.
-
#gettext_parsers ⇒ Array
readonly
A list of tettext parsers to use for the app.
-
#label ⇒ String
readonly
App.
-
#models_path ⇒ String
readonly
Path of the ‘models’ folder.
-
#path ⇒ String
readonly
Filesystem path of the app.
-
#pub_path ⇒ String
readonly
Path of the ‘public’ folder.
-
#route_url ⇒ String
readonly
Url from which the app will be routed.
-
#setup_path ⇒ String
readonly
Path of the ‘setup’ folder.
-
#short_name ⇒ String
readonly
Name, without spaces.
-
#short_prefix ⇒ String
Prefix used to distinguish Database table.
-
#spec ⇒ AppSpec
readonly
The app’s AppSpec.
-
#tags_path ⇒ String
readonly
Path of the ‘tags’ folder.
-
#test_path ⇒ String
readonly
Path of the ‘test’ folder.
-
#version ⇒ Gem::Version
readonly
App’s version.
-
#views_path ⇒ String
readonly
Path of the ‘views’ folder.
-
#widgets_path ⇒ String
readonly
Path of the ‘widgets’ folder.
Instance Method Summary collapse
-
#app ⇒ self
Convenience method: since all classes inside the app have an #app method, the App itself has it too.
-
#base_path ⇒ String
The path to the apps’ container (the home or the Spider lib).
-
#controller ⇒ Spider::Controller
If setting the instance variable, use a Symbol.
-
#controllers ⇒ Array
An array of all the Controller subclasses defined inside the module.
-
#description ⇒ String
Description or spec.description or name.
-
#find_resource(type, name, cur_path = nil) ⇒ Spider::Resource
Finds a resource (see Spider.find_resource).
-
#find_resource_path(type, name, cur_path = nil) ⇒ String
Finds the path of the resource (see Spider#find_resource).
-
#full_name ⇒ String
The apps’ full_name or spec.name.
-
#get_tag(tag) ⇒ Object
The object corresponding to a registered tag.
-
#has_tag?(tag) ⇒ bool
Whether the given tag is registered.
-
#http_s_url(action = nil) ⇒ String
If the site supports SSL, returns the #https_url; otherwise, the #http_url.
-
#http_url(action = nil) ⇒ String
The full url used to access the application from the browser.
-
#https_url(action = nil) ⇒ String
The full url used to access the application from the browser, prefixed with https.
-
#init ⇒ Object
Initializes missing variables to default variables.
-
#installed_version ⇒ Gem::Version
Returns the currently installed version of an app.
-
#installed_version=(version) ⇒ nil
Sets the currently installed version of an app.
-
#load_spec(spec_path = nil) ⇒ AppSpec
Loads the app’s .spec file.
-
#models(container = nil) ⇒ Array
An array of all the BaseModel subclasses defined inside the module.
-
#pub_url ⇒ String
option is set to ‘publish’, the app’s url inside the home is returned.
-
#pub_url! ⇒ String
The url to the app’s public content, inside the app’s folder (ignoring publishing mode).
-
#register_tag(tag, obj) ⇒ void
Register the pointer from a widget tag to the an object.
-
#relative_path ⇒ String
The app’s path, relative to its container (the home or the Spider lib).
-
#req(*list) ⇒ nil
(also: #app_require)
Require files inside the App’s path.
-
#request_url ⇒ String
(also: #url)
The path used to access the application from the browser.
-
#route(path, dest = nil, options = nil) ⇒ nil
Calls route on the app’s controller (see Dispatcher#route).
- #route_path(action = '') ⇒ Object
Instance Attribute Details
#gettext_dirs ⇒ Array (readonly)
67 68 69 |
# File 'lib/spiderfw/app.rb', line 67 def gettext_dirs @gettext_dirs end |
#gettext_domain ⇒ String (readonly)
73 74 75 |
# File 'lib/spiderfw/app.rb', line 73 def gettext_domain @gettext_domain end |
#gettext_extensions ⇒ Array (readonly)
69 70 71 |
# File 'lib/spiderfw/app.rb', line 69 def gettext_extensions @gettext_extensions end |
#gettext_parsers ⇒ Array (readonly)
A list of tettext parsers to use for the app
71 72 73 |
# File 'lib/spiderfw/app.rb', line 71 def gettext_parsers @gettext_parsers end |
#label ⇒ String (readonly)
59 60 61 |
# File 'lib/spiderfw/app.rb', line 59 def label @label end |
#models_path ⇒ String (readonly)
53 54 55 |
# File 'lib/spiderfw/app.rb', line 53 def models_path @models_path end |
#path ⇒ String (readonly)
39 40 41 |
# File 'lib/spiderfw/app.rb', line 39 def path @path end |
#pub_path ⇒ String (readonly)
41 42 43 |
# File 'lib/spiderfw/app.rb', line 41 def pub_path @pub_path end |
#route_url ⇒ String (readonly)
57 58 59 |
# File 'lib/spiderfw/app.rb', line 57 def route_url @route_url end |
#setup_path ⇒ String (readonly)
45 46 47 |
# File 'lib/spiderfw/app.rb', line 45 def setup_path @setup_path end |
#short_name ⇒ String (readonly)
55 56 57 |
# File 'lib/spiderfw/app.rb', line 55 def short_name @short_name end |
#short_prefix ⇒ String
63 64 65 |
# File 'lib/spiderfw/app.rb', line 63 def short_prefix @short_prefix end |
#spec ⇒ AppSpec (readonly)
65 66 67 |
# File 'lib/spiderfw/app.rb', line 65 def spec @spec end |
#tags_path ⇒ String (readonly)
51 52 53 |
# File 'lib/spiderfw/app.rb', line 51 def end |
#test_path ⇒ String (readonly)
43 44 45 |
# File 'lib/spiderfw/app.rb', line 43 def test_path @test_path end |
#version ⇒ Gem::Version (readonly)
61 62 63 |
# File 'lib/spiderfw/app.rb', line 61 def version @version end |
#views_path ⇒ String (readonly)
49 50 51 |
# File 'lib/spiderfw/app.rb', line 49 def views_path @views_path end |
#widgets_path ⇒ String (readonly)
47 48 49 |
# File 'lib/spiderfw/app.rb', line 47 def end |
Instance Method Details
#app ⇒ self
Convenience method: since all classes inside the app have an #app method, the App itself has it too
260 261 262 |
# File 'lib/spiderfw/app.rb', line 260 def app self end |
#base_path ⇒ String
241 242 243 244 245 246 247 |
# File 'lib/spiderfw/app.rb', line 241 def base_path if Spider.paths[:apps] && @path.index(Spider.paths[:apps]) Spider.paths[:apps] else $SPIDER_PATH end end |
#controller ⇒ Spider::Controller
If setting the instance variable, use a Symbol
185 186 187 188 189 190 191 192 |
# File 'lib/spiderfw/app.rb', line 185 def controller if (!@controller || !const_defined?(@controller)) @controller = :AppController return const_set(@controller, Spider::PageController.clone) end return const_get(@controller) end |
#controllers ⇒ Array
208 209 210 |
# File 'lib/spiderfw/app.rb', line 208 def controllers self.constants.map{ |m| const_get(m) }.select{ |m| m.subclass_of? Spider::Controller } end |
#description ⇒ String
112 113 114 115 |
# File 'lib/spiderfw/app.rb', line 112 def description desc = @description || self.spec.description desc.blank? ? self.name : desc end |
#find_resource(type, name, cur_path = nil) ⇒ Spider::Resource
Finds a resource (see Spider.find_resource)
214 215 216 |
# File 'lib/spiderfw/app.rb', line 214 def find_resource(type, name, cur_path=nil) Spider.find_resource(type, name, cur_path, self) end |
#find_resource_path(type, name, cur_path = nil) ⇒ String
Finds the path of the resource (see Spider#find_resource)
220 221 222 223 |
# File 'lib/spiderfw/app.rb', line 220 def find_resource_path(type, name, cur_path=nil) res = Spider.find_resource(type, name, cur_path, self) return res ? res.path : nil end |
#full_name ⇒ String
107 108 109 |
# File 'lib/spiderfw/app.rb', line 107 def full_name @full_name || self.spec.name end |
#get_tag(tag) ⇒ Object
347 348 349 |
# File 'lib/spiderfw/app.rb', line 347 def get_tag(tag) [tag] end |
#has_tag?(tag) ⇒ bool
353 354 355 356 |
# File 'lib/spiderfw/app.rb', line 353 def has_tag?(tag) return false unless [tag] ? true : false end |
#http_s_url(action = nil) ⇒ String
163 164 165 166 |
# File 'lib/spiderfw/app.rb', line 163 def http_s_url(action=nil) return https_url(action) if Spider.site && Spider.site.ssl? return http_url(action) end |
#http_url(action = nil) ⇒ String
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/spiderfw/app.rb', line 127 def http_url(action=nil) if u = Spider.conf.get("#{@dotted_name}.http_url") if action u += '/' if u[-1].chr != '/' u += action.to_s end return u end return nil unless Spider.site u = "http://#{Spider.site.domain}" u += ":#{Spider.site.port}" unless Spider.site.port == 80 u += url u += "/"+action.to_s if action u end |
#https_url(action = nil) ⇒ String
The full url used to access the application from the browser, prefixed with https
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/spiderfw/app.rb', line 146 def https_url(action=nil) if u = Spider.conf.get("#{@dotted_name}.https_url") if action u += '/' if u[-1].chr != '/' u += action.to_s end return u end return nil unless Spider.site && Spider.site.ssl? u = "https://#{Spider.site.domain}" u += ":#{Spider.site.ssl_port}" unless Spider.site.ssl_port == 443 u += url u += "/"+action.to_s if action u end |
#init ⇒ Object
Initializes missing variables to default variables.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/spiderfw/app.rb', line 77 def init unless @path file = caller[1].split(':')[0] dir = File.dirname(file) @path = dir end @path = File.(@path) @short_name ||= Inflector.underscore(self.name).gsub(File::SEPARATOR, '_') @dotted_name = Inflector.underscore(self.name).gsub(File::SEPARATOR, '.') @pub_path ||= File.join(@path, 'public') @test_path ||= File.join(@path, 'test') @setup_path ||= File.join(@path, 'setup') @models_path ||= File.join(@path, 'models') ||= File.join(@path, 'widgets') @views_path ||= File.join(@path, '/views') ||= File.join(@path, 'tags') @version = Gem::Version.new(@version.to_s) if @version && !@version.is_a?(Gem::Version) spec_path = File.join(@path, "#{@short_name}.appspec") load_spec(spec_path) if File.exists?(spec_path) @route_url ||= Inflector.underscore(self.name) @label ||= @short_name.split('_').each{ |p| p[0] = p[0].chr.upcase }.join(' ') @gettext_parsers ||= [] @gettext_dirs ||= ['lib','bin','controllers','models','views','widgets','public'] @gettext_extensions ||= ['rb','rhtml','shtml','js'] @gettext_domain ||= @short_name end |
#installed_version ⇒ Gem::Version
Returns the currently installed version of an app
303 304 305 306 307 |
# File 'lib/spiderfw/app.rb', line 303 def installed_version FileUtils.mkpath(File.dirname(installed_version_path)) return unless File.exist?(installed_version_path) return Gem::Version.new(IO.read(installed_version_path)) end |
#installed_version=(version) ⇒ nil
Sets the currently installed version of an app
312 313 314 315 316 317 318 |
# File 'lib/spiderfw/app.rb', line 312 def installed_version=(version) FileUtils.mkpath(File.dirname(installed_version_path)) version = Gem::Version.new(version) unless version.is_a?(Gem::Version) File.open(installed_version_path, 'w') do |f| f << version.to_s end end |
#load_spec(spec_path = nil) ⇒ AppSpec
Loads the app’s .spec file
323 324 325 326 327 328 |
# File 'lib/spiderfw/app.rb', line 323 def load_spec(spec_path=nil) @spec = AppSpec.load(spec_path) @spec.app_id = File.basename(spec_path, 'appsec') unless @spec.app_id @version = @spec.version if @spec.version @spec end |
#models(container = nil) ⇒ Array
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/spiderfw/app.rb', line 195 def models(container=nil) container ||= self mods = [] container.constants.each do |c| begin mods += get_models(container.const_get(c)) rescue LoadError end end return mods end |
#pub_url ⇒ String
option is set to ‘publish’, the app’s url inside the home is returned.
170 171 172 173 174 175 176 |
# File 'lib/spiderfw/app.rb', line 170 def pub_url if Spider.conf.get('static_content.mode') == 'publish' Spider::HomeController.pub_url+'/apps/'+self.short_name else request_url+'/public' end end |
#pub_url! ⇒ String
179 180 181 |
# File 'lib/spiderfw/app.rb', line 179 def pub_url! request_url+'/public' end |
#register_tag(tag, obj) ⇒ void
This method returns an undefined value.
Register the pointer from a widget tag to the an object
340 341 342 343 |
# File 'lib/spiderfw/app.rb', line 340 def register_tag(tag, obj) ||= {} [tag] = obj end |
#relative_path ⇒ String
232 233 234 235 236 237 238 |
# File 'lib/spiderfw/app.rb', line 232 def relative_path if Spider.paths[:apps] && @path.index(Spider.paths[:apps]) == 0 return @path[Spider.paths[:apps].length+1..-1] else return @path[$SPIDER_PATHS[:core_apps].length+1..-1] end end |
#req(*list) ⇒ nil Also known as: app_require
Require files inside the App’s path
Can accept either a list of files to require, relative to the app’s path; or, a Hash containing arrays for keys corresponding to folders inside app (e.g. :models, :controllers)
If an Hash is provided, will load files in the order :lib, :models, :widgets, :controllers, followed by any additional keys, in the order they are defined in the Hash (under Ruby 1.9.x), or in random order (Ruby 1.8.x)
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/spiderfw/app.rb', line 273 def req(*list) do_require = lambda{ |f| Kernel.require File.join(@path, f) } if list.first.is_a?(Hash) hash = list.first load_keys = ([:lib, :models, :widgets, :controllers] + hash.keys).uniq load_keys.each do |k| if hash[k].is_a?(Array) hash[k].each{ |file| if k == :widgets file = File.join(file, file) end file = File.join(k.to_s, file) do_require.call(file) } end end else list.each do |file| do_require.call(file) end end end |
#request_url ⇒ String Also known as: url
118 119 120 121 122 123 |
# File 'lib/spiderfw/app.rb', line 118 def request_url if u = Spider.conf.get("#{@dotted_name}.url") return u end Spider::ControllerMixins::HTTPMixin.reverse_proxy_mapping('/'+@route_url) end |
#route(path, dest = nil, options = nil) ⇒ nil
Calls route on the app’s controller (see Dispatcher#route).
227 228 229 |
# File 'lib/spiderfw/app.rb', line 227 def route(path, dest=nil, =nil) self.controller.route(path, dest, ) end |
#route_path(action = '') ⇒ Object
250 251 252 253 254 |
# File 'lib/spiderfw/app.rb', line 250 def route_path(action='') path = Spider::ControllerMixins::HTTPMixin.reverse_proxy_mapping('/'+@route_url) action = action[1..-1] if action[0].chr == '/' [path, action].reject{ |p| p.blank? }.join('/') end |