Class: UzuUzu::Application

Inherits:
Object show all
Defined in:
lib/uzuuzu-core/application.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Application

Returns a new instance of Application.

Yields:

  • (_self)

Yield Parameters:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/uzuuzu-core/application.rb', line 39

def initialize(options={})
  ::Thread.current[:application] = self
  options.each do |key, value|
    case(key)
    when :name, 'name'
      @name = value
    when :config, 'config'
      @config_file = value
    end
  end if options.kind_of?(Hash)
  @name ||= :uzuuzu
  @environments = Environments.new(@config_file)
  ::UzuUzu.apps[@name] = self
  yield self if block_given?
end

Instance Attribute Details

#after_filtersObject

Returns the value of attribute after_filters.



19
20
21
# File 'lib/uzuuzu-core/application.rb', line 19

def after_filters
  @after_filters
end

#before_filtersObject

Returns the value of attribute before_filters.



17
18
19
# File 'lib/uzuuzu-core/application.rb', line 17

def before_filters
  @before_filters
end

#config_fileObject

Returns the value of attribute config_file.



11
12
13
# File 'lib/uzuuzu-core/application.rb', line 11

def config_file
  @config_file
end

#controller_rootObject

Returns the value of attribute controller_root.



23
24
25
# File 'lib/uzuuzu-core/application.rb', line 23

def controller_root
  @controller_root
end

#controllersObject

Returns the value of attribute controllers.



15
16
17
# File 'lib/uzuuzu-core/application.rb', line 15

def controllers
  @controllers
end

#environmentsObject

Returns the value of attribute environments.



13
14
15
# File 'lib/uzuuzu-core/application.rb', line 13

def environments
  @environments
end

#helper_rootObject

Returns the value of attribute helper_root.



25
26
27
# File 'lib/uzuuzu-core/application.rb', line 25

def helper_root
  @helper_root
end

#helpersObject

Returns the value of attribute helpers.



21
22
23
# File 'lib/uzuuzu-core/application.rb', line 21

def helpers
  @helpers
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/uzuuzu-core/application.rb', line 9

def name
  @name
end

#view_rootObject

Returns the value of attribute view_root.



27
28
29
# File 'lib/uzuuzu-core/application.rb', line 27

def view_root
  @view_root
end

Class Method Details

.after_filtersObject



71
72
73
# File 'lib/uzuuzu-core/application.rb', line 71

def self.after_filters
  @@after_filters ||= [Controller.method(:after_all)]
end

.before_filtersObject



63
64
65
# File 'lib/uzuuzu-core/application.rb', line 63

def self.before_filters
  @@before_filters ||= [Controller.method(:before_all)]
end

.controller_rootObject



95
96
97
# File 'lib/uzuuzu-core/application.rb', line 95

def self.controller_root
  @@controller_root ||= 'controller'
end

.controllersObject

initialize



55
56
57
# File 'lib/uzuuzu-core/application.rb', line 55

def self.controllers
  @@controllers ||= [Controller]
end

.currentObject



32
33
34
# File 'lib/uzuuzu-core/application.rb', line 32

def self.current
  ::Thread.current[:application]
end

.helper_rootObject



111
112
113
# File 'lib/uzuuzu-core/application.rb', line 111

def self.helper_root
  @@helper_root ||= 'helper'
end

.helpersObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/uzuuzu-core/application.rb', line 79

def self.helpers
  @@helpers ||= [
                 Helper::Controller,
                 Helper::Form,
                 Helper::Jquery,
                 Helper::Localize,
                 Helper::Renderer,
                 Helper::Route,
                 Helper::XHtml
               ]
end

.view_rootObject



103
104
105
# File 'lib/uzuuzu-core/application.rb', line 103

def self.view_root
  @@view_root ||= 'view'
end

Instance Method Details

#auto_require(env) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/uzuuzu-core/application.rb', line 178

