ramaze 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/Rakefile +4 -12
  2. data/bin/ramaze +95 -171
  3. data/doc/CHANGELOG +387 -4
  4. data/doc/README +81 -13
  5. data/doc/meta/announcement.txt +51 -8
  6. data/doc/meta/configuration.txt +17 -34
  7. data/doc/meta/internals.txt +34 -4
  8. data/doc/migrate/1110_to_1111.txt +131 -0
  9. data/doc/readme_chunks/features.txt +81 -12
  10. data/doc/readme_chunks/installing.txt +0 -1
  11. data/doc/tutorial/todolist.html +293 -65
  12. data/doc/tutorial/{todolist.txt → todolist.mkd} +251 -51
  13. data/examples/blog/main.rb +1 -1
  14. data/examples/blog/src/model.rb +0 -1
  15. data/examples/hello.rb +2 -5
  16. data/examples/templates/template/external.haml +5 -5
  17. data/examples/templates/template/external.liquid +1 -1
  18. data/examples/templates/template/external.mab +8 -8
  19. data/examples/templates/template/external.rem +30 -0
  20. data/examples/templates/template/external.rhtml +6 -7
  21. data/examples/templates/template/external.zmr +13 -9
  22. data/examples/templates/template_amrita2.rb +8 -8
  23. data/examples/templates/template_erubis.rb +11 -11
  24. data/examples/templates/template_ezamar.rb +9 -11
  25. data/examples/templates/template_haml.rb +13 -13
  26. data/examples/templates/template_liquid.rb +10 -10
  27. data/examples/templates/template_markaby.rb +13 -10
  28. data/examples/templates/template_remarkably.rb +59 -0
  29. data/examples/todolist/main.rb +1 -7
  30. data/examples/todolist/src/controller/main.rb +26 -13
  31. data/examples/todolist/src/element/page.rb +5 -0
  32. data/examples/whywiki/main.rb +1 -1
  33. data/lib/proto/main.rb +0 -8
  34. data/lib/proto/public/css/ramaze_error.css +12 -4
  35. data/lib/proto/public/error.zmr +6 -25
  36. data/lib/ramaze.rb +35 -245
  37. data/lib/ramaze/action.rb +21 -0
  38. data/lib/ramaze/adapter.rb +94 -29
  39. data/lib/ramaze/adapter/base.rb +57 -0
  40. data/lib/ramaze/adapter/mongrel.rb +12 -19
  41. data/lib/ramaze/adapter/webrick.rb +21 -20
  42. data/lib/ramaze/cache.rb +47 -3
  43. data/lib/ramaze/cache/memcached.rb +22 -0
  44. data/lib/ramaze/cache/yaml_store.rb +19 -0
  45. data/lib/ramaze/controller.rb +47 -271
  46. data/lib/ramaze/controller/error.rb +43 -0
  47. data/lib/ramaze/controller/render.rb +90 -0
  48. data/lib/ramaze/controller/resolve.rb +147 -0
  49. data/lib/ramaze/dispatcher.rb +41 -9
  50. data/lib/ramaze/dispatcher/file.rb +1 -1
  51. data/lib/ramaze/global.rb +73 -158
  52. data/lib/ramaze/global/dsl.rb +29 -0
  53. data/lib/ramaze/global/globalstruct.rb +90 -0
  54. data/lib/ramaze/helper.rb +1 -1
  55. data/lib/ramaze/helper/aspect.rb +39 -179
  56. data/lib/ramaze/helper/cache.rb +8 -9
  57. data/lib/ramaze/helper/cgi.rb +23 -0
  58. data/lib/ramaze/helper/file.rb +3 -0
  59. data/lib/ramaze/helper/inform.rb +3 -0
  60. data/lib/ramaze/helper/link.rb +56 -63
  61. data/lib/ramaze/helper/nitroform.rb +4 -0
  62. data/lib/ramaze/helper/redirect.rb +1 -1
  63. data/lib/ramaze/inform.rb +6 -2
  64. data/lib/ramaze/inform/analogger.rb +5 -1
  65. data/lib/ramaze/inform/hub.rb +1 -1
  66. data/lib/ramaze/inform/informing.rb +7 -0
  67. data/lib/ramaze/snippets/kernel/aquire.rb +2 -0
  68. data/lib/ramaze/snippets/kernel/constant.rb +2 -0
  69. data/lib/ramaze/snippets/kernel/pretty_inspect.rb +2 -0
  70. data/lib/ramaze/snippets/object/traits.rb +4 -0
  71. data/lib/ramaze/snippets/openstruct/temp.rb +3 -0
  72. data/lib/ramaze/snippets/string/DIVIDE.rb +2 -0
  73. data/lib/ramaze/snippets/string/camel_case.rb +2 -0
  74. data/lib/ramaze/snippets/string/color.rb +2 -0
  75. data/lib/ramaze/snippets/string/each.rb +2 -0
  76. data/lib/ramaze/snippets/string/snake_case.rb +3 -0
  77. data/lib/ramaze/snippets/struct/fill.rb +8 -2
  78. data/lib/ramaze/snippets/struct/values_at.rb +16 -0
  79. data/lib/ramaze/snippets/symbol/to_proc.rb +3 -0
  80. data/lib/ramaze/sourcereload.rb +89 -0
  81. data/lib/ramaze/template.rb +21 -12
  82. data/lib/ramaze/template/amrita2.rb +6 -6
  83. data/lib/ramaze/template/erubis.rb +4 -9
  84. data/lib/ramaze/template/ezamar.rb +13 -57
  85. data/lib/ramaze/template/ezamar/element.rb +10 -12
  86. data/lib/ramaze/template/ezamar/engine.rb +40 -101
  87. data/lib/ramaze/template/ezamar/morpher.rb +3 -3
  88. data/lib/ramaze/template/haml.rb +3 -6
  89. data/lib/ramaze/template/liquid.rb +4 -9
  90. data/lib/ramaze/template/markaby.rb +16 -22
  91. data/lib/ramaze/template/remarkably.rb +28 -0
  92. data/lib/ramaze/tool/mime.rb +2 -0
  93. data/lib/ramaze/tool/record.rb +6 -0
  94. data/lib/ramaze/trinity/request.rb +44 -54
  95. data/lib/ramaze/trinity/response.rb +1 -1
  96. data/lib/ramaze/trinity/session.rb +15 -37
  97. data/lib/ramaze/version.rb +1 -1
  98. data/rake_tasks/gem.rake +2 -2
  99. data/rake_tasks/maintaince.rake +42 -1
  100. data/rake_tasks/spec.rake +45 -0
  101. data/spec/examples/caching.rb +1 -1
  102. data/spec/examples/simple.rb +1 -1
  103. data/spec/examples/templates/template_amrita2.rb +1 -0
  104. data/spec/examples/templates/template_erubis.rb +2 -1
  105. data/spec/examples/templates/template_ezamar.rb +1 -1
  106. data/spec/examples/templates/template_haml.rb +2 -1
  107. data/spec/examples/templates/template_liquid.rb +2 -1
  108. data/spec/examples/templates/template_markaby.rb +2 -1
  109. data/spec/examples/templates/template_remarkably.rb +22 -0
  110. data/spec/examples/todolist.rb +125 -0
  111. data/spec/helper.rb +2 -23
  112. data/spec/helper/minimal.rb +20 -0
  113. data/spec/helper/mock_http.rb +24 -30
  114. data/spec/helper/simple_http.rb +2 -2
  115. data/spec/helper/wrap.rb +6 -9
  116. data/spec/ramaze/adapter.rb +1 -1
  117. data/spec/ramaze/adapter/record.rb +31 -0
  118. data/spec/ramaze/cache.rb +41 -54
  119. data/spec/ramaze/controller.rb +121 -137
  120. data/spec/ramaze/controller/template/list.xhtml +1 -0
  121. data/spec/ramaze/controller/template/other/greet/other.xhtml +1 -0
  122. data/spec/ramaze/controller/template_resolving.rb +27 -3
  123. data/spec/ramaze/element.rb +11 -7
  124. data/spec/ramaze/error.rb +1 -1
  125. data/spec/ramaze/gestalt.rb +2 -0
  126. data/spec/ramaze/helper/aspect.rb +30 -21
  127. data/spec/ramaze/helper/auth.rb +1 -1
  128. data/spec/ramaze/helper/cache.rb +2 -1
  129. data/spec/ramaze/helper/form.rb +14 -11
  130. data/spec/ramaze/helper/link.rb +18 -41
  131. data/spec/ramaze/localize.rb +29 -2
  132. data/spec/ramaze/morpher.rb +23 -12
  133. data/spec/ramaze/params.rb +46 -24
  134. data/spec/ramaze/request.rb +6 -2
  135. data/spec/ramaze/store/yaml.rb +5 -0
  136. data/spec/ramaze/template.rb +22 -27
  137. data/spec/ramaze/template/amrita2.rb +1 -2
  138. data/spec/ramaze/template/erubis.rb +1 -1
  139. data/spec/ramaze/template/ezamar.rb +1 -2
  140. data/spec/ramaze/template/haml.rb +2 -2
  141. data/spec/ramaze/template/haml/with_vars.haml +1 -1
  142. data/spec/ramaze/template/liquid.rb +1 -1
  143. data/spec/ramaze/template/markaby.rb +1 -1
  144. data/spec/ramaze/template/remarkably.rb +56 -0
  145. data/spec/ramaze/template/remarkably/external.rem +8 -0
  146. data/spec/ramaze/template/remarkably/sum.rem +1 -0
  147. metadata +38 -63
  148. data/doc/README.html +0 -637
  149. data/doc/allison/LICENSE +0 -184
  150. data/doc/allison/README +0 -37
  151. data/doc/allison/allison.css +0 -299
  152. data/doc/allison/allison.gif +0 -0
  153. data/doc/allison/allison.js +0 -307
  154. data/doc/allison/allison.rb +0 -287
  155. data/doc/allison/cache/BODY +0 -588
  156. data/doc/allison/cache/CLASS_INDEX +0 -4
  157. data/doc/allison/cache/CLASS_PAGE +0 -1
  158. data/doc/allison/cache/FILE_INDEX +0 -4
  159. data/doc/allison/cache/FILE_PAGE +0 -1
  160. data/doc/allison/cache/FONTS +0 -1
  161. data/doc/allison/cache/FR_INDEX_BODY +0 -1
  162. data/doc/allison/cache/IMGPATH +0 -1
  163. data/doc/allison/cache/INDEX +0 -1
  164. data/doc/allison/cache/JAVASCRIPT +0 -307
  165. data/doc/allison/cache/METHOD_INDEX +0 -4
  166. data/doc/allison/cache/METHOD_LIST +0 -1
  167. data/doc/allison/cache/SRC_PAGE +0 -1
  168. data/doc/allison/cache/STYLE +0 -321
  169. data/doc/allison/cache/URL +0 -1
  170. data/doc/changes.txt +0 -3375
  171. data/doc/changes.xml +0 -3378
  172. data/examples/todolist/conf/benchmark.yaml +0 -35
  173. data/examples/todolist/conf/debug.yaml +0 -34
  174. data/examples/todolist/conf/live.yaml +0 -33
  175. data/examples/todolist/conf/silent.yaml +0 -31
  176. data/examples/todolist/conf/stage.yaml +0 -33
  177. data/examples/todolist/public/css/coderay.css +0 -105
  178. data/examples/todolist/public/css/ramaze_error.css +0 -42
  179. data/lib/proto/conf/benchmark.yaml +0 -21
  180. data/lib/proto/conf/debug.yaml +0 -21
  181. data/lib/proto/conf/live.yaml +0 -21
  182. data/lib/proto/conf/silent.yaml +0 -21
  183. data/lib/proto/conf/stage.yaml +0 -21
  184. data/lib/proto/public/css/coderay.css +0 -105
  185. data/lib/ramaze/http_status.rb +0 -66
  186. data/lib/ramaze/snippets/hash/keys_to_sym.rb +0 -19
  187. data/lib/ramaze/snippets/kernel/method.rb +0 -26
  188. data/lib/ramaze/snippets/method/name.rb +0 -22
  189. data/lib/ramaze/snippets/ramaze/autoreload.rb +0 -135
  190. data/lib/ramaze/snippets/rdoc/usage_no_exit.rb +0 -65
  191. data/spec/all.rb +0 -32
  192. data/spec/ramaze/conf/locale_de.yaml +0 -6
  193. data/spec/ramaze/conf/locale_en.yaml +0 -6
  194. data/spec/ramaze/dependencies.rb +0 -16
  195. data/spec/ramaze/global.rb +0 -44
