ramaze 0.3.5 → 0.3.9

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 (204) hide show
  1. data/Rakefile +5 -20
  2. data/bin/ramaze +0 -4
  3. data/doc/AUTHORS +5 -0
  4. data/doc/meta/announcement.txt +2 -1
  5. data/doc/tutorial/todolist.html +20 -21
  6. data/doc/tutorial/todolist.mkd +10 -9
  7. data/examples/blog/{src/controller.rb → controller/main.rb} +2 -0
  8. data/examples/blog/{src/model.rb → model/entry.rb} +0 -0
  9. data/examples/blog/spec/blog.rb +3 -3
  10. data/examples/blog/start.rb +2 -3
  11. data/examples/blog/view/edit.xhtml +17 -0
  12. data/examples/blog/view/index.xhtml +17 -0
  13. data/examples/blog/view/layout.xhtml +11 -0
  14. data/examples/blog/view/new.xhtml +16 -0
  15. data/examples/facebook.rb +15 -8
  16. data/examples/identity.rb +1 -1
  17. data/examples/memleak_detector.rb +1 -1
  18. data/examples/rammit/src/model.rb +1 -1
  19. data/examples/rapaste/controller/paste.rb +7 -7
  20. data/examples/rapaste/model/paste.rb +1 -3
  21. data/examples/rapaste/public/css/display.css +2 -1
  22. data/examples/rapaste/start.rb +2 -3
  23. data/examples/sourceview/sourceview.rb +1 -1
  24. data/examples/templates/template/external.tenjin +28 -0
  25. data/examples/templates/template_amrita2.rb +0 -3
  26. data/examples/templates/template_tenjin.rb +57 -0
  27. data/examples/todolist/spec/todolist.rb +13 -2
  28. data/examples/todolist/src/controller/main.rb +1 -1
  29. data/examples/todolist/template/index.xhtml +1 -1
  30. data/examples/wiktacular/spec/wiktacular.rb +18 -0
  31. data/examples/wiktacular/src/controller.rb +2 -2
  32. data/examples/wiktacular/src/model.rb +8 -2
  33. data/lib/proto/public/css/ramaze_error.css +4 -0
  34. data/lib/proto/public/dispatch.fcgi +11 -0
  35. data/lib/proto/spec/main.rb +1 -1
  36. data/lib/proto/start.ru +8 -0
  37. data/lib/proto/view/error.xhtml +2 -0
  38. data/lib/ramaze.rb +28 -5
  39. data/lib/ramaze/action.rb +9 -2
  40. data/lib/ramaze/action/render.rb +40 -24
  41. data/lib/ramaze/adapter.rb +10 -17
  42. data/lib/ramaze/adapter/base.rb +8 -12
  43. data/lib/ramaze/adapter/cgi.rb +14 -13
  44. data/lib/ramaze/adapter/ebb.rb +34 -0
  45. data/lib/ramaze/adapter/evented_mongrel.rb +1 -1
  46. data/lib/ramaze/adapter/fcgi.rb +14 -14
  47. data/lib/ramaze/adapter/lsws.rb +15 -11
  48. data/lib/ramaze/adapter/mongrel.rb +2 -1
  49. data/lib/ramaze/adapter/scgi.rb +24 -0
  50. data/lib/ramaze/adapter/swiftiplied_mongrel.rb +1 -1
  51. data/lib/ramaze/adapter/thin.rb +4 -5
  52. data/lib/ramaze/adapter/webrick.rb +5 -5
  53. data/lib/ramaze/cache.rb +1 -1
  54. data/lib/ramaze/cache/memcached.rb +1 -1
  55. data/lib/ramaze/contrib.rb +70 -10
  56. data/lib/ramaze/contrib/auto_params.rb +1 -1
  57. data/lib/ramaze/contrib/email.rb +15 -15
  58. data/lib/ramaze/contrib/facebook.rb +2 -2
  59. data/lib/ramaze/contrib/facebook/facebook.rb +7 -4
  60. data/lib/ramaze/contrib/file_cache.rb +65 -0
  61. data/lib/ramaze/contrib/gettext.rb +56 -55
  62. data/lib/ramaze/contrib/profiling.rb +36 -0
  63. data/lib/ramaze/contrib/sequel_cache.rb +92 -0
  64. data/lib/ramaze/controller.rb +80 -47
  65. data/lib/ramaze/controller/error.rb +10 -5
  66. data/lib/ramaze/controller/resolve.rb +36 -48
  67. data/lib/ramaze/current.rb +70 -0
  68. data/lib/ramaze/{trinity → current}/request.rb +62 -15
  69. data/lib/ramaze/current/response.rb +19 -0
  70. data/lib/ramaze/{trinity → current}/session.rb +32 -110
  71. data/lib/ramaze/current/session/flash.rb +67 -0
  72. data/lib/ramaze/current/session/hash.rb +65 -0
  73. data/lib/ramaze/dispatcher.rb +1 -28
  74. data/lib/ramaze/dispatcher/action.rb +6 -3
  75. data/lib/ramaze/dispatcher/directory.rb +1 -1
  76. data/lib/ramaze/dispatcher/error.rb +26 -5
  77. data/lib/ramaze/dispatcher/file.rb +13 -2
  78. data/lib/ramaze/gestalt.rb +3 -1
  79. data/lib/ramaze/global.rb +6 -3
  80. data/lib/ramaze/global/globalstruct.rb +3 -1
  81. data/lib/ramaze/helper.rb +66 -20
  82. data/lib/ramaze/helper/aspect.rb +25 -17
  83. data/lib/ramaze/helper/auth.rb +4 -3
  84. data/lib/ramaze/helper/cache.rb +5 -4
  85. data/lib/ramaze/helper/cgi.rb +11 -9
  86. data/lib/ramaze/helper/flash.rb +28 -3
  87. data/lib/ramaze/helper/formatting.rb +1 -3
  88. data/lib/ramaze/helper/identity.rb +2 -3
  89. data/lib/ramaze/helper/inform.rb +7 -6
  90. data/lib/ramaze/helper/link.rb +15 -17
  91. data/lib/ramaze/helper/markaby.rb +2 -4
  92. data/lib/ramaze/helper/maruku.rb +1 -1
  93. data/lib/ramaze/helper/nitroform.rb +4 -4
  94. data/lib/ramaze/helper/pager.rb +4 -6
  95. data/lib/ramaze/helper/partial.rb +24 -21
  96. data/lib/ramaze/helper/redirect.rb +8 -5
  97. data/lib/ramaze/helper/{file.rb → sendfile.rb} +1 -3
  98. data/lib/ramaze/helper/sequel.rb +1 -3
  99. data/lib/ramaze/helper/stack.rb +1 -3
  100. data/lib/ramaze/helper/tagz.rb +19 -0
  101. data/lib/ramaze/helper/user.rb +63 -0
  102. data/lib/ramaze/inform.rb +2 -24
  103. data/lib/ramaze/log.rb +28 -0
  104. data/lib/ramaze/{inform → log}/analogger.rb +3 -3
  105. data/lib/ramaze/{inform → log}/growl.rb +2 -2
  106. data/lib/ramaze/{inform → log}/hub.rb +4 -6
  107. data/lib/ramaze/{inform → log}/informer.rb +4 -4
  108. data/lib/ramaze/{inform → log}/knotify.rb +2 -2
  109. data/lib/ramaze/log/logger.rb +22 -0
  110. data/lib/ramaze/{inform/informing.rb → log/logging.rb} +16 -16
  111. data/lib/ramaze/{inform → log}/syslog.rb +0 -0
  112. data/lib/ramaze/{inform → log}/xosd.rb +2 -2
  113. data/lib/ramaze/route.rb +64 -36
  114. data/lib/ramaze/snippets/array/put_within.rb +13 -0
  115. data/lib/ramaze/snippets/binding/locals.rb +13 -0
  116. data/lib/ramaze/snippets/kernel/__dir__.rb +10 -8
  117. data/lib/ramaze/snippets/kernel/aquire.rb +1 -1
  118. data/lib/ramaze/snippets/kernel/constant.rb +1 -1
  119. data/lib/ramaze/snippets/object/pretty.rb +6 -0
  120. data/lib/ramaze/snippets/object/scope.rb +11 -0
  121. data/lib/ramaze/snippets/object/thread_accessor.rb +5 -0
  122. data/lib/ramaze/snippets/ordered_set.rb +1 -1
  123. data/lib/ramaze/snippets/proc/locals.rb +11 -0
  124. data/lib/ramaze/snippets/ramaze/thread_accessor.rb +44 -0
  125. data/lib/ramaze/snippets/string/esc.rb +29 -0
  126. data/lib/ramaze/snippets/string/start_with.rb +7 -0
  127. data/lib/ramaze/snippets/string/unindent.rb +6 -1
  128. data/lib/ramaze/snippets/struct/values_at.rb +1 -5
  129. data/lib/ramaze/sourcereload.rb +16 -14
  130. data/lib/ramaze/spec.rb +1 -0
  131. data/lib/ramaze/spec/helper.rb +11 -3
  132. data/lib/ramaze/spec/helper/browser.rb +25 -1
  133. data/lib/ramaze/spec/helper/pretty_output.rb +11 -11
  134. data/lib/ramaze/template.rb +5 -4
  135. data/lib/ramaze/template/amrita2.rb +2 -3
  136. data/lib/ramaze/template/ezamar/element.rb +2 -3
  137. data/lib/ramaze/template/ezamar/morpher.rb +1 -2
  138. data/lib/ramaze/template/ezamar/render_partial.rb +1 -1
  139. data/lib/ramaze/template/haml.rb +5 -2
  140. data/lib/ramaze/template/markaby.rb +2 -0
  141. data/lib/ramaze/template/tagz.rb +79 -0
  142. data/lib/ramaze/template/tenjin.rb +34 -0
  143. data/lib/ramaze/tool/create.rb +0 -3
  144. data/lib/ramaze/tool/localize.rb +107 -105
  145. data/lib/ramaze/tool/mime.rb +0 -2
  146. data/lib/ramaze/trinity.rb +1 -26
  147. data/lib/ramaze/version.rb +1 -1
  148. data/lib/vendor/bacon.rb +47 -41
  149. data/rake_tasks/conf.rake +18 -0
  150. data/rake_tasks/darcs.rake +5 -0
  151. data/rake_tasks/maintenance.rake +37 -24
  152. data/rake_tasks/spec.rake +1 -1
  153. data/spec/contrib/auto_params.rb +3 -1
  154. data/spec/contrib/profiling.rb +26 -0
  155. data/spec/examples/templates/template_redcloth.rb +1 -1
  156. data/spec/examples/templates/template_tenjin.rb +28 -0
  157. data/spec/helper.rb +0 -1
  158. data/spec/ramaze/action/layout.rb +28 -0
  159. data/spec/ramaze/controller/actionless_templates.rb +32 -0
  160. data/spec/ramaze/controller/template/other_wrapper.xhtml +1 -0
  161. data/spec/ramaze/controller/template_resolving.rb +37 -0
  162. data/spec/ramaze/{trinity → current}/request.rb +12 -2
  163. data/spec/ramaze/current/session.rb +97 -0
  164. data/spec/ramaze/dispatcher/directory.rb +2 -1
  165. data/spec/ramaze/dispatcher/file.rb +8 -3
  166. data/spec/ramaze/dispatcher/public/file name.txt +1 -0
  167. data/spec/ramaze/gestalt.rb +11 -0
  168. data/spec/ramaze/helper/aspect.rb +28 -22
  169. data/spec/ramaze/helper/cgi.rb +2 -2
  170. data/spec/ramaze/helper/flash.rb +33 -15
  171. data/spec/ramaze/helper/formatting.rb +2 -2
  172. data/spec/ramaze/helper/link.rb +46 -18
  173. data/spec/ramaze/helper/pager.rb +8 -5
  174. data/spec/ramaze/helper/partial.rb +8 -1
  175. data/spec/ramaze/helper/template/recursive_local_ivars.xhtml +7 -0
  176. data/spec/ramaze/helper/user.rb +46 -0
  177. data/spec/ramaze/{inform → log}/informer.rb +0 -0
  178. data/spec/ramaze/{inform → log}/syslog.rb +1 -1
  179. data/spec/ramaze/request.rb +14 -10
  180. data/spec/ramaze/route.rb +23 -0
  181. data/spec/ramaze/template.rb +48 -1
  182. data/spec/ramaze/template/haml.rb +6 -16
  183. data/spec/ramaze/template/haml/locals.haml +2 -1
  184. data/spec/ramaze/template/tagz.rb +62 -0
  185. data/spec/ramaze/template/tagz/external.tagz +8 -0
  186. data/spec/ramaze/template/tagz/sum.tagz +1 -0
  187. data/spec/ramaze/template/tenjin.rb +49 -0
  188. data/spec/ramaze/template/tenjin/external.tenjin +1 -0
  189. data/spec/snippets/string/unindent.rb +15 -0
  190. metadata +509 -475
  191. data/doc/README.html +0 -729
  192. data/doc/changes.txt +0 -5757
  193. data/doc/changes.xml +0 -5759
  194. data/examples/blog/src/view.rb +0 -16
  195. data/examples/blog/template/edit.xhtml +0 -19
  196. data/examples/blog/template/index.xhtml +0 -19
  197. data/examples/blog/template/new.xhtml +0 -18
  198. data/examples/wiktacular/mkd/newpagename/current.mkd +0 -1
  199. data/examples/wiktacular/mkd/newpagename/current.mkd.bak +0 -1
  200. data/lib/ramaze/contrib/email.rb-darcs-backup0 +0 -81
  201. data/lib/ramaze/template/bijou.rb +0 -39
  202. data/lib/ramaze/trinity/response.rb +0 -32
  203. data/spec/ramaze/template/bijou.rb +0 -25
  204. data/spec/ramaze/trinity/session.rb +0 -29
