moomerman-rambo 0.4.11 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rambo/application_context.rb +52 -0
- data/lib/rambo/application_request.rb +25 -0
- data/lib/rambo/controller.rb +1 -1
- data/lib/rambo/controller/template.rb +25 -13
- data/lib/rambo/env.rb +10 -31
- data/lib/rambo/middleware/proxy.rb +1 -1
- data/lib/rambo/request.rb +20 -6
- data/lib/rambo/server.rb +35 -20
- metadata +5 -2
@@ -0,0 +1,52 @@
|
|
1
|
+
module Rambo
|
2
|
+
class ApplicationContext
|
3
|
+
attr_accessor :application_name
|
4
|
+
|
5
|
+
def initialize(application_name = nil)
|
6
|
+
@application_name = application_name
|
7
|
+
puts "Initializing application: #{application_name || 'default'}"
|
8
|
+
@prefix = "#{self.application_name}/" if self.application_name
|
9
|
+
@prefix ||= ''
|
10
|
+
load_classes
|
11
|
+
end
|
12
|
+
|
13
|
+
def load_classes
|
14
|
+
Dir["#{@prefix}controller/*.rb"].each { |x| funkyload x; }
|
15
|
+
Dir["#{@prefix}model/*.rb"].each { |x| funkyload x }
|
16
|
+
Dir["#{@prefix}lib/*.rb"].each { |x| funkyload x }
|
17
|
+
Dir["#{@prefix}*.rb"].each { |x| funkyload x unless x == 'Rakefile.rb' }
|
18
|
+
end
|
19
|
+
|
20
|
+
def reload
|
21
|
+
load_classes
|
22
|
+
end
|
23
|
+
|
24
|
+
def view_path
|
25
|
+
"./#{@prefix}view"
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def funkyload(file)
|
30
|
+
@@loadcache ||= {}
|
31
|
+
begin
|
32
|
+
if cache = @@loadcache[file]
|
33
|
+
return if Env.config['rambo'] and Env.config['rambo']['reload_classes'] == false
|
34
|
+
if (mtime = File.mtime(file)) > cache
|
35
|
+
puts "rambo: reloading: #{file}"
|
36
|
+
load file
|
37
|
+
@@loadcache[file] = mtime
|
38
|
+
end
|
39
|
+
else
|
40
|
+
puts " -> loading: #{file}"
|
41
|
+
load file
|
42
|
+
@@loadcache[file] = File.mtime(file)
|
43
|
+
end
|
44
|
+
rescue Exception => e
|
45
|
+
puts "Exception loading class [#{file}]: #{e.message}"
|
46
|
+
puts e.backtrace.join("\n")
|
47
|
+
raise e
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rambo
|
2
|
+
class ApplicationRequest < Rambo::Request
|
3
|
+
|
4
|
+
def initialize(env)
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
def application
|
9
|
+
path_components[1]
|
10
|
+
end
|
11
|
+
|
12
|
+
def controller
|
13
|
+
path_components[2] || default_controller
|
14
|
+
end
|
15
|
+
|
16
|
+
def action
|
17
|
+
path_components[3] || 'index'
|
18
|
+
end
|
19
|
+
|
20
|
+
def controller_class
|
21
|
+
self.application.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase } + '::' + super
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/rambo/controller.rb
CHANGED
@@ -7,24 +7,32 @@ module Template
|
|
7
7
|
render :erb, template, options, locals
|
8
8
|
end
|
9
9
|
|
10
|
+
def haml(template, options={}, locals={})
|
11
|
+
require 'haml' unless defined? ::Haml
|
12
|
+
render :haml, template, options, locals
|
13
|
+
end
|
14
|
+
|
10
15
|
private
|
11
16
|
def render(engine, template, options={}, locals={})
|
12
17
|
# extract generic options
|
13
18
|
layout = options.delete(:layout)
|
14
19
|
layout = :layout if layout.nil? || layout == true
|
15
|
-
views = options.delete(:views) ||
|
20
|
+
views = options.delete(:views) || self.request.application_context.view_path
|
16
21
|
locals = options.delete(:locals) || locals || {}
|
17
|
-
|
22
|
+
|
18
23
|
# render template
|
19
|
-
data = lookup_template(engine, template, views)
|
20
|
-
output = __send__("render_#{engine}",
|
21
|
-
|
24
|
+
data, options[:filename], options[:line] = lookup_template(engine, template, views)
|
25
|
+
output = __send__("render_#{engine}", data, options, locals)
|
26
|
+
|
22
27
|
# render layout
|
23
|
-
if layout
|
24
|
-
|
25
|
-
|
26
|
-
|
28
|
+
if layout
|
29
|
+
data, options[:filename], options[:line] = lookup_layout(engine, layout, views)
|
30
|
+
if data
|
31
|
+
output = __send__("render_#{engine}", data, options, locals) { output }
|
32
|
+
end
|
27
33
|
end
|
34
|
+
|
35
|
+
output
|
28
36
|
end
|
29
37
|
|
30
38
|
def lookup_template(engine, template, views_dir)
|
@@ -34,14 +42,14 @@ module Template
|
|
34
42
|
# cached
|
35
43
|
# else
|
36
44
|
path = ::File.join(views_dir, "#{template}.#{engine}")
|
37
|
-
|
45
|
+
[ ::File.read(path), path, 1 ]
|
38
46
|
# @@template_cache[template] = res
|
39
47
|
# res
|
40
48
|
#end
|
41
49
|
when Proc
|
42
|
-
template.call
|
50
|
+
[template.call, template, 1]
|
43
51
|
when String
|
44
|
-
template
|
52
|
+
[template, template, 1]
|
45
53
|
else
|
46
54
|
raise ArgumentError
|
47
55
|
end
|
@@ -53,7 +61,7 @@ module Template
|
|
53
61
|
nil
|
54
62
|
end
|
55
63
|
|
56
|
-
def render_erb(
|
64
|
+
def render_erb(data, options, locals, &block)
|
57
65
|
original_out_buf = @_out_buf
|
58
66
|
data = data.call if data.kind_of? Proc
|
59
67
|
|
@@ -65,4 +73,8 @@ module Template
|
|
65
73
|
@_out_buf, result = original_out_buf, @_out_buf
|
66
74
|
result
|
67
75
|
end
|
76
|
+
|
77
|
+
def render_haml(data, options, locals, &block)
|
78
|
+
::Haml::Engine.new(data, options).render(self, locals, &block)
|
79
|
+
end
|
68
80
|
end
|
data/lib/rambo/env.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# Env handles all the configuration loading, database initialization and class (re)loading
|
2
|
+
# would be nice to allow the user to specify their own init though if they want
|
3
|
+
# Env.new can be called anywhere in any application and therefore acts as a global config
|
1
4
|
module Rambo
|
2
5
|
class Env
|
3
6
|
def self.config
|
@@ -8,12 +11,16 @@ module Rambo
|
|
8
11
|
def initialize
|
9
12
|
begin
|
10
13
|
# TODO: config reload
|
11
|
-
|
14
|
+
|
15
|
+
if dbconf = Env.config['mongodb']
|
16
|
+
require 'mongomapper'
|
17
|
+
@@database ||= MongoMapper.database = dbconf['database']
|
18
|
+
end
|
19
|
+
|
12
20
|
if dbconf = Env.config['datamapper']
|
13
21
|
require 'dm-core'
|
14
22
|
require 'dm-validations'
|
15
23
|
require 'dm-timestamps'
|
16
|
-
#DataMapper.setup(:default, 'mysql://localhost/moo_development')
|
17
24
|
@@connection ||= DataMapper.setup(
|
18
25
|
:default,
|
19
26
|
:adapter => :mysql,
|
@@ -27,38 +34,10 @@ module Rambo
|
|
27
34
|
rescue Exception => e
|
28
35
|
puts "Exception initializing environment: #{e.message}"
|
29
36
|
puts e.backtrace.join("\n")
|
37
|
+
raise e
|
30
38
|
end
|
31
39
|
|
32
|
-
Dir["controller/*.rb"].each { |x| funkyload x }
|
33
|
-
Dir["model/*.rb"].each { |x| funkyload x }
|
34
|
-
Dir["lib/*.rb"].each { |x| funkyload x }
|
35
|
-
Dir["*.rb"].each { |x| funkyload x unless x == 'Rakefile.rb' }
|
36
40
|
end
|
37
41
|
|
38
|
-
private
|
39
|
-
# turn this into a thread that checks every x seconds
|
40
|
-
# (any chance of a callback?) so it is outside of the
|
41
|
-
# request/response cycle
|
42
|
-
def funkyload(file)
|
43
|
-
@@loadcache ||= {}
|
44
|
-
begin
|
45
|
-
if cache = @@loadcache[file]
|
46
|
-
return if Env.config['rambo'] and Env.config['rambo']['reload_classes'] == false
|
47
|
-
if (mtime = File.mtime(file)) > cache
|
48
|
-
puts "rambo: reloading: #{file}"
|
49
|
-
load file
|
50
|
-
@@loadcache[file] = mtime
|
51
|
-
end
|
52
|
-
else
|
53
|
-
#puts "rambo: loading: #{file}"
|
54
|
-
load file
|
55
|
-
@@loadcache[file] = File.mtime(file)
|
56
|
-
end
|
57
|
-
rescue Exception => e
|
58
|
-
puts "Exception loading class [#{file}]: #{e.message}"
|
59
|
-
puts e.backtrace.join("\n")
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
42
|
end
|
64
43
|
end
|
data/lib/rambo/request.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
1
3
|
module Rambo
|
2
4
|
class Request < Rack::Request
|
3
5
|
def user_agent
|
@@ -10,11 +12,11 @@ module Rambo
|
|
10
12
|
end
|
11
13
|
|
12
14
|
def path
|
13
|
-
env["REQUEST_PATH"]
|
15
|
+
@env["REQUEST_PATH"]
|
14
16
|
end
|
15
17
|
|
16
18
|
def uri
|
17
|
-
env["REQUEST_URI"]
|
19
|
+
@env["REQUEST_URI"]
|
18
20
|
end
|
19
21
|
|
20
22
|
def path_components
|
@@ -22,13 +24,21 @@ module Rambo
|
|
22
24
|
end
|
23
25
|
|
24
26
|
def default_controller
|
25
|
-
if Rambo::Env.config['rambo']
|
26
|
-
|
27
|
+
if rambo_conf = Rambo::Env.config['rambo']
|
28
|
+
rambo_conf['default_controller'] || 'home'
|
27
29
|
else
|
28
30
|
'home'
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
34
|
+
def application_context
|
35
|
+
@application_context
|
36
|
+
end
|
37
|
+
|
38
|
+
def application_context=(ctx)
|
39
|
+
@application_context = ctx
|
40
|
+
end
|
41
|
+
|
32
42
|
def controller
|
33
43
|
path_components[1] || default_controller
|
34
44
|
end
|
@@ -36,7 +46,11 @@ module Rambo
|
|
36
46
|
def action
|
37
47
|
path_components[2] || 'index'
|
38
48
|
end
|
39
|
-
|
49
|
+
|
50
|
+
def controller_class
|
51
|
+
self.controller.downcase.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase } + 'Controller'
|
52
|
+
end
|
53
|
+
|
40
54
|
# Override Rack 0.9.x's #params implementation (see #72 in lighthouse)
|
41
55
|
def params
|
42
56
|
self.GET.update(self.POST)
|
@@ -44,4 +58,4 @@ module Rambo
|
|
44
58
|
self.GET
|
45
59
|
end
|
46
60
|
end
|
47
|
-
end
|
61
|
+
end
|
data/lib/rambo/server.rb
CHANGED
@@ -1,52 +1,67 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require 'rubygems'
|
4
|
-
require 'thin'
|
5
|
-
|
6
3
|
require 'rambo/env'
|
7
4
|
require 'rambo/controller'
|
8
5
|
require 'rambo/middleware'
|
9
6
|
require 'rambo/time'
|
10
7
|
require 'rambo/request'
|
8
|
+
require 'rambo/application_request'
|
11
9
|
require 'rambo/response'
|
10
|
+
require 'rambo/application_context'
|
12
11
|
|
12
|
+
# Server simply routes the request to the correct controller/action and returns the result
|
13
13
|
module Rambo
|
14
14
|
class Server
|
15
15
|
|
16
16
|
def initialize(options = {})
|
17
17
|
@options = options
|
18
|
+
env = Rambo::Env.new
|
19
|
+
@contexts = {
|
20
|
+
'default' => Rambo::ApplicationContext.new(),
|
21
|
+
#'blog' => Rambo::ApplicationContext.new('blog')
|
22
|
+
}
|
18
23
|
end
|
19
|
-
|
24
|
+
|
20
25
|
def call(env)
|
21
26
|
begin
|
22
|
-
|
23
|
-
|
27
|
+
|
28
|
+
@contexts.each { |key, context| context.reload } if Rambo::Env.config
|
29
|
+
|
24
30
|
request = Request.new(env)
|
25
31
|
response = Response.new
|
26
|
-
|
27
|
-
|
32
|
+
|
33
|
+
if @contexts.keys.include? request.controller.downcase
|
34
|
+
current_context = @contexts[request.controller.downcase]
|
35
|
+
request = Rambo::ApplicationRequest.new(env)
|
36
|
+
end
|
37
|
+
current_context ||= @contexts['default']
|
38
|
+
request.application_context = current_context
|
39
|
+
|
40
|
+
#puts "rambo: looking for #{request.controller_class}"
|
41
|
+
|
28
42
|
begin
|
29
|
-
|
43
|
+
controller = Object.module_eval("::#{request.controller_class}", __FILE__, __LINE__).new
|
30
44
|
rescue Exception => e
|
31
|
-
return [404, response.header, "<
|
45
|
+
return [404, response.header, "<h2>Routing error: controller <span style='color:grey'>#{request.controller}</span> not found</h2>"]
|
46
|
+
end
|
47
|
+
controller.request = request
|
48
|
+
controller.response = response
|
49
|
+
|
50
|
+
controller.init if controller.respond_to? :init
|
51
|
+
|
52
|
+
unless controller.respond_to? request.action
|
53
|
+
return [404, response.header, "<h2>Routing error: action <span style='color:grey'>#{request.action}</span> not found in <span style='color:grey'>#{request.controller}</span></h2>"]
|
32
54
|
end
|
33
|
-
obj.request = request
|
34
|
-
obj.response = response
|
35
55
|
|
36
|
-
|
56
|
+
result = controller.send(request.action) unless controller.already_rendered?
|
37
57
|
|
38
|
-
#begin
|
39
|
-
result = obj.send(request.action) unless obj.rendered?
|
40
|
-
#rescue Exception => e
|
41
|
-
#return [404, response.header, "<h1>Action #{request.action} Not Found</h1>"]
|
42
|
-
#end
|
43
58
|
response.body = result if result
|
44
59
|
|
45
60
|
[response.status, response.header, response.body]
|
46
61
|
rescue Exception => e
|
47
62
|
puts e.message
|
48
|
-
return [500,
|
63
|
+
return [500, {}, "<pre><b>#{e.message.gsub("<","<")}</b>\n#{e.backtrace.join("\n")}</pre>"]
|
49
64
|
end
|
50
65
|
end
|
51
66
|
end
|
52
|
-
end
|
67
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moomerman-rambo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Taylor
|
@@ -48,10 +48,13 @@ files:
|
|
48
48
|
- lib/rambo/middleware/proxy.rb
|
49
49
|
- lib/rambo/request.rb
|
50
50
|
- lib/rambo/response.rb
|
51
|
+
- lib/rambo/application_request.rb
|
52
|
+
- lib/rambo/application_context.rb
|
51
53
|
- lib/rambo/time.rb
|
52
54
|
- lib/rambo/middleware/upload.rb
|
53
55
|
has_rdoc: false
|
54
56
|
homepage: http://github.com/moomerman/rambo
|
57
|
+
licenses:
|
55
58
|
post_install_message:
|
56
59
|
rdoc_options:
|
57
60
|
- --inline-source
|
@@ -73,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
76
|
requirements: []
|
74
77
|
|
75
78
|
rubyforge_project: rambo
|
76
|
-
rubygems_version: 1.
|
79
|
+
rubygems_version: 1.3.5
|
77
80
|
signing_key:
|
78
81
|
specification_version: 2
|
79
82
|
summary: rambo is an experimental ruby web framework
|