@@ -0,0 +1,59 @@
1
+ # Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
2
+ # All files in this distribution are subject to the terms of the Ruby license.
3
+
4
+ require 'ramaze'
5
+
6
+ include Ramaze
7
+
8
+ class MainController < Controller
9
+ template_root File.expand_path((File.dirname(__FILE__)/'template'))
10
+ trait :engine => Template::Remarkably
11
+
12
+ include Remarkably::Common
13
+
14
+ def index
15
+ %{ #{A 'Home', :href => :/} | #{A(:internal)} | #{A(:external)} }
16
+ end
17
+
18
+ def internal *args
19
+ @place = :internal
20
+ html do
21
+ head do
22
+ title "Template::Remarkably internal"
23
+ end
24
+ body do
25
+ h1 "The #@place Template for Remarkably"
26
+ a("Home", :href => R(:/))
27
+ P do
28
+ text "Here you can pass some stuff if you like, parameters are just passed like this:"
29
+ br
30
+ a("#@place/one", :href => Rs( @place, :one))
31
+ br
32
+ a("#@place/one/two/three", :href => Rs( @place, :one, :two, :three))
33
+ br
34
+ a("#@place/one?foo=bar", :href => Rs( @place, :one, :foo => :bar))
35
+ br
36
+ end
37
+ div do
38
+ text "The arguments you have passed to this action are:"
39
+ if args.empty?
40
+ "none"
41
+ else
42
+ args.each do |arg|
43
+ span arg
44
+ end
45
+ end
46
+ end
47
+ div request.params.inspect
48
+ end
49
+ end
50
+ end
51
+
52
+ def external *args
53
+ @args = args
54
+ @place = :external
55
+ @request = request
56
+ end
57
+ end
58
+
59
+ Ramaze.start
@@ -9,10 +9,4 @@ require 'src/controller/main'
9
9
  require 'src/element/page'
10
10
  require 'src/model'
11
11
 
12
- # mode selects the config-file.
13
- # benchmark | debug | stage | live | silent
14
- mode = 'debug'
15
-
16
- Global.setup YAML.load_file("conf/#{mode}.yaml")
17
-
18
- start
12
+ Ramaze.start
@@ -3,53 +3,66 @@
3
3
 
4
4
  class MainController < Controller
5
5
 
6
- trait :engine => Template::Ezamar
6
+ helper :aspect
7
7
 
8
8
  def index
9
9
  @tasks = []
10
10
  TodoList.original.each do |title, parameters|
11
11
  if parameters[:done]
12
12
  status = 'done'
13
- toggle = link( R( self, :open, CGI.escape(title) ), :title => 'Open Task' )
13
+ toggle = A('Open Task', :href => Rs(:open, title))
14
14
  else
15
15
  status = 'not done'
16
- toggle = link( R( self, :close, CGI.escape(title) ), :title => 'Close Task' )
16
+ toggle = A('Close Task', :href => Rs(:close, title))
17
17
  end
18
- delete = link( R( self, :delete, CGI.escape(title) ), :title => 'Delete' )
18
+ delete = A('Delete', :href => Rs(:delete, title))
19
19
  @tasks << [title, status, toggle, delete]
20
20
  end
21
21
  @tasks.sort!
22
22
  end
23
23
 
24
24
  def create
25
- title = request['title']
26
- TodoList[title] = {:done => false}
27
- redirect R(self)
25
+ if title = request['title']
26
+ title.strip!
27
+ if title.empty?
28
+ error("Please enter a title")
29
+ redirect '/new'
30
+ end
31
+ TodoList[title] = {:done => false}
32
+ end
28
33
  end
29
34
 
30
35
  def open title
31
36
  task_status title, false
32
- redirect R(self)
33
37
  end
34
38
 
35
39
  def close title
36
40
  task_status title, true
37
- redirect R(self)
38
41
  end
39
42
 
40
43
  def delete title
41
44
  TodoList.delete title
42
- redirect R(self)
43
45
  end
44
46
 
45
- def error
46
- @foo = 'bar'
47
+ after(:create, :open, :close, :delete){ redirect_index }
48
+
49
+ def redirect_index
50
+ redirect(Rs())
47
51
  end
48
52
 
49
53
  private
50
54
 
55
+ def error(message)
56
+ flash[:error] = message
57
+ end
58
+
51
59
  def task_status title, status
52
- task = TodoList[title]
60
+ p title
61
+ unless task = TodoList[title]
62
+ error "No such Task: `#{title}'"
63
+ redirect_referer
64
+ end
65
+
53
66
  task[:done] = status
54
67
  TodoList[title] = task
55
68
  end
@@ -18,6 +18,11 @@ class Page < Ezamar::Element
18
18
  </head>
19
19
  <body>
20
20
  <h1>#{@title}</h1>
21
+ <?r if flash[:error] ?>
22
+ <div class="error">
23
+ \#{flash[:error]}
24
+ </div>
25
+ <?r end ?>
21
26
  #{content}
22
27
  </body>
23
28
  </html>
@@ -48,4 +48,4 @@ Global.adapter = :mongrel
48
48
  #Global.tidy = true
49
49
  Global.mapping = {'/' => WikiController}
50
50
 
51
- start
51
+ Ramaze.start
@@ -8,11 +8,3 @@ include Ramaze
8
8
  require 'src/controller/main'
9
9
  require 'src/element/page'
10
10
  require 'src/model'
11
-
12
- # mode selects the config-file.
13
- # benchmark | debug | stage | live | silent
14
- mode = 'debug'
15
-
16
- Global.setup YAML.load_file("conf/#{mode}.yaml")
17
-
18
- start
@@ -1,7 +1,3 @@
1
- img#logo {
2
- float:left;
3
- }
4
-
5
1
  h1.main {
6
2
  text-align: center;
7
3
  }