@@ -0,0 +1 @@
1
+ require 'ramaze/spec/helper'
@@ -3,11 +3,9 @@
3
3
 
4
4
  begin
5
5
  require 'rubygems'
6
- rescue LoadError => ex
6
+ rescue LoadError
7
7
  end
8
8
 
9
- require 'timeout'
10
-
11
9
  require 'ramaze'
12
10
  require 'ramaze/spec/helper/bacon'
13
11
 
@@ -112,3 +110,13 @@ shared 'xpath' do
112
110
  end
113
111
  end
114
112
  end
113
+
114
+ shared 'resolve' do
115
+ def resolve(url)
116
+ Ramaze::Controller::resolve(url)
117
+ end
118
+
119
+ def stack(url, &block)
120
+ resolve(url).stack(&block)
121
+ end
122
+ end
@@ -2,7 +2,7 @@
2
2
  # All files in this distribution are subject to the terms of the Ruby license.
3
3
 
4
4
  class Browser
5
- attr_reader :cookie
5
+ attr_reader :cookie, :http
6
6
 
7
7
  def initialize(url = '/', base = '/', &block)
8
8
  @base = base
@@ -42,6 +42,30 @@ class Browser
42
42
  erequest(:get, url, hash)
43
43
  end
44
44
 
45
+ def hget(*args)
46
+ @page = Hpricot(get(*args))
47
+ end
48
+
49
+ def hpost(*args)
50
+ @page = Hpricot(post(*args))
51
+ end
52
+
53
+ def find_link(text)
54
+ link = @page.at("a[text()='#{text}']")
55
+ link.should.not.be.nil if link.respond_to?(:should)
56
+ link[:href]
57
+ end
58
+
59
+ def follow_link(text)
60
+ hget find_link(text)
61
+ end
62
+
63
+ def follow_links(*texts)
64
+ texts.each do |text|
65
+ follow_link text
66
+ end
67
+ end
68
+
45
69
  def request method, url, hash = {}
