cuca 0.02 → 0.03

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,5 +17,5 @@ To get started:
17
17
  2. See http://cuca.rubyforge.org/ for more information
18
18
 
19
19
 
20
- Questions/Comments/Suggestions are welcome: Email boesemar@rubyforge.org or
20
+ Questions/Comments/Suggestions are welcome: Email boesemar@gmx.de or
21
21
  use the rubyforge cuca project forum.
@@ -11,7 +11,8 @@ class TestWidget < Cuca::Widget
11
11
 
12
12
  def output(*args, &block)
13
13
  @a = @_assigns
14
- mab { text "Debug-Widget: "; br ;
14
+ @params = params
15
+ mab { text "Test-Widget (debug): "; br ;
15
16
  text "ARGS: " + args.inspect ; br ;
16
17
  text "ASSIGNS:" + @a.inspect; br;
17
18
  text "PARAMS: " + @params.inspect ; br;
@@ -0,0 +1,49 @@
1
+ # === conf/config.rb
2
+ # Use this file to set initial cuca framework and application configuration
3
+ # values - Only!
4
+ #
5
+ # You should NOT use this to load additional libraries or to modify cuca classes. Use
6
+ # conf/environment.rb instead.
7
+ #
8
+ # The cuca framework itself isn't fully loaded at this point.
9
+ #
10
+ # === Using Cuca::App.configure:
11
+ #
12
+ # You can pass any information here as you like. Some settings the framework
13
+ # will make use of, but this is also the right place to define application
14
+ # specific settings.
15
+ # You can access these values later anytime with Cuca::App.config[key]
16
+
17
+ Cuca::App.configure do |config|
18
+ ### future use?
19
+ config.log_level = 3
20
+
21
+ ### files within these directories will be automatically 'required' before
22
+ ### your controller script get loaded.
23
+ config.include_directories = %w{_controllers _layouts _models _widgets}
24
+
25
+ ### For pretty url mapping
26
+ # config.magic_action_prefix = '__default_'
27
+
28
+ ### This defines the session cookie definitions
29
+ # config.session_key = 'cuca_session'
30
+ # config.session_prefix = 'cuca.'
31
+ # config.session_valid = 3600*24 # (one day)
32
+
33
+ ### the view generator will look for external templates here:
34
+ # config.view_directory = 'app/_views'
35
+
36
+ ### 404 (file not found) and 500 system error page (relative to the public folder)
37
+ # config.http_404 = '404.html'
38
+ # config.http_500 = '500.html'
39
+
40
+ ### display_errors: Instead of showing a http-500 page this will display an application
41
+ ### trace (similar to php display-errors) on an error event. Switch that off in
42
+ # production systems.
43
+ # config.display_errors = true
44
+
45
+ ### Default mime type to be sent within the http header unless specified by the
46
+ ### Controller
47
+ # config.default_mime_type = 'text/html'
48
+
49
+ end
@@ -1,16 +1,6 @@
1
-
2
- # Use this file to setup your application environment
3
-
4
- Cuca::App.configure do |config|
5
- # config.db_database = 'm3'
6
- # config.db_username = 'postgres'
7
- # config.db_password = ''
8
- # config.db_host = 'localhost'
9
- # config.db_adapter = 'postgresql'
10
- # config.db_database_testing = 'm3_test'
11
-
12
- config.log_level = 3
13
- config.include_directories = %w{_controllers _layouts _models _widgets}
14
- config.magic_action_prefix = '__default_'
15
-
16
- end
1
+ # Use this file to setup your application environment e.g.:
2
+ #
3
+ # - Initialize a 3rd party library
4
+ # - Load additional variables
5
+ # - Open a database handler
6
+ # - ...
@@ -0,0 +1,8 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2
+ <html>
3
+ <head><TITLE>404 - File not found</TITLE></head>
4
+ <body>
5
+ <h1>404 - File not found</h1>
6
+ The requested file could not be located.
7
+ </body>
8
+ </html>
@@ -0,0 +1,9 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2
+ <html>
3
+ <head><TITLE>500 - Server error</TITLE></head>
4
+ <body>
5
+ <h1>500 - Server Error</h1>
6
+ An internal server error happened while processing your request.
7
+ </body>
8
+ </html>
9
+
File without changes
data/lib/cuca/app.rb CHANGED
@@ -6,6 +6,8 @@ module Cuca
6
6
  # Sandbox is used internally run the action script defined by the controller
7
7
  # In future this can be extended to implement some security features etc..
8
8
  class Sandbox
9
+
10
+
9
11
  def self.run(controller_class_name, mod, assigns, request_method, subcall)
10
12
  self.class.send(:include, mod)
11
13
  controller_class = mod::const_get(controller_class_name)
@@ -23,7 +25,7 @@ class Sandbox
23
25
 
24
26
  controller.run_after_filters
25
27
 
26
- return controller.send(:to_s)
28
+ return [controller.http_status, controller.mime_type, controller.to_s]
27
29
  end
28
30
  end
29
31
 
@@ -67,12 +69,14 @@ class App
67
69
  self['include_directories'] = %w{_controllers _widgets _layouts}
68
70
  self['log_level'] = 3
69
71
  self['magic_prefix'] = '__default_'
70
- self['directory_seperator'] = '/' # future?
71
72
  self['session_key'] = 'cuca_session'
72
73
  self['session_prefix'] = 'cuca.'
73
74
  self['session_valid'] = 3600*24
74
- self['view_directory'] = 'app/_view' # where to find views for the view/erb generator
75
-
75
+ self['view_directory'] = 'app/_views' # where to find views for the view/erb generator
76
+ self['http_404'] = '404.html'
77
+ self['http_500'] = '500.html'
78
+ self['default_mime_type'] = 'text/html'
79
+ self['display_errors'] = true
76
80
  end
77
81
 
78
82
  end
@@ -172,10 +176,10 @@ class App
172
176
  @conf['ASSIGNS'] = mapping.assigns
173
177
  @conf['ACTION'] = mapping.action
174
178
  @urlmap = mapping
175
- rescue RoutingError => r
176
- @conf['SCRIPT'] = "#{@conf['PUBLIC_PATH']}/#{@conf['PATH_INFO']}"
177
- return
178
- # raise "RoutingError: #{r}"
179
+
180
+ rescue RoutingError => r # no script found - maybe serve a static file?
181
+ @conf['SCRIPT'] = nil
182
+ return
179
183
  end
180
184
 
181
185
  # get the PATH_TREE for file inclusion!
@@ -213,8 +217,10 @@ class App
213
217
  $conf = @conf
214
218
  $app = self
215
219
 
216
- rescue RuntimeError => e
217
- @cgi.out { "Error initializing the app: #{$!}" }
220
+ # rescue RuntimeError => e
221
+ # @cgi.out { "Error initializing the app: #{$!}" }
222
+ rescue RoutingError => e
223
+ @cgi.out { "Error initializing the app (RoutingError: #{$!})" }
218
224
  end
219
225
 
220
226
  # this has to be public for testing scripts
@@ -238,6 +244,25 @@ class App
238
244
  # end
239
245
  end
240
246
 
247
+
248
+ # this will build an error message depending on configuration
249
+ private
250
+ def get_error(title, exception, show_trace = true, file=nil)
251
+ err = "<h3>#{title}</h3>"
252
+ if (show_trace) then
253
+ exception.backtrace.each do |b|
254
+ err +="<br/>#{b}"
255
+ end
256
+ else
257
+ begin
258
+ err = File.open(file).read
259
+ rescue
260
+ end
261
+ end
262
+ err
263
+ end
264
+
265
+
241
266
  public
242
267
  def cgicall
243
268
  script = @conf['SCRIPT']
@@ -259,6 +284,19 @@ class App
259
284
  return
260
285
  end
261
286
 
287
+ # If config (urlmap) couldn't find a script then let's give up
288
+ # with a file-not-found 404 error
289
+ if script.nil? then
290
+ begin
291
+ file = "#{@conf['PUBLIC_PATH']}/#{Cuca::App.config['http_404']}"
292
+ c = File.open(file).read
293
+ @cgi.out('status' => 'NOT_FOUND', 'type' => 'text/html') { c }
294
+ rescue => e
295
+ @cgi.out('status' => 'NOT_FOUND', 'type' => 'text/html') { "404 - File not found!" }
296
+ end
297
+ return
298
+ end
299
+
262
300
 
263
301
  #
264
302
  # 2nd: Check if we have a script for requested action
@@ -299,25 +337,21 @@ class App
299
337
  #
300
338
  begin
301
339
 
302
- result = Sandbox.run(controller_class_name, @urlmap.action_module, @conf['ASSIGNS'],
303
- $cgi.request_method, @conf['SUBCALL'])
304
-
340
+ (status, mime, content) = Sandbox.run(controller_class_name,
341
+ @urlmap.action_module, @conf['ASSIGNS'],
342
+ $cgi.request_method, @conf['SUBCALL'])
305
343
 
306
- @cgi.out('type' => 'text/html') {result}
344
+ @cgi.out('type' => mime, 'status' => status) {content}
307
345
 
308
346
  rescue Cuca::ApplicationException => e
309
- err = "<b>Application Error: #{e}</b><br/>"
310
- e.backtrace.each do |b|
311
- err +="<br/>#{b}"
312
- end
347
+ err = get_error("Application Error", e,
348
+ Cuca::App.config['display_errors'], Cuca::App.config['http_500'])
313
349
  @cgi.out('status' => 'SERVER_ERROR') { err }
314
- return
350
+ return
315
351
 
316
352
  rescue => e
317
- err = "<b>System Error: #{e}</b><br/>"
318
- e.backtrace.each do |b|
319
- err +="<br/>#{b}"
320
- end
353
+ err = get_error("System Error", e,
354
+ Cuca::App.config['display_errors'], Cuca::App.config['http_500'])
321
355
  @cgi.out('status' => 'SERVER_ERROR') { err }
322
356
  return
323
357
 
data/lib/cuca/const.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cuca
2
- VERSION = '0.02'
2
+ VERSION = '0.03'
3
3
  end
@@ -48,6 +48,28 @@ end
48
48
  #
49
49
  # The magic URL prefix (default: __default_) can be change within App::Config
50
50
  #
51
+ # == Defining a Layouts
52
+ #
53
+ # In most cases a layout is defined on the controller class definition with the
54
+ # layout instruction:
55
+ #
56
+ # class IndexController
57
+ # layout 'standard'
58
+ # end
59
+ #
60
+ # In some cases you want to render a different layout or disable it at all. For
61
+ # this you can call within your action the 'layout' instance method that will
62
+ # temporarily the instruct the controller to render another layout or no layout
63
+ # (if false).
64
+ #
65
+ #
66
+ # == Interrupting the program
67
+ #
68
+ # If you want to stop your program you can call the 'stop' method. Stop has
69
+ # function to do most function, like redirect or setting a different layout or
70
+ # raising an error message.
71
+ #
72
+ #
51
73
  # == Examples
52
74
  #
53
75
  # Hello world (index.rb):
@@ -85,16 +107,39 @@ class Controller < Widget
85
107
  attr_reader :cancel_execution # this can be set by anyone,
86
108
  # methods get/post/run will not be executed
87
109
 
110
+
111
+ # Tells the app what to send to the browser 'text/html'
112
+ def mime_type(mt=nil)
113
+ @_mime_type = mt unless mt.nil?
114
+ @_mime_type
115
+ end
116
+
117
+ # A ruby cgi status type 'OK', 'NOT_FOUND'....to be sent within the http header
118
+ def http_status(hs=nil)
119
+ @_http_status = hs unless hs.nil?
120
+ @_http_status
121
+ end
122
+
88
123
 
89
124
  # get layout
125
+ private # ??
90
126
  def self.def_layout
127
+ # return @_layout unless @_layout.nil?
91
128
  self.run_attr_method('def_layout_name')
92
129
  end
93
130
 
94
- # define a layout
131
+ # define a layout for the Controller
132
+ public
95
133
  def self.layout(name)
96
134
  define_attr_method(:def_layout_name, name)
97
135
  end
136
+
137
+ # define a layout for the current instance
138
+ def layout(name)
139
+ $stderr.puts "Overwriting Layout: #{self.class.def_layout.inspect} with #{name}"
140
+ @_layout = name
141
+ end
142
+
98
143
 
99
144
  private
100
145
  def self.define_filter_method(chain_name, method_name)
@@ -177,11 +222,11 @@ class Controller < Widget
177
222
  # this piece of code will handle a thrown exception BreakControllerException
178
223
  def handle_exception(e)
179
224
  if e.flags.has_key?(:layout) then
180
- self.class.layout(e.flags[:layout])
225
+ @_layout = e.flags[:layout]
181
226
  end
182
227
 
183
228
  if e.flags.has_key?(:redirect) then
184
- self.class.layout false
229
+ @_layout = false
185
230
  to = e.flags[:redirect]
186
231
  clear
187
232
  @_content = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head><title>Redirecting...</title><meta http-equiv=\"REFRESH\" content=\"0;url=#{to}\"></HEAD></HTML>"
@@ -189,7 +234,7 @@ class Controller < Widget
189
234
  end
190
235
 
191
236
  if e.flags.has_key?(:error) then
192
- self.class.layout false
237
+ @_layout = false
193
238
  clear
194
239
  @error_message = e.flags[:error]
195
240
  @cancel_execution = true
@@ -241,13 +286,19 @@ class Controller < Widget
241
286
  get_assigns.each_pair do |k,v|
242
287
  instance_variable_set("@#{k}", v)
243
288
  end
289
+
290
+ @_mime_type = Cuca::App.config['default_mime_type']
291
+ @_http_status = 'OK'
244
292
  end
245
293
 
246
294
  private
247
295
  def load_layout
248
- return nil if self.class.def_layout.nil?
296
+ l = @_layout.nil? ? self.class.def_layout : @_layout
297
+ return nil if (l.nil? || l == false)
298
+ lname = l.capitalize+"Layout"
299
+
249
300
  begin
250
- layout_class = Object::const_get(self.class.def_layout.capitalize+"Layout")
301
+ layout_class = Object::const_get(lname)
251
302
  rescue => e
252
303
  # fixme - if layout loading fails we should display some error
253
304
  return nil
data/lib/cuca/urlmap.rb CHANGED
@@ -23,8 +23,9 @@ end
23
23
  # * assigns - hash with variable assigns from the url (magick prefixes)
24
24
  # * subcall - name of subcall or nil if a normal call was made
25
25
  # * action - Action name (Note: action.capitalize+"Controller" is your controller class name)
26
- # * action_path - Path to the action
27
- # * action_path_full - Full path to application
26
+ # * base_url - Base URL to the action (e.g.: /user/someone/show is -> /user/someone/)
27
+ # * action_path - Path to the action script
28
+ # * action_path_full - Full path to action script
28
29
  # * action_module - The module the action should be loaded into (to avoid name conflicts, depends on action_path)
29
30
  #
30
31
  #
@@ -52,6 +53,7 @@ end
52
53
  #
53
54
  # u.script => '/home/bones/src/cuca_app/app/customer/__customer/show.rb'
54
55
  # u.action => 'show'
56
+ # u.base_url => '/customer/__customer/'
55
57
  # u.assigns => { 'customer' => 'southwind_lda' }
56
58
  # u.action_path => 'customer/southwind_lda/'
57
59
  #
@@ -59,6 +61,7 @@ class URLMap
59
61
  attr_reader :assigns
60
62
  attr_reader :script
61
63
  attr_reader :subcall
64
+ attr_reader :base_url
62
65
  attr_reader :action
63
66
  attr_reader :action_path
64
67
  attr_reader :action_path_full
@@ -138,7 +141,7 @@ class URLMap
138
141
  files = @path_info.split('/')
139
142
 
140
143
  files << '' if @path_info[@path_info.size-1].chr == '/' # add empty element if we point to a directory
141
-
144
+
142
145
  # puts files.inspect
143
146
  real_path = @base_path
144
147
 
@@ -150,6 +153,7 @@ class URLMap
150
153
  real_path = "#{real_path}/#{r}"
151
154
  end
152
155
 
156
+ @base_url = "#{files[0..-2].join('/')}/"
153
157
  @action_path = real_path[@base_path.length..-1]
154
158
  @action_path_full = real_path
155
159
  @action_module = make_module(@action_path)
data/lib/cuca.rb CHANGED
@@ -58,11 +58,18 @@ require 'cuca/app'
58
58
 
59
59
  $LOAD_PATH << $cuca_path+'/lib'
60
60
  begin
61
- require $cuca_path+'/conf/environment'
61
+ require $cuca_path+'/conf/config'
62
62
  rescue LoadError => e
63
- $stderr.puts "WARN: Error loading conf/environment: #{e}"
63
+ $stderr.puts "WARN: Error loading conf/config: #{e}"
64
64
  end
65
65
 
66
66
  require 'cuca/widget'
67
67
  require 'cuca/controller'
68
68
  require 'cuca/layout'
69
+
70
+ begin
71
+ require $cuca_path+'/conf/environment'
72
+ rescue LoadError => e
73
+ $stderr.puts "WARN: Error loading conf/environment: #{e}"
74
+ end
75
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuca
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.02"
4
+ version: "0.03"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Boese
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-05-11 00:00:00 +01:00
12
+ date: 2008-05-18 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -90,9 +90,6 @@ files:
90
90
  - application_skeleton/app/_widgets
91
91
  - application_skeleton/app/_widgets/test.rb
92
92
  - application_skeleton/app/_widgets/sourcecode.rb
93
- - application_skeleton/app/user
94
- - application_skeleton/app/user/__default_username
95
- - application_skeleton/app/user/__default_username/index.rb
96
93
  - application_skeleton/app/_views
97
94
  - application_skeleton/scripts
98
95
  - application_skeleton/scripts/server-lighttpd-fcgi.rb
@@ -100,16 +97,18 @@ files:
100
97
  - application_skeleton/scripts/test.rb
101
98
  - application_skeleton/scripts/server-lighttpd.rb
102
99
  - application_skeleton/scripts/server-webrick.rb
103
- - application_skeleton/scripts/console
104
100
  - application_skeleton/lib
105
101
  - application_skeleton/public
102
+ - application_skeleton/public/404.html
106
103
  - application_skeleton/public/dispatch.fcgi
107
104
  - application_skeleton/public/dispatch.cgi
108
105
  - application_skeleton/public/css
109
106
  - application_skeleton/public/css/style.css
110
107
  - application_skeleton/public/img
111
108
  - application_skeleton/public/img/cuca-seagull.png
109
+ - application_skeleton/public/500.html
112
110
  - application_skeleton/conf
111
+ - application_skeleton/conf/config.rb
113
112
  - application_skeleton/conf/environment.rb
114
113
  has_rdoc: true
115
114
  homepage: http://cuca.rubyforge.org/
@@ -1,7 +0,0 @@
1
- class IndexController < ApplicationController
2
-
3
- def run
4
- content << "User is #{@username}"
5
- end
6
-
7
- end
@@ -1,5 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- libs = " -r irb/completion -r rubygems -r cuca"
4
-
5
- exec "irb #{libs} --simple-prompt"