def auto_require(env)
  require_base = "/#{@name.to_s}"
  require_base = '' if @name == :uzuuzu || @name == '' || @name.blank?
  array = []
  if env['PATH_INFO'] == '/'
    require_noerror "#{controller_root}#{require_base}/index"
    require_noerror "#{helper_root}#{require_base}/index"
  end
  env['PATH_INFO'].split('/').each do |path|
    array << path
    begin
      if array.size > 1
        require_noerror "#{controller_root}#{require_base}#{array.join('/')}/index"
        require_noerror "#{helper_root}#{require_base}#{array.join('/')}/index"
      end
      require_noerror "#{controller_root}#{require_base}#{array.join('/')}"
      require_noerror "#{helper_root}#{require_base}#{array.join('/')}"
    rescue
    end
  end
end

#call(env) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/uzuuzu-core/application.rb', line 122

def call(env)
  Thread.current[:application] = self
  request = UzuUzu::Request.new(env)
  response = UzuUzu::Response.new
  auto_require(env)
  classies = find_controller(env)
  if classies.blank?
    di_thread(request, response, nil, nil,  nil, nil, nil)
    return not_found(response)
  end

  is_send = false
  action_return = nil
  render = nil
  
  classies.reverse.each do |map|
    controller_class = map[:controller]
    helper_module = map[:helper]
    query = map[:query]
    route = map[:route]
    controller = controller_class.new
    action, query = find_action(controller, query)
    di_thread(request, response, nil, nil, nil, query, route)
    next if action.blank?
    controller.extend(::UzuUzu::Controller)
    helper = Helper::Helper.new
    helpers.each do |h|
      helper.extend(h)
    end
    helper.extend(helper_module) if helper_module.kind_of?(::Module)
    di_thread(request, response, controller, helper,  action, query, route)
    is_send = true
    action_return = send_action(request, response, controller, action, query)
    if response.body.blank?
      render = helper.render(action)
    end
    break
  end
  
  unless is_send
    return not_found(response)
  end
  if response.status == 200 and response.body.blank?
    response.write render || action_return
  end

  response.finish
end

#di_thread(request, response, controller, helper, action, query, route) ⇒ Object



291
292
293
294
295
296
297
298
299
300
# File 'lib/uzuuzu-core/application.rb', line 291

def di_thread(request, response, controller, helper, action, query, route)
  current = ::Thread.current
  current[:request] = request
  current[:response] = response
  current[:controller] = controller
  current[:helper] = helper
  current[:action] = action
  current[:query] = query
  current[:route] = route
end

#find_action(controller, query) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/uzuuzu-core/application.rb', line 305

def find_action(controller, query)
  action = nil
  index_flag = false
  query[-1] = query[-1].sub(/\.\w+$/, '') if query && query[-1]
  if query[0] && controller.respond_to?(query[0])
    action = query.shift
  end
  if action.nil? && controller.respond_to?(:index)
    action = 'index'
  end
  
  unless action.nil?
    arity = controller.method(action).arity
    owner = controller.method(action).owner
    unless controller.method(action).owner == controller.class
      index_flag = true
      if controller.respond_to?(:use_actions)
        controller.actions.each do |use_action|
          if use_action.to_s == action.to_s
            index_flag = false
          end
        end
      end
    end
    unless (arity >= 0 && query.size == arity) ||
           (arity < 0 && query.size >= ((-1 * arity)-1))
      index_flag = true
    end
    if index_flag && !(action == 'index') && controller.respond_to?(:index)
      query.unshift(action)
      action = 'index'
    end
    if controller.respond_to?(action)
      arity = controller.method(action).arity
      unless (arity >= 0 && query.size == arity) ||
             (arity < 0 && query.size >= ((-1 * arity)-1))
        action = nil
      end
    else
      action = nil
    end
  end
  [action, query]
end

#find_controller(env) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/uzuuzu-core/application.rb', line 236