46
70
  @http.uri = url2uri(url)
47
71
  @http.request_headers['referer'] = @history.last.path rescue '/'
@@ -14,12 +14,12 @@ module Bacon
14
14
  error = yield
15
15
 
16
16
  unless error.empty?
17
- if defined?(Ramaze::Informing)
17
+ if defined?(Ramaze::Logging)
18
18
  puts '', " #{NAME} -- #{description} [FAILED]".center(70, '-'), ''
19
19
  colors = Ramaze::Informer::COLORS
20
20
 
21
- until RamazeLogger.log.empty?
22
- tag, line = RamazeLogger.log.shift
21
+ until RamazeLogger.history.empty?
22
+ tag, line = RamazeLogger.history.shift
23
23
  out = "%6s | %s" % [tag.to_s, line]
24
24
  puts out.send(colors[tag])
25
25
  end
@@ -45,30 +45,30 @@ module Bacon
45
45
  end
46
46
  end
47
47
 
48
- if defined?(Ramaze::Informing)
48
+ if defined?(Ramaze::Logging)
49
49
  module Ramaze
50
50
  class SpecLogger
51
- include Ramaze::Informing
51
+ include Ramaze::Logging
52
52
  include Enumerable
53
53
 
54
- attr_accessor :log
54
+ attr_accessor :history
55
55
 
