ramaze 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. data/Rakefile +14 -29
  2. data/bin/ramaze +2 -3
  3. data/doc/AUTHORS +5 -2
  4. data/doc/CHANGELOG +262 -9
  5. data/doc/FAQ +6 -6
  6. data/doc/meta/announcement.txt +5 -19
  7. data/doc/tutorial/todolist.html +47 -57
  8. data/doc/tutorial/todolist.mkd +47 -55
  9. data/examples/memleak_detector.rb +31 -0
  10. data/examples/todolist/src/controller/main.rb +14 -13
  11. data/examples/todolist/src/element/page.rb +2 -2
  12. data/examples/todolist/src/model.rb +2 -2
  13. data/examples/todolist/todolist.db +0 -4
  14. data/examples/whywiki/main.rb +2 -2
  15. data/examples/whywiki/template/edit.xhtml +1 -1
  16. data/examples/whywiki/template/show.xhtml +3 -3
  17. data/lib/proto/src/controller/main.rb +18 -1
  18. data/lib/proto/template/index.xhtml +11 -2
  19. data/lib/ramaze.rb +1 -1
  20. data/lib/ramaze/action.rb +104 -5
  21. data/lib/ramaze/action/render.rb +54 -0
  22. data/lib/ramaze/adapter.rb +2 -1
  23. data/lib/ramaze/adapter/mongrel.rb +13 -4
  24. data/lib/ramaze/cache.rb +17 -8
  25. data/lib/ramaze/cache/memcached.rb +1 -5
  26. data/lib/ramaze/controller.rb +51 -18
  27. data/lib/ramaze/controller/resolve.rb +19 -14
  28. data/lib/ramaze/dispatcher.rb +13 -16
  29. data/lib/ramaze/dispatcher/action.rb +2 -3
  30. data/lib/ramaze/dispatcher/error.rb +8 -3
  31. data/lib/ramaze/dispatcher/file.rb +1 -4
  32. data/lib/ramaze/error.rb +5 -5
  33. data/lib/ramaze/global.rb +7 -1
  34. data/lib/ramaze/global/globalstruct.rb +1 -3
  35. data/lib/ramaze/helper/aspect.rb +8 -10
  36. data/lib/ramaze/helper/cgi.rb +21 -3
  37. data/lib/ramaze/helper/identity.rb +4 -6
  38. data/lib/ramaze/helper/link.rb +4 -4
  39. data/lib/ramaze/helper/pager.rb +316 -0
  40. data/lib/ramaze/helper/partial.rb +37 -0
  41. data/lib/ramaze/helper/stack.rb +1 -1
  42. data/lib/ramaze/inform.rb +9 -0
  43. data/lib/ramaze/inform/hub.rb +5 -0
  44. data/lib/ramaze/inform/informer.rb +12 -6
  45. data/lib/ramaze/inform/informing.rb +32 -7
  46. data/lib/ramaze/inform/knotify.rb +21 -0
  47. data/lib/ramaze/inform/xosd.rb +58 -24
  48. data/lib/ramaze/sourcereload.rb +30 -1
  49. data/lib/ramaze/template.rb +33 -12
  50. data/lib/ramaze/template/amrita2.rb +21 -20
  51. data/lib/ramaze/template/erubis.rb +18 -14
  52. data/lib/ramaze/template/ezamar.rb +15 -26
  53. data/lib/ramaze/template/ezamar/element.rb +1 -1
  54. data/lib/ramaze/template/ezamar/engine.rb +45 -36
  55. data/lib/ramaze/template/ezamar/morpher.rb +3 -3
  56. data/lib/ramaze/template/ezamar/render_partial.rb +26 -0
  57. data/lib/ramaze/template/haml.rb +23 -18
  58. data/lib/ramaze/template/liquid.rb +5 -3
  59. data/lib/ramaze/template/markaby.rb +14 -11
  60. data/lib/ramaze/template/remarkably.rb +11 -5
  61. data/lib/ramaze/tool/localize.rb +12 -4
  62. data/lib/ramaze/tool/tidy.rb +26 -23
  63. data/lib/ramaze/trinity/request.rb +11 -7
  64. data/lib/ramaze/trinity/session.rb +24 -8
  65. data/lib/ramaze/version.rb +1 -1
  66. data/rake_tasks/maintaince.rake +136 -11
  67. data/spec/examples/templates/template_liquid.rb +6 -3
  68. data/spec/examples/todolist.rb +1 -2
  69. data/spec/helper/minimal.rb +7 -7
  70. data/spec/ramaze/action/basics.rb +19 -0
  71. data/spec/ramaze/action/render.rb +18 -0
  72. data/spec/ramaze/controller.rb +1 -1
  73. data/spec/ramaze/controller/template_resolving.rb +1 -1
  74. data/spec/ramaze/dispatcher/file.rb +24 -0
  75. data/spec/ramaze/error.rb +28 -29
  76. data/spec/ramaze/helper/cgi.rb +43 -0
  77. data/spec/ramaze/helper/pager.rb +27 -0
  78. data/spec/ramaze/helper/partial.rb +38 -0
  79. data/spec/ramaze/helper/template/partial.xhtml +1 -0
  80. data/spec/ramaze/inform/informer.rb +1 -1
  81. data/spec/ramaze/localize.rb +1 -1
  82. data/spec/ramaze/morpher.rb +3 -3
  83. data/spec/ramaze/request.rb +1 -3
  84. data/spec/ramaze/template.rb +9 -7
  85. data/spec/ramaze/template/haml.rb +2 -1
  86. metadata +21 -7
  87. data/examples/todolist/public/404.jpg +0 -0
  88. data/examples/todolist/public/error.xhtml +0 -74
  89. data/lib/ramaze/controller/render.rb +0 -90
