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

Instance Method Summary collapse

Instance Attribute Details

#gettext_dirsArray (readonly)

Returns A list of directories to look for translations.

Returns:

  • (Array)

    A list of directories to look for translations



69
70
71
# File 'lib/spiderfw/app.rb', line 69

def gettext_dirs
  @gettext_dirs
end

#gettext_extensionsArray (readonly)

Returns File extensions to parse for translations.

Returns:

  • (Array)

    File extensions to parse for translations



71
72
73
# File 'lib/spiderfw/app.rb', line 71

def gettext_extensions
  @gettext_extensions
end

#gettext_parsersArray (readonly)

A list of tettext parsers to use for the app

Returns:

  • (Array)


73
74
75
# File 'lib/spiderfw/app.rb', line 73

def gettext_parsers
  @gettext_parsers
end

#idString (readonly)

Returns unique identifier for the app.

Returns:

  • (String)

    unique identifier for the app



39
40
41
# File 'lib/spiderfw/app.rb', line 39

def id
  @id
end

#labelString (readonly)

Returns app.

Returns:

  • (String)

    app



61
62
63
# File 'lib/spiderfw/app.rb', line 61

def label
  @label
end

#models_pathString (readonly)

Returns path of the ‘models’ folder.

Returns:

  • (String)

    path of the ‘models’ folder



55
56
57
# File 'lib/spiderfw/app.rb', line 55

def models_path
  @models_path
end

#pathString (readonly)

Returns filesystem path of the app.

Returns:

  • (String)

    filesystem path of the app



41
42
43
# File 'lib/spiderfw/app.rb', line 41

def path
  @path
end

#pub_pathString (readonly)

Returns path of the ‘public’ folder.

Returns:

  • (String)

    path of the ‘public’ folder



43
44
45
# File 'lib/spiderfw/app.rb', line 43

def pub_path
  @pub_path
end

#route_urlString (readonly)

Returns url from which the app will be routed.

Returns:

  • (String)

    url from which the app will be routed



59
60
61
# File 'lib/spiderfw/app.rb', line 59

def route_url
  @route_url
end

#setup_pathString (readonly)

Returns path of the ‘setup’ folder.

Returns:

  • (String)

    path of the ‘setup’ folder



47
48
49
# File 'lib/spiderfw/app.rb', line 47

def setup_path
  @setup_path
end

#short_nameString (readonly)

Returns name, without spaces.

Returns:

  • (String)

    name, without spaces



57
58
59
# File 'lib/spiderfw/app.rb', line 57

def short_name
  @short_name
end

#short_prefixString

Returns prefix used to distinguish Database table.

Returns:

  • (String)

    prefix used to distinguish Database table



65
66
67
# File 'lib/spiderfw/app.rb', line 65

def short_prefix
  @short_prefix
end

#specAppSpec (readonly)

Returns the app’s AppSpec.

Returns:

  • (AppSpec)

    the app’s AppSpec



67
68
69
# File 'lib/spiderfw/app.rb', line 67

def spec
  @spec
end

#tags_pathString (readonly)

Returns path of the ‘tags’ folder.

Returns:

  • (String)

    path of the ‘tags’ folder



53
54
55
# File 'lib/spiderfw/app.rb', line 53

def tags_path
  @tags_path
end

#test_pathString (readonly)

Returns path of the ‘test’ folder.

Returns:

  • (String)

    path of the ‘test’ folder



45
46
47
# File 'lib/spiderfw/app.rb', line 45

def test_path
  @test_path
end

#versionGem::Version (readonly)

Returns app’s version.

Returns:

  • (Gem::Version)

    app’s version



63
64
65
# File 'lib/spiderfw/app.rb', line 63

def version
  @version
end

#views_pathString (readonly)

Returns path of the ‘views’ folder.

Returns:

  • (String)

    path of the ‘views’ folder



51
52
53
# File 'lib/spiderfw/app.rb', line 51

def views_path
  @views_path
end

#widgets_pathString (readonly)

Returns path of the ‘widgets’ folder.

Returns:

  • (String)

    path of the ‘widgets’ folder



49
50
51
# File 'lib/spiderfw/app.rb', line 49

def widgets_path
  @widgets_path
end

Instance Method Details

#appself

Convenience method: since all classes inside the app have an #app method, the App itself has it too

Returns:

  • (self)


235
236
237
# File 'lib/spiderfw/app.rb', line 235

def app
    self
end

#controllerSpider::Controller

If setting the instance variable, use a Symbol

Returns:



176
177
178
179
180
181
182
183
# File 'lib/spiderfw/app.rb', line 176

def controller
    if (!@controller || !const_defined?(@controller))
        @controller = :AppController
        return const_set(@controller, Spider::PageController.clone)
        
    end
    return const_get(@controller)
end

#controllersArray

Returns An array of all the Controller subclasses defined inside the module.

Returns:

  • (Array)

    An array of all the Controller subclasses defined inside the module