56
56
  def initialize
57
- @log = []
57
+ @history = []
58
58
  end
59
59
 
60
60
  def each
61
- @log.each{|e| yield(e) }
61
+ @history.each{|e| yield(e) }
62
62
  end
63
63
 
64
- def inform(tag, str)
65
- @log << [tag, str]
64
+ def log(tag, str)
65
+ @history << [tag, str]
66
66
  end
67
67
  end
68
68
  end
69
69
 
70
70
  module Bacon::PrettyOutput
71
71
  RamazeLogger = Ramaze::SpecLogger.new
72
- Ramaze::Inform.loggers = [RamazeLogger]
72
+ Ramaze::Log.loggers = [RamazeLogger]
73
73
  end
74
74
  end
@@ -16,16 +16,17 @@ module Ramaze
16
16
 
17
17
  ENGINES = {} unless defined?(ENGINES)
18
18
 
19
- %w[ Amrita2 Builder Erubis Haml Liquid Markaby Nagoro None RedCloth Remarkably Sass XSLT ].
20
- each do |const|
19
+ AVAILABLE_ENGINES = %w[ Amrita2 Builder Erubis Haml Liquid Markaby Nagoro None RedCloth Remarkably Sass XSLT Tenjin Tagz ]
20
+
21
+ AVAILABLE_ENGINES.each do |const|
21
22
  autoload(const, "ramaze/template/#{const.downcase}")