@@ -3,27 +3,31 @@
3
3
 
4
4
  require 'erubis'
5
5
 
6
- module Ramaze::Template
6
+ module Ramaze
7
+ module Template
7
8
 
8
- # Is responsible for compiling a template using the Erubis templating engine.
9
+ # Is responsible for compiling a template using the Erubis templating engine.
9
10
 
10
- class Erubis < Template
11
+ class Erubis < Template
11
12
 
12
- Ramaze::Controller.register_engine self, %w[ rhtml ]
13
+ ENGINES[self] = %w[ rhtml ]
13
14
 
14
- class << self
15
+ class << self
15
16
 
16
- # Takes a controller and the options :action, :parameter, :file and :binding
17
- #
18
- # Builds a template out of the method on the controller and the
19
- # template-file.
17
+ # Entry-point for Action#render
20
18
 
21
- def transform action
22
- template = reaction_or_file(action)
19
+ def transform action
20
+ eruby = wrap_compile(action)
21
+ eruby.result(action.binding)
22
+ end
23
23
 
24
- eruby = ::Erubis::Eruby.new(template)
25
- eruby.init_evaluator(:filename => (action.template || __FILE__))
26
- eruby.result(action.binding)
24
+ # Creates an instance of ::Erubis::Eruby, sets the filename
25
+ # from the template and returns the instance.
26
+ def compile(action, template)
27
+ eruby = ::Erubis::Eruby.new(template)
28
+ eruby.init_evaluator(:filename => (action.template || __FILE__))
29
+ eruby
30
+ end
27
31
  end
28
32
  end
29
33
  end
@@ -10,41 +10,30 @@ module Ramaze
10
10
 
11
11
  class Ezamar < Template
12
12
 
13
- Ramaze::Controller.register_engine self, %w[ xhtml zmr ]
13
+ ENGINES[self] = %w[ xhtml zmr ]
14
14
 
15
- trait :transform_pipeline => [
16
- ::Ezamar::Element, ::Ezamar::Template
17
- ]
18
-
19
- trait :actionless => true
15
+ TRANSFORM_PIPELINE = [ ::Ezamar::Element ]
20
16
 