199
200
201
# File 'lib/spiderfw/app.rb', line 199

def controllers
    self.constants.map{ |m| const_get(m) }.select{ |m| m.subclass_of? Spider::Controller }
end

#descriptionString

Returns description or spec.description or name.

Returns:

  • (String)

    description or spec.description or name



111
112
113
114
# File 'lib/spiderfw/app.rb', line 111

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)

Returns:



205
206
207
# File 'lib/spiderfw/app.rb', line 205

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)

Returns:

  • (String)


211
212
213
214
# File 'lib/spiderfw/app.rb', line 211

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_nameString

Returns The apps’ full_name or spec.name.

Returns:

  • (String)

    The apps’ full_name or spec.name



106
107
108
# File 'lib/spiderfw/app.rb', line 106

def full_name
    @full_name || self.spec.name
end

#get_tag(tag) ⇒ Object

Returns The object corresponding to a registered tag.

Parameters:

  • tag (String)

Returns:

  • (Object)

    The object corresponding to a registered tag



296
297
298
# File 'lib/spiderfw/app.rb', line 296

def get_tag(tag)
    @tags[tag]
end

#has_tag?(tag) ⇒ bool

Returns Whether the given tag is registered.

Parameters:

  • tag (String)

Returns:

  • (bool)

    Whether the given tag is registered



302
303
304
305
# File 'lib/spiderfw/app.rb', line 302

def has_tag?(tag)
    return false unless @tags
    @tags[tag] ? true : false
end

#http_s_urlString

Returns If the site supports SSL, returns the #https_url; otherwise, the #http_url.

Returns:

  • (String)

    If the site supports SSL, returns the #https_url; otherwise, the #http_url



154
155
156
157
# File 'lib/spiderfw/app.rb', line 154

def http_s_url
    return https_url if Spider.site && Spider.site.ssl?
    return http_url
end

#http_url(action = nil) ⇒ String

Returns The full url used to access the application from the browser.

Returns:

  • (String)

    The full url used to access the application from the browser



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/spiderfw/app.rb', line 126

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_urlString

The full url used to access the application from the browser, prefixed with https

Returns:

  • (String)


145
146
147
148
149
150
151
# File 'lib/spiderfw/app.rb', line 145

def https_url
    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
end

#initObject

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
# 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.expand_path(@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')
    @widgets_path ||= File.join(@path, 'widgets')
    @views_path ||= File.join(@path, '/views')
    @tags_path ||= 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']
    
    find_tags
end

#installed_versionGem::Version

Returns the currently installed version of an app

Returns:

  • (Gem::Version)


252
253
254
255
256
# File 'lib/spiderfw/app.rb', line 252

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

Parameters:

  • version (String|Gem::Version)

Returns:

  • (nil)


261
262
263
264
265
266
267
# File 'lib/spiderfw/app.rb', line 261

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

Parameters:

  • spec_path (String) (defaults to: nil)

Returns:



272
273
274
275
276
277
# File 'lib/spiderfw/app.rb', line 272

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

Returns An array of all the BaseModel subclasses defined inside the module.

Returns:

  • (Array)

    An array of all the BaseModel subclasses defined inside the module



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/spiderfw/app.rb', line 186

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_urlString

option is set to ‘publish’, the app’s url inside the home is returned.

Returns:

  • (String)

    The url to the app’s public content. If the static_content.mode configuration



161
162
163
164
165
166
167
# File 'lib/spiderfw/app.rb', line 161

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

Returns The url to the app’s public content, inside the app’s folder (ignoring publishing mode).

Returns:

  • (String)

    The url to the app’s public content, inside the app’s folder (ignoring publishing mode)



170
171
172
# File 'lib/spiderfw/app.rb', line 170

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

Parameters:

  • tag (String)
  • object (String)


289
290
291
292
# File 'lib/spiderfw/app.rb', line 289

def register_tag(tag, obj)
    @tags ||= {}
    @tags[tag] = obj
end

#relative_pathString

Returns The app’s path, relative to its container (the home or the Spider lib).

Returns:

  • (String)

    The app’s path, relative to its container (the home or the Spider lib)



223
224
225
226
227
228
229
# File 'lib/spiderfw/app.rb', line 223

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

Require files inside the App’s path

Parameters:

  • files (file1, file2...)

    to require

Returns:

  • (nil)


242
243
244
245
246
# File 'lib/spiderfw/app.rb', line 242

def req(*list)
    list.each do |file|
        require File.join(@path, file)
    end
end

#request_urlString Also known as: url

Returns The path used to access the application from the browser.

Returns:

  • (String)

    The path used to access the application from the browser



117
118
119
120
121
122
# File 'lib/spiderfw/app.rb', line 117

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

Returns:

  • (nil)


218
219
220
# File 'lib/spiderfw/app.rb', line 218

def route(path, dest=nil, options=nil)
    self.controller.route(path, dest, options)
end