22
23
  end
23
24
 
24
25
  # The superclass for all templates, contains the shared behaviour of
25
- # the templates and includes Ramaze::Helper
26
+ # the templates and includes Ramaze::Helper::Methods
26
27
 
27
28
  class Template
28
- include Ramaze::Helper
29
+ include Ramaze::Helper::Methods
29
30
 
30
31
  class << self
31
32
 
@@ -7,9 +7,8 @@ class Amrita2::Template
7
7
 
8
8
  # Ramaze helpers are available in template contexts.
9
9
 
10
- include Ramaze::Helper
11
- extend Ramaze::Helper
12
- helper :link, :file, :flash, :cgi
10
+ include Ramaze::Helper::Methods
11
+ helper :link, :sendfile, :flash, :cgi
13
12
  end
14
13
 
15
14
  module Ramaze
@@ -56,8 +56,7 @@
56
56
  class Ezamar::Element
57
57
  attr_accessor :content
58
58
 
59
- include Ramaze::Helper
60
- extend Ramaze::Helper
59
+ include Ramaze::Helper::Methods
61
60
 
62
61
  helper :redirect, :link
63
62
 
@@ -137,7 +136,7 @@ class Ezamar::Element
137
136
 
138
137
  instance.render
139
138
  rescue => ex
140
- Ramaze::Inform.debug(ex.message)
139
+ Ramaze::Log.debug(ex.message)
141
140
  ''
142
141
  end
143
142
 
@@ -1,11 +1,10 @@
1
1
  # Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
2
2
  # All files in this distribution are subject to the terms of the Ruby license.
3
3
 
4
- require 'ramaze/template/ezamar/engine'
5
4
  begin
6
5
  require 'hpricot'
7
6
  rescue LoadError => ex
8
- Ramaze::Inform.error "Please install hpricot (for example via `gem install hpricot`) to get morphing"
7
+ Ramaze::Log.error "Please install hpricot (for example via `gem install hpricot`) to get morphing"
9
8
  puts ex
10
9
  end
11
10
 
@@ -9,7 +9,7 @@ module Ezamar
9
9
  # A transformer for <render /> tags.
10
10
 
11
11
  class RenderPartial
12
- extend Ramaze::PartialHelper
12
+ extend Ramaze::Helper::Partial
13
13
 
14
14
  # Renders <render src="/path" [optional="option", ...]> in place.
15
15
  #
@@ -19,14 +19,17 @@ module Ramaze
19
19
 
20
20
  def transform action
21
21
  haml = wrap_compile(action)
22
- haml.to_html(action.instance)
22
+ haml.to_html(action.instance, action.binding.locals)
23
23
  end
24
24
 
25
25
  # Instantiates Haml::Engine with the template and haml_options trait from
26
26
  # the controller.
27
27
 
28
28
  def compile(action, template)
29
- ::Haml::Engine.new(template, action.controller.trait[:haml_options] || {})
29
+ opts = action.controller.trait[:haml_options] || {}
30
+ opts.merge! :filename => action.template if action.template
31
+
32
+ ::Haml::Engine.new(template, opts)
30
33
  end
31
34
  end
32
35
  end
@@ -10,6 +10,8 @@ module Ramaze
10
10
  # Can be found at: http://code.whytheluckystiff.net/markaby/
11
11
 
12
12
  class Markaby < Template
13
+ include Ramaze::Helper::Methods
14
+
13
15
  ENGINES[self] = %w[ mab ]
14
16
 