21
17
  class << self
22
18
 
23
- # Takes a controller and the options :action, :parameter, :file and :binding
24
- #
25
- # Uses Ezamar::Template to compile the template.
26
-
19
+ # Transforms an action into the XHTML code for parsing and returns
20
+ # the result
27
21
  def transform action
28
- template = reaction_or_file(action)
29
- file = (action.template || __FILE__)
30
- pipeline(template.to_s, action.binding, file)
22
+ ezamar = wrap_compile(action)
23
+ ezamar.result(action.binding)
31
24
  end
32
25
 
33
- # go through the pipeline and call #transform on every object found there,
34
- # passing the template at that point.
35
- # the order and contents of the pipeline are determined by an array
36
- # in trait[:template_pipeline]
37
- # the default being [Element, Morpher, self]
38
- #
39
- # TODO
40
- # - put the pipeline into the Controller for use with all templates.
41
-
42
- def pipeline(template, binding, file)
43
- class_trait[:transform_pipeline].each do |klass|
44
- template = klass.transform(template, binding, file)
26
+ # Compile a template, applying all transformations from the pipeline
27
+ # and returning an instance of ::Ezamar::Template
28
+
29
+ def compile(action, template)
30
+ file = (action.template || __FILE__)
31
+
32
+ TRANSFORM_PIPELINE.each do |tp|
33
+ template = tp.transform(template)
45
34
  end
46
35
 
47
- template
36
+ ::Ezamar::Template.new(template, :file => file)
48
37
  end
49
38
  end
50
39
  end
@@ -83,7 +83,7 @@ class Ezamar::Element
83
83
  # a binding to be compatible to the transform-pipeline, won't have
84
84
  # any use for it though.
85
85
 
86
- def transform template, binding, file = __FILE__
86
+ def transform template
87
87
  matches = template.scan(/<([A-Z][a-zA-Z0-9]*)(.*?)?>/)
88
88
 
89
89
  matches.each do |(klass, params)|
@@ -17,47 +17,56 @@ module Ezamar
17
17
  # This class is responsible for initializing and compiling the template.
18
18
 
19
19
  class Template