@@ -40,3 +36,15 @@ tr.source_container div table tr.source {
40
36
  div.source {
41
37
  background: #fff;
42
38
  }
39
+
40
+ tr.source {
41
+ font-size: 1.0em;
42
+ }
43
+
44
+ tr.source td.lineno {
45
+ font-size: 0.8em;
46
+ }
47
+
48
+ tr.source td.code pre {
49
+ height: 0.1em;
50
+ }
@@ -1,17 +1,10 @@
1
1
  <html>
2
2
  <head>
3
3
  <title>#{@title}</title>
4
- <?r if @coderay ?>
5
- <style>
6
- #{CodeRay::Encoders[:html]::CSS.new.stylesheet}
7
- </style>
8
- <?r end ?>
9
4
  <link rel="stylesheet" href="/css/ramaze_error.css" />
10
5
  <script type="text/javascript" src="/js/jquery.js"></script>
11
6
  </head>
12
7
  <body>
13
- <img id="logo" alt="Ramaze" src="/ramaze.png" />
14
-
15
8
  <h1>#{@title}</h1>
16
9
 
17
10
  <table class="main">
@@ -27,25 +20,13 @@
27
20
  <div class="source">
28
21
  <table style="width:100%;">
29
22
  <tr>
30
- <td colspan="2">vim #{file} +#{lineno}</td>
23
+ <td colspan="2">#{@editor} #{file} +#{lineno}</td>
31
24
  </tr>
32
- <?r
33
- if @coderay
34
- code = lines.map{|no, code, cur| code}.join("\n")
35
- options = {
36
- :wrap => :div,
37
- :line_numbers => :inline,
38
- :line_number_start => (lineno.to_i - Global.backtrace_size)
39
- }
40
- ?>
41
- #{CodeRay.scan(code, :ruby).html(options)}
42
- <?r else ?>
43
- <?r lines.each do |llineno, lcode, lcurrent| ?>
44
- <tr class="source" #{'style="background:#faa;"' if lcurrent}>
45
- <td>#{llineno}</td>
46
- <td><pre>#{lcode.to_s.rstrip}</pre></td>
47
- </tr>
48
- <?r end ?>
25
+ <?r lines.each do |llineno, lcode, lcurrent| ?>
26
+ <tr class="source" #{'style="background:#faa;"' if lcurrent}>
27
+ <td class="lineno">#{llineno}</td>
28
+ <td class="code"><pre>#{lcode.to_s.rstrip}</pre></td>
29
+ </tr>
49
30
  <?r end ?>
50
31
  </table>
51
32
  </div>
@@ -4,276 +4,66 @@
4
4
  # The main namespace for Ramaze
5
5
  module Ramaze
6
6
  BASEDIR = File.dirname(File.expand_path(__FILE__))
7
+ $:.unshift BASEDIR
7
8
  end
8
9
 
9
- $:.unshift Ramaze::BASEDIR
10
-
11
- require 'timeout'
12
- require 'ostruct'
13
- require 'socket'
14
- require 'yaml'
15
- require 'set'
16
- require 'pp'
10
+ Thread.abort_on_exception = true
17
11
 
12
+ # Bootstrap
13
+ require 'ramaze/version'
18
14
  require 'ramaze/snippets'
19
- require 'ramaze/error'
20
- require 'ramaze/cache'
21
15
  require 'ramaze/inform'
22
16
  require 'ramaze/global'
23
- require 'ramaze/dispatcher'
24
- require 'ramaze/helper'
17
+ require 'ramaze/cache'
18
+
19
+ # Startup
25
20
  require 'ramaze/controller'
26
- require 'ramaze/template/ezamar'
27
- require 'ramaze/http_status'
28
- require 'ramaze/gestalt'
29
- require 'ramaze/version'
21
+ require 'ramaze/adapter'
22
+ require 'ramaze/sourcereload'
30
23
 
31
- Thread.abort_on_exception = true
24
+ # Complete
25
+ require 'ramaze/dispatcher'
26
+ require 'ramaze/template/ezamar'
32
27
 
33
28
  module Ramaze
34
29
 
35
- # This initializes all the other stuff, Controller, Adapter and Global
36
- # which in turn kickstart Ramaze into duty.
37
- # additionally it starts up the autoreload , which reloads all the stuff every
38
- # second in case it has changed.
39
- # please note that Ramaze will catch SIGINT (^C) and kill the running adapter
40
- # at that event, this provides a nice and clean way to shut down.
41
- #
42
- # The start might be a bit odd, but i think it is a quite decent hack, what
43
- # it does is following.
44
- # It looks up who called the start-method, and if the method that called it
45
- # is not the file that was run first, it will return without doing anything
46
- # The use of this is:
47
- # If you start ramaze over the CLI, using ruby main.rb
48
- # and the start-method is called in your main.rb, it will run, using the
49
- # configuration from you without any modifcation (just with the defaults)
50
- # In case you start it via the ramaze-command however, you may pass options
51
- # that could be overwritten by your application, but you just want to use
52
- # these options temporarily - in this case the start-call from your main.rb
53
- # will simply be ignored, the start from bin/ramaze is invoked which in turn
54
- # sets the options you passed on the commandline.
55
- # If you pertout want to run with bin/ramaze, pass commandline-options _and_
56
- # use the start-method your own application, use start(:force => true)
57
- # This also applies if your start is in another file than the file you
58
- # called first, therefor giving you the option to layout your application
59
- # as it pleases you.
60
-
61
- def start options = {}
62
- starter = caller[0].split(':').first
63
- return unless ($0 == starter or options.delete(:force))
64
-
65
- init_global options
66
-
67
- return if options.delete(:fake_start)
68
-
69
- startup
70
- end
71
-
72
- alias run start
30
+ # Each of these classes will be called ::startup upon Ramaze.startup
73
31
 
74
- # same as start(:force => true)
32
+ trait :essentials => [
33
+ Global, Controller, Session, SourceReload, Adapter
34
+ ]
75
35
 
76
- def force_start(options = {})
77
- start options.merge(:force => true)
78
- end
79
-
80
- alias force_run force_start
81
-
82
- # Execute the tasks specified in Global.startup
83
- # (where you can define your own tasks)
84
- # and afterwards the ones in Global.ramaze_startup
85
- # (reserved for the usage of Ramaze itself)
86
-
87
- def startup
88
- tasks = Global.startup + Global.ramaze_startup
89
- execute(*tasks)
90
- end
36
+ class << self
91
37
 
92
- # Execute the tasks specified in Global.shutdown
93
- # (where you can define your own tasks)
94
- # and afterwards the ones in Global.ramaze_shutdown
95
- # (reserved for the usage of Ramaze itself)
38
+ # The one place to start Ramaze, takes an Hash of options to pass on to
39
+ # each class in trait[:essentials] by calling ::startup on them.
96
40
 
97
- def shutdown
98
- tasks = Global.shutdown + Global.ramaze_shutdown
99
- execute(*tasks)
100
- end
101
-
102
- # executes a list of tasks, depending on the task-object, if it responds to
103
- # :call it will be called upon, otherwise the task is sent to self ( the
104
- # module Ramaze ).
105
-
106
- def execute *tasks
107
- tasks.flatten.each do |task|
108
- begin
109
- if task.respond_to?(:call)
110
- task.call
111
- else
112
- send(task)
113
- end
114
- rescue Object => ex
115
- exit if ex.is_a?(SystemExit)
116
- Inform.error(ex)
117
- end
118
- end
119
- end
41
+ def startup options = {}
42
+ Inform.info("Starting up Ramaze (Version #{VERSION})")
120
43
 
121
- # kill all threads except Thread.main before #shutdown
44
+ starter = caller[0].split(':').first
45
+ return unless ($0 == starter or options.delete(:force))
122
46
 
123
- def kill_threads
124
- Inform.info("Killing the Threads")
125
- Global.adapter_klass.stop rescue nil
126
- (Thread.list - [Thread.main]).each do |thread|
127
- Timeout.timeout(2) do
128
- if thread[:adapter] and thread[:adapter].respond_to?(:shutdown)
129
- thread[:adapter].shutdown
130
- end
131
- thread.kill
47
+ trait[:essentials].each do |obj|
48
+ obj.startup(options)
132
49
  end
133
50
  end
134
- end
135
51
 
136
- # Setup the Controllers
137
- # This autogenerates a mapping and does some munging/validation on the way
52
+ # This will be called when you hit ^C or send SIGINT.
53
+ # It sends ::shutdown to every class in trait[:essentials] and informs you
54
+ # when it is done
138
55
 
139
- def setup_controllers
140
- Global.mapping ||= {}
141
-
142
- Global.mapping.dup.each do |route, controller|
143
- Global.mapping[route] = constant(controller.to_s)
144
- end
145
-
146
- Global.controllers.each do |controller|
147
- if map = controller.mapping
148
- Global.mapping[map] ||= controller
149
- Inform.debug("mapped #{map} => #{controller}")
56
+ def shutdown
57
+ trait[:essentials].each do |obj|
58
+ obj.shutdown if obj.respond_to?(:shutdown)
150
59
  end
151
- end
152
-
153
- Inform.debug("found Controllers: #{Global.controllers.inspect}")
154
- Inform.debug("mapped Controllers: #{Global.mapping.inspect}")
155
- end
156
60
 
157
- # Initialize the Kernel#autoreload with the value of Global.autoreload
61
+ puts("Shutdown Ramaze (it's safe to kill me now if i hang)")
158
62
 
159
- def init_autoreload
160
- return unless Global.autoreload
161
- Ramaze.autoreload Global.autoreload
162
- end
163
-
164
- # initialize the Global, setting a default-mapping if none is given yet.
165
- #
166
- # You may pass :force_setup => true in your options if you want your options
167
- # to override everything else set till now.
168
-
169
- def init_global options = {}
170
- tmp_mapping = Global.mapping || {}
171
-
172
- if options.delete(:force_setup)
173
- Global.setup(options)
174
- else
175
- Global.update(options)
176
- end
177
-
178
- Global.mapping = tmp_mapping.merge(Global.mapping)
179
- end
180
-
181
- # Finally decide wether to use a main-thread to run Ramaze
182
- # so that further stuff can be done outside (very useful for testcases)
183
- # or we run it in standalone-mode, which is the default and waits
184
- # until the adapter is finished. (hopefully never ;)
185
- # change this behaviour by setting Global.run_loose = (true|false)
186
- # In every case the running adapter-thread is assigned to
187
- # Global.running_adapter
188
-
189
- def init_adapter
190
- unless Global.adapter
191
- Inform.info "No adapter will be used."
192
- return
193
- end
194
-
195
- (Thread.list - [Thread.current]).each do |thread|
196
- thread.kill if thread[:task] == :adapter
197
- end
198
-
199
- Thread.new do
200
- Thread.current.priority = 99
201
- Thread.current[:task] = :adapter
202
- Global.running_adapter = run_adapter
203
-
204
- trap(Global.shutdown_trap){ shutdown } rescue nil
205
- end
206
-
207
- Timeout.timeout(3) do
208
- sleep 0.1 until Global.running_adapter
209
- end
210
- Global.running_adapter.join unless Global.run_loose
211
- rescue Object => ex
212
- Inform.debug(ex.message) unless ex.is_a? Interrupt
213
- shutdown
214
- end
215
-
216
- # This first picks the right adapter according to Global.adapter
217
- # It also looks for Global.host and Global.port and passes it on
218
- # to the class-method of the adapter ::start
219
-
220
- def run_adapter
221
- adapter, host, port = Global.values_at(:adapter, :host, :port)
222
- require_adapter(adapter)
223
-
224
- adapter_klass = Ramaze::Adapter.const_get(adapter.to_s.capitalize)
225
- Global.adapter_klass = adapter_klass
226
-
227
- Inform.info("Found adapter: #{adapter_klass}, trying to connect to #{host}:#{port} ...")
228
-
229
- parse_port(port)
230
- shutdown unless test_connections(host, Global.ports)
231
- Inform.info("and we're running: #{host}:#{port}")
232
-
233
- adapter_klass.start host, Global.ports
234
- end
235
-
236
- # require the specified adapter from 'ramaze/adapter/name.to_s.downcase'
237
-
238
- def require_adapter adapter
239
- require "ramaze/adapter" / adapter.to_s.downcase
240
- rescue LoadError => ex
241
- Inform.error(ex)
242
- puts "Please make sure you have an adapter called #{adapter}"
243
- shutdown
244
- end
245
-
246
- # convert ports given as string (7000..7007) to an actual range.
247
- # sets Global.port to the first of the ports given
248
- # sets Global.ports to the range (if one port given just a range from
249
- # that to the same (7000..7000)
250
-
251
- def parse_port port
252
- from_port, to_port = port.to_s.split('..')
253
- if from_port and to_port
254
- Global.ports = (from_port.to_i..to_port.to_i)
255
- else
256
- Global.ports = (from_port.to_i..from_port.to_i)
63
+ exit!
257
64
  end
258
- Global.port = Global.ports.begin
259
- end
260
-
261
- # test if a connection can be made at the specified host/ports.
262
65
 
263
- def test_connections host, ports
264
- return true unless Global.test_connections
265
- ports.map{|port| connection_possible(host, port) }.all?
266
- end
267
-
268
- # check connectivity to a specific host/port
269
-
270
- def connection_possible host, port
271
- Timeout.timeout(1) do
272
- TCPServer.open(host, port){ true }
273
- end
274
- rescue => ex
275
- Inform.error(ex)
276
- false
66
+ alias start startup
67
+ alias stop shutdown
277
68
  end
278
- extend self
279
69
  end