15
17
  class << self
@@ -0,0 +1,79 @@
1
+ # Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
2
+ # All files in this distribution are subject to the terms of the Ruby license.
3
+
4
+ require "tagz"
5
+
6
+ module Ramaze
7
+ module Template
8
+
9
+ # Is responsible for compiling a template using the Tagz templating engine.
10
+ # Can be found at: http://rubyforge.org/projects/codeforpeople/
11
+ # gem install tagz
12
+ # be sure you get version 1.0 or higher
13
+
14
+ class Tagz < Template
15
+ ENGINES[self] = %w[ rb tagz ]
16
+
17
+ class << self
18
+
19
+ # Entry point for Action#render
20
+
21
+ def transform action
22
+ result, file = result_and_file(action)
23
+ if file
24
+ markup = "tagz{#{ file }}"
25
+ action.instance.extend Methods
26
+ result = eval markup, action.binding, file
27
+ end
28
+ result
29
+ end
30
+
31
+ end
32
+
33
+ # Methods defines a host of methods useful inside the context of a view
34
+ # including print style methods that output content rather that printing to
35
+ # STDOUT
36
+
37
+ module Methods
38
+ include ::Tagz
39
+
40
+ private
41
+ def << s
42
+ tagz << s; self
43
+ end
44
+
45
+ def concat *a
46
+ a.each{|s| tagz << s}; self
47
+ end
48
+
49
+ def puts *a
50
+ a.each{|elem| tagz << "#{ elem.to_s.chomp }#{ eol }"}
51
+ end
52
+
53
+ def print *a
54
+ a.each{|elem| tagz << elem}
55
+ end
56
+
57
+ def p *a
58
+ a.each{|elem| tagz << "#{ CGI.escapeHTML elem.inspect }#{ eol }"}
59
+ end
60
+
61
+ def pp *a
62
+ a.each{|elem| tagz << "#{ CGI.escapeHTML PP.pp(elem, '') }#{ eol }"}
63
+ end
64
+
65
+ def eol
66
+ if response.content_type =~ %r|text/plain|io
67
+ "\n"
68
+ else
69
+ "<br />"
70
+ end
71
+ end
72
+
73
+ def __ *a
74
+ concat eol
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,34 @@
1
+ require "tenjin"
2
+ require "tempfile"
3
+
4
+ class Tenjin::Context
5
+ include Ramaze::Helper::Methods
6
+ helper :link, :sendfile, :flash, :cgi
7
+ end
8
+
9
+ module Ramaze
10
+ module Template
11
+
12
+ # Is responsible for compiling a template using the Tenjin templating engine.
13
+ # Can be found at: http://www.kuwata-lab.com/tenjin/
14
+
15
+ class Tenjin < Template
16
+
17
+ ENGINES[self] = %w[ rbhtml tenjin ]
18
+
19
+ class << self
20
+ def transform(action)
21
+ tenjin = wrap_compile(action)
22
+ context = action.instance.instance_variable_get("@context") || {}
23
+ tenjin.render(context)
24
+ end
25
+
26
+ def compile(action, template)
27
+ tenjin = ::Tenjin::Template.new
28
+ tenjin.convert(template)
29
+ return tenjin
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,9 +1,6 @@
1
1
  # Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
2
2
  # All files in this distribution are subject to the terms of the Ruby license.
3
3
 
4
- require 'fileutils'
5
- require 'yaml'
6
-
7
4
  module Ramaze
8
5
  module Tool
9
6
 
@@ -12,149 +12,151 @@ require 'ya2yaml'
12
12
  #
13
13
  # Ramaze::Dispatcher::Action::FILTER << Ramaze::Tool::Localize
14
14
 
15
- class Ramaze::Tool::Localize
15
+ module Ramaze
16
+ class Tool::Localize
16
17
 
17
- # Enable Localization
18
- trait :enable => true
18
+ # Enable Localization
19
+ trait :enable => true
19
20
 
20
- # Default language that is used if the browser don't suggests otherwise or
21
- # the language requested is not available.
22
- trait :default_language => 'en'
21
+ # Default language that is used if the browser don't suggests otherwise or
22
+ # the language requested is not available.
23
+ trait :default_language => 'en'
23
24
 
24
- # languages supported
25
- trait :languages => %w[ en ]
25
+ # languages supported
26
+ trait :languages => %w[ en ]
26
27
 