20
- class << self
21
-
22
- # All ye who seek magic, look elsewhere, this method is ASAP (as simple as possible)
23
- #
24
- # There are some simple gsubs that build a final template which is evaluated
25
- #
26
- # The rules are following:
27
- # <?r rubycode ?>
28
- # evaluate the code inside the tag, this is considered XHTML-valid and so is the
29
- # preferred method for executing code inside your templates.
30
- # The return-value is ignored
31
- # <% rubycode %>
32
- # The same as <?r ?>, ERB-style and not valid XHTML, but should give someone who
33
- # is already familiar with ERB some common ground
34
- # #{ rubycode }
35
- # You know this from normal ruby already and it's actually nothing else.
36
- # Interpolation at the position in the template, isn't any special taggy format
37
- # and therefor safe to use.
38
- # <%= rubycode %>
39
- # The result of this will be interpolated at the position in the template.
40
- # Not valid XHTML either.
41
- #
42
- # TODO
43
- # - provide C version or maybe use erbuis
44
-
45
- def transform(template, binding, file = __FILE__)
46
- start_heredoc = "T" << Digest::SHA1.hexdigest(template)
47
- start_heredoc, end_heredoc = "\n<<#{start_heredoc}\n", "\n#{start_heredoc}\n"
48
- bufadd = "_out_ << "
49
-
50
- template.gsub!(/<%\s+(.*?)\s+%>/m,
20
+ def initialize(template, options = {})
21
+ @template, @options = template, options
22
+ compile
23
+ end
24
+
25
+ # All ye who seek magic, look elsewhere, this method is ASAP (as simple as possible)
26
+ #
27
+ # There are some simple gsubs that build a final template which is evaluated
28
+ #
29
+ # The rules are following:
30
+ # <?r rubycode ?>
31
+ # evaluate the code inside the tag, this is considered XHTML-valid and so is the
32
+ # preferred method for executing code inside your templates.
33
+ # The return-value is ignored
34
+ # <% rubycode %>
35
+ # The same as <?r ?>, ERB-style and not valid XHTML, but should give someone who
36
+ # is already familiar with ERB some common ground
37
+ # #{ rubycode }
38
+ # You know this from normal ruby already and it's actually nothing else.
39
+ # Interpolation at the position in the template, isn't any special taggy format
40
+ # and therefor safe to use.
41
+ # <%= rubycode %>
42
+ # The result of this will be interpolated at the position in the template.
43
+ # Not valid XHTML either.
44
+ #
45
+ # TODO
46
+ # - provide C version or maybe use erbuis
47
+
48
+ def compile
49
+ temp = @template.dup
50
+ start_heredoc = "T" << Digest::SHA1.hexdigest(temp)
51
+ start_heredoc, end_heredoc = "\n<<#{start_heredoc}\n", "\n#{start_heredoc}\n"
52
+ bufadd = "_out_ << "
53
+
54
+ temp.gsub!(/<%\s+(.*?)\s+%>/m,
51
55
  "#{end_heredoc} \\1; #{bufadd} #{start_heredoc}")
52
- template.gsub!(/<\?r\s+(.*?)\s+\?>/m,
56
+ temp.gsub!(/<\?r\s+(.*?)\s+\?>/m,
53
57
  "#{end_heredoc} \\1; #{bufadd} #{start_heredoc}")
54
- template.gsub!(/<%=\s+(.*?)\s+%>/m,
58
+ temp.gsub!(/<%=\s+(.*?)\s+%>/m,
55
59
  "#{end_heredoc} #{bufadd} (\\1); #{bufadd} #{start_heredoc}")
56
60
 
57
- template = "_out_ = ''; #{bufadd} #{start_heredoc} #{template} #{end_heredoc}; _out_"
61
+ @compiled = "_out_ = ''
62
+ #{bufadd} #{start_heredoc} #{temp} #{end_heredoc}
63
+ _out_"
64
+ end
65
+
66
+ # Takes a binding and evals it with the previously set options.
58
67
 
59
- eval(template, binding, file).strip
60
- end
68
+ def result(binding)
69
+ eval(@compiled, binding, @options[:file]).strip
61
70
  end
62
71
  end
63
72
  end
@@ -18,7 +18,7 @@ require 'ramaze/template/ezamar/engine'
18
18
  #
19
19
  # If you want to turn this functionality off, either remove Morpher
20
20
  # from:
21
- # Ramaze::Template::Ezamar.trait[:transform_pipeline]
21
+ # Ramaze::Template::Ezamar::TRANSFORM_PIPELINE
22
22
  # or do:
23
23
  # Ramaze::Morpher.trait[:morphs] = {}
24
24
  #
@@ -106,7 +106,7 @@ class Ezamar::Morpher
106
106
  # TODO:
107
107
  # - Add pure Ruby implementation as a fall-back.
108
108
 
109
- def self.transform template, action, file = __FILE__
109
+ def self.transform(template)
110
110
  morphs =
111
111
  trait[:morphs].map{|k,v| [k.to_s, v.to_s]}.select do |(k,v)|
112
112
  template.to_s.include?("#{k}=")
@@ -145,7 +145,7 @@ class Ezamar::Morpher
145
145
  # replace this method with a stub that only returns the template.
146
146
 
147
147
  self.class_eval do
148
- def self.transform(template, action)
148
+ def self.transform(template)
149
149
  template
150
150
  end
151
151
  end
@@ -0,0 +1,26 @@
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/template/ezamar/engine'
5
+
6
+ module Ezamar
7
+ class RenderPartial
8
+
9
+ # Renders <render src="/path" [optional="option", ...]> in place.
10
+ #
11
+ # Other options than `src` will be transformed to session parameters for the
12
+ # rendered action to use.
13
+
14
+ def self.transform(template, action, file = __FILE__)
15
+ template.gsub!(/<render (.*?) \/>/) do |m|
16
+ args = Hash[*$1.scan(/(\S+)=["'](.*?)["']/).flatten]
17
+ if src = args.delete('src')
18
+ PartialHelper.render_partial(src, args)
19
+ end
20
+ end
21
+
22
+ template
23
+ end
24
+
25
+ end
26
+ end
@@ -3,32 +3,37 @@
3
3
 
4
4
  require 'haml/engine'
5
5
 
6
- module Ramaze::Template
7
- class Haml < Template
6
+ module Ramaze
7
+ module Template
8
+ class Haml < Template
8
9
 
9
- # Custom HAML-options for your controller to be merged.
10
+ # Custom HAML-options for your controller to be merged.
10
11
 
11
- trait :haml_options => {
12
- :locals => {}
13
- }
12
+ trait :haml_options => {
13
+ :locals => {}
14
+ }
14
15
 
15
- Ramaze::Controller.register_engine self, %w[ haml ]
16
+ ENGINES[self] = %w[ haml ]
16
17
 
17
- class << self
18
+ class << self
18
19
 
19
- # Transform any String via Haml, takes optionally an hash with the haml_options
20
- # that you can set also by
21
- # trait :haml_options => {}
22
- # if you pass the options it will merge the trait with them. (your options
23
- # override the defaults from trait[:haml_options]
20
+ # Transform any String via Haml, takes optionally an hash with the
21
+ # haml_options that you can set also by
22
+ # trait :haml_options => {}
23
+ # if you pass the options it will merge the trait with them. (your
24
+ # options override the defaults from trait[:haml_options]
24
25
 
25
- def transform action
26
- template = reaction_or_file(action)
26
+ def transform action
27
+ haml = wrap_compile(action)
28
+ haml.to_html(action.instance)
29
+ end
27
30
 
28
- return '' unless template
31
+ # Instantiates Haml::Engine with the template and haml_options from
32
+ # the trait.
29
33
 
30
- haml = ::Haml::Engine.new(template, ancestral_trait[:haml_options])
31
- haml.to_html(action.controller)
34
+ def compile(action, template)
35
+ ::Haml::Engine.new(template, ancestral_trait[:haml_options])
36
+ end
32
37
  end
33
38
  end
34
39
  end
@@ -6,7 +6,8 @@ require 'liquid'
6
6
  module Ramaze
7
7
  module Template
8
8
  class Liquid < Template
9
- Controller.register_engine self, %w[ liquid ]
9
+
10
+ ENGINES[self] = %w[ liquid ]
10
11
 
11
12
  class << self
12
13
 
@@ -18,9 +19,10 @@ module Ramaze
18
19
  def transform action
19
20
  template = reaction_or_file(action)
20
21
 
21
- hash = action.controller.instance_variable_get("@hash") || {}
22
+ instance = action.instance
23
+ hash = instance.instance_variable_get("@hash") || {}
22
24
  template = ::Liquid::Template.parse(template)
23
- options = action.controller.ancestral_trait[:liquid_options]
25
+ options = instance.ancestral_trait[:liquid_options]
24
26
 
25
27
  template.render(hash, options)
26
28
  end
@@ -6,31 +6,34 @@ require 'markaby'
6
6
  module Ramaze
7
7
  module Template
8
8
  class Markaby < Template
9
- Controller.register_engine self, %w[ mab ]
9
+ ENGINES[self] = %w[ mab ]
10
10
 
11
11
  class << self
12
12
 
13
- # initializes the handling of a request on the controller.
14
- # Creates a new instances of itself and sends the action and params.
15
- # Also tries to render the template.
16
- # In Theory you can use this standalone, this has not been tested though.
13
+ # Entry point for Action#render
17
14
 
18
15
  def transform action
19
16
  result, file = result_and_file(action)
20
17
 
21
- result = transform_file(file, action) if file
18
+ result = transform_string(file, action) if file
22
19
  result.to_s
23
20
  end
24
21
 
25
- def transform_file file, action
26
- controller = action.controller
27
- ivs = extract_ivs(controller)
22
+ # Takes a string and action, instance_evals the string inside a mab
23
+ # block that gets the instance_variables of the original
24
+ # action.instance passed.
28
25
 
29
- controller.send(:mab, ivs) do
30
- instance_eval(file)
26
+ def transform_string string, action
27
+ instance = action.instance
28
+ ivs = extract_ivs(instance)
29
+
30
+ instance.send(:mab, ivs) do
31
+ instance_eval(string)
31
32
  end
32
33
  end
33
34
 
35
+ # Generate a hash from instance-variables
36
+
34
37
  def extract_ivs(controller)
35
38
  controller.instance_variables.inject({}) do |hash, iv|
36
39
  sym = iv.gsub('@', '').to_sym
@@ -6,20 +6,26 @@ require 'remarkably/engines/html'
6
6
  module Ramaze
7
7
  module Template
8
8
  class Remarkably < Template
9
- Controller.register_engine self, %w[ rem ]
9
+ ENGINES[self] = %w[ rem ]
10
10
 
11
11
  class << self
12
+
13
+ # Entry point for Action#render
14
+
12
15
  def transform action
13
16
  result, file = result_and_file(action)
14
17
 
15
- result = transform_file(file, action) if file
18
+ result = transform_string(file, action) if file
16
19
  result.to_s
17
20
  end
18
21
 
19
- def transform_file(file, action)
20
- action.controller.instance_eval do
22
+ # Takes a string and action, sets args to action.args and then proceeds
23
+ # to instance_eval the string inside the action.instance
24
+
25
+ def transform_string(string, action)
26
+ action.instance.instance_eval do
21
27
  args = action.params
22
- instance_eval(file)
28
+ instance_eval(string)
23
29
  end
24
30
  end
25
31
  end
@@ -27,6 +27,11 @@ class Ramaze::Tool::Localize
27
27
  # do some coercion between what you use as keys and what the browser sends.
28
28
  trait :mapping => { 'en-us' => 'en', 'ja' => 'jp'}
29
29
 
30
+ # When this is set to false, it will not save newly collected translatable
31
+ # strings to disk. Disable this for production use, as it slows the
32
+ # application down.
33
+ trait :collect => true
34
+
30
35
  class << self
31
36
 
32
37
  include Ramaze::Trinity
@@ -46,7 +51,7 @@ class Ramaze::Tool::Localize
46
51
  localize($1, locale)
47
52
  end
48
53
 
49
- store(locale, trait[:default_language])
54
+ store(locale, trait[:default_language]) if trait[:collect]
50
55
 
51
56
  body
52
57
  end
@@ -60,9 +65,12 @@ class Ramaze::Tool::Localize
60
65
 
61
66
  if dict[locale] && trans = dict[locale][str]
62
67
  #
63
- elsif trans = dict[default_language][str]
68
+ elsif dict[default_language] && trans = dict[default_language][str]
69
+ dict[locale] ||= {}
64
70
  dict[locale][str] = str
65
71
  else
72
+ dict[locale] ||= {}
73
+ dict[default_language] ||= {}
66
74
  dict[locale][str] = str
67
75
  dict[default_language][str] = str
68
76
  end
@@ -121,8 +129,8 @@ class Ramaze::Tool::Localize
121
129
  fd.write data
122
130
  end
123
131
  end
124
- rescue Errno::ENOENT
125
- # bleargh
132
+ rescue Errno::ENOENT => e
133
+ Ramaze::Inform.error e
126
134
  end
127
135
  end
128
136
  end