def find_controller(env)
  classies = []
  request_paths = env['PATH_INFO'].split('/')
  request_paths.shift if request_paths[0].blank?
  
  _controllers = controllers.clone
  _base = "::#{@name.to_s.camel_case}"
  _base = '' if @name == :uzuuzu || @name == 'uzuuzu'
  begin
    _controllers << eval("#{_base}::#{controller_root.camel_case}")
  rescue LoadError => e
  rescue => e
  end
  _controllers.each do |c|
    _c = c
    _h = nil
    if request_paths.size == 0
      map = get_class_map('Index', _c, env['SCRIPT_NAME'], request_paths)
      if map && map[:controller].kind_of?(::Class)
        classies << map
      end
    end
    request_paths.each_with_index do |request_path, index|
      next if request_path.blank?
      c_str = request_path.camel_case
      route = "#{request_paths[0...index].join('/')}"
      if route.blank?
        route = "#{env['SCRIPT_NAME']}"
      else
        route = "#{env['SCRIPT_NAME']}/#{route}"
      end
      query = request_paths[index..-1]
      map = get_class_map('Index', _c, route, query)
      if map && map[:controller].kind_of?(::Class)
        classies << map
      end
      next if c_str == 'Index'
      route = "#{env['SCRIPT_NAME']}/#{request_paths[0..index].join('/')}"
      query = request_paths[(index+1)..-1]
      map = get_class_map(c_str, _c, route, query)
      if map && map[:controller].kind_of?(::Class)
        classies << map
      end
      _c = map[:controller] if map
      unless _c.kind_of?(::Class) || _c.kind_of?(::Module)
        break
      end
    end
  end
  classies
end

#find_helper(name, route) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/uzuuzu-core/application.rb', line 216

def find_helper(name, route)
  routes = route.split('/').map { |a| a.camel_case }
  if routes.blank? || name != routes[-1]
    routes << "#{name.camel_case}"
  end
  _base = "::#{@name.to_s.camel_case}"
  _base = '' if @name == :uzuuzu || @name == 'uzuuzu'
  helper = eval("#{_base}::#{helper_root.camel_case}")
  routes.each do |const_name|
    helper = helper.const_get(const_name) if helper.const_defined?(const_name)
  end
  helper
rescue LoadError => e
rescue NameError => e
rescue => e
end

#get_class_map(name, controller, route, query) ⇒ Object

auto_require



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/uzuuzu-core/application.rb', line 200

def get_class_map(name, controller, route, query)
  _c = controller.const_get(name) if controller.const_defined?(name)
  return nil if _c.nil?
  _h = find_helper(name, route)
  map = {
    :controller => _c,
    :helper => _h,
    :route => route,
    :query => query
  }
rescue NameError => e
end

#not_found(response) ⇒ Object

send_action



377
378
379
380
381
382
# File 'lib/uzuuzu-core/application.rb', line 377

def not_found(response)
  catch(:finish) do
    response.not_found
  end
  response.finish
end

#require_noerror(path) ⇒ Object

call



171
172
173
174
# File 'lib/uzuuzu-core/application.rb', line 171

def require_noerror(path)
  require path
rescue LoadError => e
end

#send_action(request, response, controller, action, query) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/uzuuzu-core/application.rb', line 353

def send_action(request, response, controller, action, query)
  value = nil
  catch(:finish) do
    before_filters.each do |filter|
      filter.call if filter.respond_to?(:call)
    end
    controller.send(:before) if controller.respond_to?(:before)
    controller.send("before_#{action}") if controller.respond_to?("before_#{action}")
    value = controller.send(action, *query)
  end
  catch(:finish) do
    controller.send("after_#{action}") if controller.respond_to?("after_#{action}")
  end
  catch(:finish) do
    controller.send(:after) if controller.respond_to?(:after)
  end
  after_filters.reverse.each do |filter|
    catch(:finish) do
      filter.call if filter.respond_to?(:call)
    end
  end
  value
end