27
- # YAML files the localizations are saved to and loaded from, %s is
28
- # substituted by the values from trait[:languages]
29
- trait :file => 'conf/locale_%s.yaml'.freeze
28
+ # YAML files the localizations are saved to and loaded from, %s is
29
+ # substituted by the values from trait[:languages]
30
+ trait :file => 'conf/locale_%s.yaml'.freeze
30
31
 
31
- # The pattern that is substituted with the translation of the current locale.
32
- trait :regex => /\[\[(.*?)\]\]/
32
+ # The pattern that is substituted with the translation of the current locale.
33
+ trait :regex => /\[\[(.*?)\]\]/
33
34
 
34
- # Browsers may send different keys for the same language, this allows you to
35
- # do some coercion between what you use as keys and what the browser sends.
36
- trait :mapping => { 'en-us' => 'en', 'ja' => 'jp'}
35
+ # Browsers may send different keys for the same language, this allows you to
36
+ # do some coercion between what you use as keys and what the browser sends.
37
+ trait :mapping => { 'en-us' => 'en', 'ja' => 'jp'}
37
38
 
38
- # When this is set to false, it will not save newly collected translatable
39
- # strings to disk. Disable this for production use, as it slows the
40
- # application down.
41
- trait :collect => true
39
+ # When this is set to false, it will not save newly collected translatable
40
+ # strings to disk. Disable this for production use, as it slows the
41
+ # application down.
42
+ trait :collect => true
42
43
 
43
- class << self
44
+ class << self
44
45
 
45
- include Ramaze::Trinity
46
+ include Trinity
46
47
 
47
- # Enables being plugged into Dispatcher::Action::FILTER
48
+ # Enables being plugged into Dispatcher::Action::FILTER
48
49
 
49
- def call(response, options = {})
50
- return response unless trait[:enable]
51
- return response if response.body.nil?
52
- return response if response.body.respond_to?(:read)
53
- response.body = localize_body(response.body, options)
54
- response
55
- end
50
+ def call(response, options = {})
51
+ return response unless trait[:enable]
52
+ return response if response.body.nil?
53
+ return response if response.body.respond_to?(:read)
54
+ response.body = localize_body(response.body, options)
55
+ response
56
+ end
56
57
 
57
- # Localizes a response body. It reacts to a regular expression as given
58
- # in trait[:regex]. Every 'entity' in it will be translated, see
59
- # `localize` for more information.
58
+ # Localizes a response body. It reacts to a regular expression as given
59
+ # in trait[:regex]. Every 'entity' in it will be translated, see
60
+ # `localize` for more information.
60
61
 
61
- def localize_body(body, options)
62
- locale =
63
- if trait[:languages].include?(response["Content-Language"])
64
- response["Content-Language"]
65
- else
66
- (session[:LOCALE] || set_session_locale).to_s
67
- end
62
+ def localize_body(body, options)
63
+ locale =
64
+ if trait[:languages].include?(response["Content-Language"])
65
+ response["Content-Language"]
66
+ else
67
+ (session[:LOCALE] || set_session_locale).to_s
68
+ end
68
69
 
69
- body.gsub!(trait[:regex]) do
70
- localize($1, locale) unless $1.to_s.empty?
71
- end
70
+ body.gsub!(trait[:regex]) do
71
+ localize($1, locale) unless $1.to_s.empty?
72
+ end
72
73
 
73
- store(locale, trait[:default_language]) if trait[:collect]
74
+ store(locale, trait[:default_language]) if trait[:collect]
74
75
 
75
- body
76
- end
76
+ body
77
+ end
77
78
 
78
- # Localizes a single 'entity'. If a translation in the chosen language is
79
- # not available, it falls back to the default language.
79
+ # Localizes a single 'entity'. If a translation in the chosen language is
80
+ # not available, it falls back to the default language.
80
81
 
81
- def localize(str, locale)
82
- default_language = trait[:default_language]
83
- dict = dictionary
84
- dict[locale] ||= {}
85
- dict[default_language] ||= {}
82
+ def localize(str, locale)
83
+ default_language = trait[:default_language]
84
+ dict = dictionary
85
+ dict[locale] ||= {}
86
+ dict[default_language] ||= {}
86
87
 
87
- trans = dict[locale].fetch(str, dict[default_language][str])
88
- dict[locale][str] ||= trans
89
- dict[default_language][str] ||= trans
88
+ trans = dict[locale].fetch(str, dict[default_language][str])
89
+ dict[locale][str] ||= trans
90
+ dict[default_language][str] ||= trans
90
91
 
91
- trans
92
- rescue Object => ex
93
- Ramaze::Inform.error(ex)
94
- str
95
- end
92
+ trans
93
+ rescue Object => ex
94
+ Log.error(ex)
95
+ str
96
+ end
96
97
 
97
- # Sets session[:LOCALE] to one of the languages defined in the dictionary.
98
- # It first tries to honor the browsers accepted languages and then falls
99
- # back to the default language.
98
+ # Sets session[:LOCALE] to one of the languages defined in the dictionary.
99
+ # It first tries to honor the browsers accepted languages and then falls
100
+ # back to the default language.
100
101
 
101
- def set_session_locale
102
- session[:LOCALE] = trait[:default_language]
103
- accepted_langs = request.http_accept_language rescue 'en'
102
+ def set_session_locale
103
+ session[:LOCALE] = trait[:default_language]
104
+ accepted_langs = request.http_accept_language rescue 'en'
104
105
 
105
- mapping = trait[:mapping]
106
- dict = dictionary
107
- accepted_langs = accepted_langs.scan(/([^,;]+)(?:;q=[^,]+)?/m)[0]
106
+ mapping = trait[:mapping]
107
+ dict = dictionary
108
+ accepted_langs = accepted_langs.scan(/([^,;]+)(?:;q=[^,]+)?/m)[0]
108
109
 
109
- accepted_langs.each do |language|
110
- language = mapping[language] || language
111
- if dict.key?(language)
112
- session[:LOCALE] = language
113
- break
110
+ accepted_langs.each do |language|
111
+ language = mapping[language] || language
112
+ if dict.key?(language)
113
+ session[:LOCALE] = language
114
+ break
115
+ end
114
116
  end
115
- end
116
117
 
117
- session[:LOCALE]
118
- end
118
+ session[:LOCALE]
119
+ end
119
120
 
120
- # Returns the dictionary used for translation.
121
+ # Returns the dictionary used for translation.
121
122
 
122
- def dictionary
123
- trait[:languages].map! {|x| x.to_s }.uniq!
124
- trait[:dictionary] || load(*trait[:languages])
125
- end
123
+ def dictionary
124
+ trait[:languages].map! {|x| x.to_s }.uniq!
125
+ trait[:dictionary] || load(*trait[:languages])
126
+ end
126
127
 
127
- # Load given locales from disk and save it into the dictionary.
128
+ # Load given locales from disk and save it into the dictionary.
128
129
 
129
- def load(*locales)
130
- Ramaze::Inform.debug "loading locales: #{locales.inspect}"
130
+ def load(*locales)
131
+ Log.debug "loading locales: #{locales.inspect}"
131
132
 
132
- dict = trait[:dictionary] || {}
133
+ dict = trait[:dictionary] || {}
133
134
 
134
- locales.each do |locale|
135
- begin
136
- dict[locale] = YAML.load_file(trait[:file] % locale)
137
- rescue Errno::ENOENT
138
- dict[locale] = {}
135
+ locales.each do |locale|
136
+ begin
137
+ dict[locale] = YAML.load_file(trait[:file] % locale)
138
+ rescue Errno::ENOENT
139
+ dict[locale] = {}
140
+ end
139
141
  end
140
- end
141
142
 
142
- trait[:dictionary] = dict
143
- end
143
+ trait[:dictionary] = dict
144
+ end
144
145
 
145
- # Stores given locales from the dictionary to disk.
146
+ # Stores given locales from the dictionary to disk.
146
147
 
147
- def store(*locales)
148
- locales.uniq.compact.each do |locale|
149
- Ramaze::Inform.dev "saving localized to: #{trait[:file] % locale}"
150
- data = dictionary[locale].ya2yaml
151
- file = trait[:file] % locale
152
- File.open(file, File::CREAT|File::TRUNC|File::WRONLY) do |fd|
153
- fd.write data
148
+ def store(*locales)
149
+ locales.uniq.compact.each do |locale|
150
+ Log.dev "saving localized to: #{trait[:file] % locale}"
151
+ data = dictionary[locale].ya2yaml
152
+ file = trait[:file] % locale
153
+ File.open(file, File::CREAT|File::TRUNC|File::WRONLY) do |fd|
154
+ fd.write data
155
+ end
154
156
  end
157
+ rescue Errno::ENOENT => e
158
+ Log.error e
155
159
  end
156
- rescue Errno::ENOENT => e
157
- Ramaze::Inform.error e
158
160
  end
159
161
  end
160
162
  end