ramaze 0.0.7 → 0.0.8

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 (193) hide show
  1. data/Rakefile +52 -19
  2. data/bin/ramaze +19 -6
  3. data/doc/CHANGELOG +33 -0
  4. data/doc/COPYING +1 -1
  5. data/doc/FAQ +92 -0
  6. data/doc/GPL +340 -0
  7. data/doc/INSTALL +34 -0
  8. data/doc/ProjectInfo +53 -0
  9. data/doc/README +187 -110
  10. data/doc/readme_chunks/appendix.txt +13 -0
  11. data/doc/readme_chunks/examples.txt +38 -0
  12. data/doc/readme_chunks/features.txt +82 -0
  13. data/doc/readme_chunks/getting_help.txt +5 -0
  14. data/doc/readme_chunks/getting_started.txt +18 -0
  15. data/doc/readme_chunks/installing.txt +41 -0
  16. data/doc/readme_chunks/introduction.txt +18 -0
  17. data/doc/readme_chunks/principles.txt +41 -0
  18. data/doc/readme_chunks/thanks.txt +59 -0
  19. data/doc/tutorial/todolist.txt +546 -0
  20. data/examples/blog/main.rb +1 -1
  21. data/examples/blog/src/controller.rb +13 -11
  22. data/examples/blog/src/element.rb +11 -6
  23. data/examples/blog/src/model.rb +8 -23
  24. data/examples/blog/template/edit.xhtml +3 -1
  25. data/examples/blog/template/index.xhtml +4 -4
  26. data/examples/caching.rb +4 -4
  27. data/examples/element.rb +10 -7
  28. data/examples/hello.rb +3 -4
  29. data/examples/simple.rb +5 -3
  30. data/examples/templates/template/external.amrita +19 -0
  31. data/examples/templates/template/{external.rmze → external.zmr} +2 -2
  32. data/examples/templates/template_amrita2.rb +48 -0
  33. data/examples/templates/template_erubis.rb +5 -2
  34. data/examples/templates/{template_ramaze.rb → template_ezamar.rb} +13 -7
  35. data/examples/templates/template_haml.rb +4 -1
  36. data/examples/templates/template_liquid.rb +2 -1
  37. data/examples/templates/template_markaby.rb +2 -1
  38. data/examples/todolist/conf/benchmark.yaml +35 -0
  39. data/examples/todolist/conf/debug.yaml +34 -0
  40. data/examples/todolist/conf/live.yaml +33 -0
  41. data/examples/todolist/conf/silent.yaml +31 -0
  42. data/examples/todolist/conf/stage.yaml +33 -0
  43. data/examples/todolist/main.rb +18 -0
  44. data/examples/todolist/public/404.jpg +0 -0
  45. data/examples/todolist/public/css/coderay.css +105 -0
  46. data/examples/todolist/public/css/ramaze_error.css +42 -0
  47. data/{lib/proto → examples/todolist}/public/error.xhtml +0 -0
  48. data/examples/todolist/public/favicon.ico +0 -0
  49. data/examples/todolist/public/js/jquery.js +1923 -0
  50. data/examples/todolist/public/ramaze.png +0 -0
  51. data/examples/todolist/src/controller/main.rb +56 -0
  52. data/examples/todolist/src/element/page.rb +26 -0
  53. data/examples/todolist/src/model.rb +14 -0
  54. data/examples/todolist/template/index.xhtml +17 -0
  55. data/examples/todolist/template/new.xhtml +7 -0
  56. data/examples/todolist/todolist.db +9 -0
  57. data/examples/whywiki/main.rb +3 -8
  58. data/examples/whywiki/template/show.xhtml +4 -0
  59. data/lib/proto/public/error.zmr +77 -0
  60. data/lib/proto/src/controller/main.rb +2 -1
  61. data/lib/proto/src/element/page.rb +2 -1
  62. data/lib/proto/src/model.rb +3 -2
  63. data/lib/ramaze.rb +7 -9
  64. data/lib/ramaze/adapter.rb +51 -0
  65. data/lib/ramaze/adapter/cgi.rb +23 -0
  66. data/lib/ramaze/adapter/fcgi.rb +22 -0
  67. data/lib/ramaze/adapter/mongrel.rb +7 -86
  68. data/lib/ramaze/adapter/webrick.rb +14 -133
  69. data/lib/ramaze/cache/memcached.rb +6 -0
  70. data/lib/ramaze/cache/yaml_store.rb +3 -1
  71. data/lib/ramaze/controller.rb +292 -2
  72. data/lib/ramaze/dispatcher.rb +85 -213
  73. data/lib/ramaze/error.rb +10 -0
  74. data/lib/ramaze/global.rb +8 -0
  75. data/lib/ramaze/helper/aspect.rb +30 -7
  76. data/lib/ramaze/helper/auth.rb +16 -9
  77. data/lib/ramaze/helper/cache.rb +40 -35
  78. data/lib/ramaze/helper/feed.rb +1 -1
  79. data/lib/ramaze/helper/flash.rb +34 -0
  80. data/lib/ramaze/helper/link.rb +8 -2
  81. data/lib/ramaze/helper/openid.rb +63 -0
  82. data/lib/ramaze/helper/redirect.rb +12 -11
  83. data/lib/ramaze/helper/stack.rb +5 -7
  84. data/lib/ramaze/inform.rb +12 -1
  85. data/lib/ramaze/snippets/kernel/aquire.rb +1 -1
  86. data/lib/ramaze/snippets/kernel/{self_method.rb → method.rb} +3 -18
  87. data/lib/ramaze/snippets/kernel/{rescue_require.rb → pretty_inspect.rb} +7 -6
  88. data/lib/ramaze/snippets/method/name.rb +22 -0
  89. data/lib/ramaze/snippets/{kernel → ramaze}/autoreload.rb +0 -0
  90. data/lib/ramaze/snippets/ramaze/caller_info.rb +14 -0
  91. data/lib/ramaze/snippets/{kernel → ramaze}/caller_lines.rb +3 -10
  92. data/lib/ramaze/snippets/rdoc/usage_no_exit.rb +49 -23
  93. data/lib/ramaze/snippets/string/DIVIDE.rb +0 -1
  94. data/lib/ramaze/store/default.rb +58 -2
  95. data/lib/ramaze/store/yaml.rb +161 -0
  96. data/lib/ramaze/template.rb +27 -86
  97. data/lib/ramaze/template/amrita2.rb +14 -19
  98. data/lib/ramaze/template/erubis.rb +15 -38
  99. data/lib/ramaze/template/ezamar.rb +100 -0
  100. data/lib/ramaze/template/ezamar/element.rb +166 -0
  101. data/lib/ramaze/template/ezamar/engine.rb +124 -0
  102. data/lib/ramaze/template/ezamar/morpher.rb +155 -0
  103. data/lib/ramaze/template/haml.rb +16 -43
  104. data/lib/ramaze/template/liquid.rb +11 -51
  105. data/lib/ramaze/template/markaby.rb +44 -42
  106. data/lib/ramaze/tool/mime.rb +18 -0
  107. data/lib/ramaze/tool/mime_types.yaml +615 -0
  108. data/lib/ramaze/trinity/request.rb +20 -196
  109. data/lib/ramaze/trinity/response.rb +4 -33
  110. data/lib/ramaze/trinity/session.rb +150 -72
  111. data/lib/ramaze/version.rb +1 -1
  112. data/spec/adapter_spec.rb +20 -0
  113. data/spec/public/favicon.ico +0 -0
  114. data/spec/public/ramaze.png +0 -0
  115. data/spec/public/test_download.css +141 -0
  116. data/spec/{tc_request.rb → request_tc_helper.rb} +45 -21
  117. data/spec/spec_all.rb +77 -34
  118. data/spec/spec_helper.rb +8 -157
  119. data/spec/spec_helper_context.rb +72 -0
  120. data/spec/spec_helper_requester.rb +52 -0
  121. data/spec/spec_helper_simple_http.rb +433 -0
  122. data/spec/tc_adapter_mongrel.rb +3 -15
  123. data/spec/tc_adapter_webrick.rb +4 -14
  124. data/spec/tc_cache.rb +3 -5
  125. data/spec/tc_controller.rb +22 -12
  126. data/spec/tc_dependencies.rb +13 -0
  127. data/spec/tc_element.rb +8 -7
  128. data/spec/tc_error.rb +13 -7
  129. data/spec/tc_global.rb +16 -18
  130. data/spec/tc_helper_aspect.rb +2 -4
  131. data/spec/tc_helper_auth.rb +15 -14
  132. data/spec/tc_helper_cache.rb +5 -7
  133. data/spec/tc_helper_feed.rb +0 -2
  134. data/spec/tc_helper_flash.rb +103 -0
  135. data/spec/tc_helper_form.rb +4 -6
  136. data/spec/tc_helper_link.rb +1 -3
  137. data/spec/tc_helper_redirect.rb +23 -8
  138. data/spec/tc_helper_stack.rb +31 -15
  139. data/spec/tc_morpher.rb +1 -3
  140. data/spec/tc_params.rb +48 -7
  141. data/spec/tc_request_mongrel.rb +9 -0
  142. data/spec/tc_request_webrick.rb +5 -0
  143. data/spec/tc_session.rb +41 -25
  144. data/spec/tc_store.rb +55 -6
  145. data/spec/tc_store_yaml.rb +71 -0
  146. data/spec/tc_template_amrita2.rb +3 -3
  147. data/spec/tc_template_erubis.rb +2 -3
  148. data/spec/{tc_template_ramaze.rb → tc_template_ezamar.rb} +15 -5
  149. data/spec/tc_template_haml.rb +4 -3
  150. data/spec/tc_template_liquid.rb +3 -4
  151. data/spec/tc_template_markaby.rb +4 -6
  152. data/spec/tc_tidy.rb +1 -3
  153. data/spec/template/amrita2/{data.html → data.amrita} +0 -0
  154. data/spec/template/amrita2/{index.html → index.amrita} +0 -0
  155. data/spec/template/amrita2/{sum.html → sum.amrita} +0 -0
  156. data/spec/template/ezamar/another/long/action.zmr +1 -0
  157. data/spec/template/ezamar/combined.zmr +1 -0
  158. data/spec/template/{ramaze/file_only.rmze → ezamar/file_only.zmr} +0 -0
  159. data/spec/template/{ramaze/index.rmze → ezamar/index.zmr} +0 -0
  160. data/spec/template/{ramaze/nested.rmze → ezamar/nested.zmr} +0 -0
  161. data/spec/template/ezamar/some__long__action.zmr +1 -0
  162. data/spec/template/{ramaze/sum.rmze → ezamar/sum.zmr} +0 -0
  163. metadata +181 -123
  164. data/doc/allison/LICENSE +0 -184
  165. data/doc/allison/README +0 -37
  166. data/doc/allison/allison.css +0 -300
  167. data/doc/allison/allison.gif +0 -0
  168. data/doc/allison/allison.js +0 -307
  169. data/doc/allison/allison.rb +0 -287
  170. data/doc/allison/cache/BODY +0 -588
  171. data/doc/allison/cache/CLASS_INDEX +0 -4
  172. data/doc/allison/cache/CLASS_PAGE +0 -1
  173. data/doc/allison/cache/FILE_INDEX +0 -4
  174. data/doc/allison/cache/FILE_PAGE +0 -1
  175. data/doc/allison/cache/FONTS +0 -1
  176. data/doc/allison/cache/FR_INDEX_BODY +0 -1
  177. data/doc/allison/cache/IMGPATH +0 -1
  178. data/doc/allison/cache/INDEX +0 -1
  179. data/doc/allison/cache/JAVASCRIPT +0 -307
  180. data/doc/allison/cache/METHOD_INDEX +0 -4
  181. data/doc/allison/cache/METHOD_LIST +0 -1
  182. data/doc/allison/cache/SRC_PAGE +0 -1
  183. data/doc/allison/cache/STYLE +0 -322
  184. data/doc/allison/cache/URL +0 -1
  185. data/doc/changes.txt +0 -2021
  186. data/doc/changes.xml +0 -2024
  187. data/lib/ramaze/snippets/kernel/silently.rb +0 -13
  188. data/lib/ramaze/snippets/thread/deadQUESTIONMARK.rb +0 -11
  189. data/lib/ramaze/template/haml/actionview_stub.rb +0 -20
  190. data/lib/ramaze/template/ramaze.rb +0 -177
  191. data/lib/ramaze/template/ramaze/element.rb +0 -166
  192. data/lib/ramaze/template/ramaze/morpher.rb +0 -156
  193. data/spec/tc_test.rb +0 -17
@@ -1,13 +0,0 @@
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
- module Kernel
4
-
5
- # sets $VERBOSE temporarily to nil so you can redefine constants in peace
6
-
7
- def silently
8
- old_verbose, $VERBOSE = $VERBOSE, nil
9
- yield
10
- ensure
11
- $VERBOSE = old_verbose
12
- end
13
- end
@@ -1,11 +0,0 @@
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
- class Thread
5
-
6
- # check if the thread is not alive (not alive?)
7
-
8
- def dead?
9
- not alive?
10
- end
11
- end
@@ -1,20 +0,0 @@
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
- # stub, in case the requires for that active_* stuff of Haml doesn't fail
5
- # but we are not running Rails ;)
6
-
7
- module ActionView
8
- class Base
9
-
10
- # stub
11
-
12
- def concat
13
- end
14
-
15
- # stub
16
-
17
- def form_tag
18
- end
19
- end
20
- end
@@ -1,177 +0,0 @@
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 'digest/sha1'
5
- require 'ramaze/template/ramaze/element'
6
- require 'ramaze/template/ramaze/morpher'
7
-
8
- module Ramaze::Template
9
-
10
- # The usual Template for use of Ramaze.
11
- # It supports erb-style interpolation and a pipeline for the transform
12
-
13
- class Ramaze < Template
14
-
15
- # Actions consist of both templates and methods on the controller.
16
- trait :actionless => false
17
-
18
- # Usual extensions for templates.
19
- trait :template_extensions => %w[rmze xhtml rhtml html]
20
-
21
- # Each Object in this Array will be called :transform upon.
22
- trait :transform_pipeline => [ Element, Morpher, self ]
23
-
24
- class << self
25
- # initializes the handling of a request on the controller.
26
- # Creates a new instances of itself and sends the action and params.
27
- # Also tries to render the template.
28
- # In Theory you can use this standalone, this has not been tested though.
29
-
30
- def handle_request(action, *params)
31
- controller = self.new
32
- controller.send(:render, action, *params)
33
- end
34
-
35
- # in case someone wants to call directly (pipeline)
36
-
37
- def transform(template, bound)
38
- self.new.send(:transform, template, bound)
39
- end
40
- end
41
-
42
- private
43
-
44
- # render an action
45
- # this looks up the file depending on your Global.template_root
46
- # and also takes the mapping of the controller in account.
47
- # To use the exception of method_missing as flow-control might seem odd,
48
- # but it works perfect for method_missing defined in the controller...
49
- # otherwise one has to modify respond_to?... maybe add a special take-all method?
50
- #
51
- # for example:
52
- # / => FooController, /main => MainController
53
- # an action to /bar would search directly in the template_root for a file called bar
54
- # and the extensions rmze, xhtml or html (in that order)
55
- # if it finds a file it will transform it and return the result
56
- # otherwise nothing happens
57
- #
58
- # TODO:
59
- # - maybe add some way to define a custom template-file per action via traits
60
- # trait :methodname_template => :foo
61
- # would point to the template-file of action :foo - template_root/foo.ext
62
- # - add extensive tests!
63
- #
64
-
65
- def render(action, *params)
66
- alternate = render_template(params.last) if params.size == 1 and action == 'index'
67
- file_template = render_template(action, *params)
68
- ctrl_template = render_action(action, *params)
69
-
70
- pipeline(alternate || file_template || ctrl_template)
71
- end
72
-
73
- # Render an action, usually a method on the controller.
74
-
75
- def render_action(action, *params)
76
- ctrl_template = send(action, *params).to_s
77
- rescue => e
78
- Informer.error e unless e.message =~ /undefined method `#{Regexp.escape(action.to_s)}'/
79
-
80
- unless caller.select{|bt| bt[/`render_action'/]}.size > 3
81
- Dispatcher.respond_action([action, *params].join('/'))
82
- ctrl_template = response.out
83
- end
84
- end
85
-
86
- # Render the template.
87
-
88
- def render_template(action, *params)
89
- File.read(find_template(action)) rescue nil
90
- end
91
-
92
- # go through the pipeline and call #transform on every object found there,
93
- # passing the template at that point.
94
- # the order and contents of the pipeline are determined by an array
95
- # in trait[:template_pipeline]
96
- # the default being [Element, Morpher, self]
97
-
98
- def pipeline(template)
99
- transform_pipeline = ancestral_trait[:transform_pipeline]
100
-
101
- transform_pipeline.inject(template) do |memo, current|
102
- current.transform(memo, binding)
103
- end
104
- end
105
-
106
- # transform a String to a final xhtml
107
- #
108
- # All ye who seek magic, look elsewhere, this method is ASAP (as simple as possible)
109
- #
110
- # There are some simple gsubs that build a final template which is evaluated
111
- #
112
- # The rules are following:
113
- # <?r rubycode ?>
114
- # evaluate the code inside the tag, this is considered XHTML-valid and so is the
115
- # preferred method for executing code inside your templates.
116
- # The return-value is ignored
117
- # <% rubycode %>
118
- # The same as <?r ?>, ERB-style and not valid XHTML, but should give someone who
119
- # is already familiar with ERB some common ground
120
- # #{ rubycode }
121
- # You know this from normal ruby already and it's actually nothing else.
122
- # Interpolation at the position in the template, isn't any special taggy format
123
- # and therefor safe to use.
124
- # <%= rubycode %>
125
- # The result of this will be interpolated at the position in the template.
126
- # Not valid XHTML either.
127
- #
128
- # Warning:
129
- # the variables used in here have the schema _variable_ to make it harder to break stuff
130
- # however, you should take care.
131
- # At the time of writing, the variables used are:
132
- # _start_heredoc_, _end_heredoc_, _template_, _out_, _file_ and _bufadd_
133
- # However, you may reuse _ivs_ if you desperatly need it and just can't live without.
134
- #
135
-
136
- def transform(_template_, _binding_ = binding)
137
- _start_heredoc_ = "T" << Digest::SHA1.hexdigest(_template_)
138
- _start_heredoc_, _end_heredoc_ = "\n<<#{_start_heredoc_}\n", "\n#{_start_heredoc_}\n"
139
- _bufadd_ = "_out_ << "
140
-
141
- _file_, _template_ = _template_, File.read(_template_) if File.file?(_template_)
142
-
143
- _template_.gsub!(/<%\s+(.*?)\s+%>/m,
144
- "#{_end_heredoc_} \\1; #{_bufadd_} #{_start_heredoc_}")
145
- _template_.gsub!(/<\?r\s+(.*?)\s+\?>/m,
146
- "#{_end_heredoc_} \\1; #{_bufadd_} #{_start_heredoc_}")
147
-
148
- _template_.gsub!(/<%=\s+(.*?)\s+%>/m,
149
- "#{_end_heredoc_} #{_bufadd_} (\\1); #{_bufadd_} #{_start_heredoc_}")
150
-
151
-
152
- # this one should not be used until we find and solution
153
- # that allows for stuff like
154
- # #[@foo]!
155
- # we just don't allow anything except space or newline
156
- # after the expression #[] to make it sane
157
- #_template_.gsub!(/#\[(.*?)\]\s*$/) do |m|
158
- # "#{_end_heredoc_} #{_bufadd_} (#{$1}); #{_bufadd_} #{_start_heredoc_}"
159
- #end
160
-
161
- _template_ = [_bufadd_, _start_heredoc_, _template_, _end_heredoc_].join(' ')
162
- _out_ = eval(*["_out_ = []; #{_template_}; _out_", _binding_, _file_].compact)
163
-
164
- _out_.map! do |line|
165
- line.to_s.chomp
166
- end
167
-
168
- _template_ = _out_.join.strip
169
- rescue Object => ex
170
- Informer.error "something bad happened while transformation"
171
- Informer.error ex
172
- #raise Error::Template, "Problem during transformation for: #{request.request_path}"
173
- {ex.message => _template_}.inspect
174
- end
175
-
176
- end
177
- end
@@ -1,166 +0,0 @@
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
- module Ramaze
5
-
6
- # an Element is almost like an Controller, however, instead
7
- # of connecting actions to templates it is only used in
8
- # Ramaze::Template::Ramaze and can be used inside the
9
- # templates of the Controller as a simple wrapper.
10
- #
11
- # Example:
12
- #
13
- # Your Element called Page:
14
- #
15
- # class Page < Element
16
- # def render
17
- # %{
18
- # <html>
19
- # <h1>
20
- # #{@hash['title']}
21
- # </h1>
22
- # #{content}
23
- # </html>
24
- # }
25
- # end
26
- # end
27
- #
28
- # and one called SideBar
29
- #
30
- # class SideBar < Element
31
- # def render
32
- # %{
33
- # <a href="http://something.com">something</a>
34
- # }
35
- # end
36
- # end
37
- #
38
- # and your template (any template for any action):
39
- #
40
- # <Page title="Test">
41
- # <SideBar />
42
- # <p>
43
- # Hello, World!
44
- # </p>
45
- # </Page>
46
- #
47
- # would result in:
48
- #
49
- # <html>
50
- # <h1>
51
- # Test
52
- # </h1>
53
- # <p>
54
- # Hello, World!
55
- # </p>
56
- # </html>
57
-
58
- class Element
59
- extend Ramaze::Helper
60
-
61
- helper :link, :redirect
62
-
63
- attr_accessor :content
64
-
65
- # this will be called by #transform, passes along the
66
- # stuff inside the tags for the element
67
-
68
- def initialize(content)
69
- @content = content
70
- end
71
-
72
- # The method that will be called upon to render the things
73
- # inside the element, you can access #content from here, which
74
- # contains the contents between the tags.
75
- #
76
- # It should answer with a String.
77
-
78
- def render *args
79
- @content
80
- end
81
-
82
- class << self
83
- # transforms all <Element> tags within the string, takes also
84
- # a binding to be compatible to the transform-pipeline, won't have
85
- # any use for it though.
86
- #
87
- # It also sets a instance-variable for you called @hash, which
88
- # contains the parameters you gave the <Element> tag.
89
- # See above for an example of writing and using them.
90
-
91
- def transform string = '', binding = nil
92
- string = string.to_s
93
- matches = string.scan(/<([A-Z][a-zA-Z0-9]*)(.*?)?>/)
94
-
95
- matches.each do |(klass, params)|
96
- transformer = (params[-1,1] == '/' ? :without : :with)
97
- string = send("transform_#{transformer}_content", string, klass)
98
- end
99
- string
100
- end
101
-
102
- # transforms elements like:
103
- # <Page> some content </Page>
104
-
105
- def transform_with_content(string, klass)
106
- string.gsub(/<#{klass}( .*?)?>(.*?)<\/#{klass}>/m) do |m|
107
- params, content = $1.to_s, $2.to_s
108
- finish_transform(klass, params, content)
109
- end
110
- end
111
-
112
- # transforms elements like:
113
- # <Page />
114
-
115
- def transform_without_content(string, klass)
116
- string.gsub(/<#{klass}( .*?)?\/>/) do |m|
117
- params = $1.to_s
118
- finish_transform(klass, params, content = '')
119
- end
120
- end
121
-
122
- # find the element, create an instance, pass it the content
123
- # check if it responds to :render and set an instance-variable
124
- # called @hash to hold the parameters passed to the element.
125
- #
126
- # Parameters look like:
127
- # <Page foo="true"> bar </Page>
128
- # <Page foo="true" />
129
-
130
- def finish_transform(klass, params, content)
131
- instance = constant(klass).new(content) rescue nil
132
-
133
- return unless instance and instance.respond_to?(:render)
134
-
135
- hash = demunge_passed_variables(params)
136
- instance.instance_variable_set("@hash", hash)
137
-
138
- instance.render
139
- end
140
-
141
- # basically processes stuff like
142
- # 'foo="bar" foobar="baz"'
143
- # do NOT pass actual objects that cannot be simply read as a string
144
- # here, the information will be lost.
145
- #
146
- # Exceptions are true, false, Integers and Floats. They will appear
147
- # in their real form (this again is also valid for strings that contain
148
- # these values in a way that makes Integer/Float possible to parse them)
149
- #
150
- # Just remember, walk like a duck, talk like a duck.
151
-
152
- def demunge_passed_variables(string)
153
- string.scan(/\s?(.*?)="(.*?)"/).inject({}) do |hash, (key, value)|
154
- value =
155
- case value
156
- when 'true' : true
157
- when 'false' : false
158
- else
159
- Integer(value) rescue Float(value) rescue value
160
- end
161
- hash.merge key => value
162
- end
163
- end
164
- end
165
- end
166
- end
@@ -1,156 +0,0 @@
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
- module Ramaze
5
-
6
- # This applies a morphing-replace for the template.
7
- #
8
- # To use the functionality of Morpher you will need to have hpricot
9
- # installed, you will get one error in case you don't and the method
10
- # will be replaced by a stub that simply returns the template.
11
- #
12
- # The method first checks if you use any morphers and just skips
13
- # the step if you don't, this should give quite some speedup for
14
- # smaller templates that don't use this functionality at all.
15
- # the check works by searching the morphs with appended '='
16
- # in the template. There may be a few cases where this won't work
17
- # since we cannot make any assumptions on the format.
18
- #
19
- # If you want to turn this functionality off, either remove Morpher
20
- # from:
21
- # Ramaze::Template::Ramaze.trait[:transform_pipeline]
22
- # or do:
23
- # Ramaze::Morpher.trait[:morphs] = {}
24
- #
25
- # The latter is a tad slower, but i mention the possibility in case you
26
- # find good use for it.
27
- #
28
- # You can add your own morphers in Ramaze::Morpher.trait[:morphs]
29
- #
30
- # For Example:
31
- #
32
- # Morpher.trait[:morphs]['if'] = '<?r %morph %expression ?>%content<?r end ?>'
33
- #
34
- # Now, assuming that some tag in your template is '<a if="@foo">x</a>'
35
- #
36
- # %morph stands for the name of your morph: 'if'
37
- # %expression is the stuff you write in the attribute: '@foo'
38
- # %content is the tag without the attribute (and all inside): '<a>x</a>'
39
-
40
- class Morpher
41
-
42
- # Use this trait to define your custom morphs.
43
- trait :morphs => {
44
- 'if' => '<?r %morph %expression ?>%content<?r end ?>',
45
- 'unless' => '<?r %morph %expression ?>%content<?r end ?>',
46
- 'for' => '<?r %morph %expression ?>%content<?r end ?>',
47
- 'each' => '<?r %expression.%morph do |_e| ?>%content<?r end ?>',
48
- 'times' => '<?r %expression.%morph do |_t| ?>%content<?r end ?>',
49
- }
50
-
51
- # Since the functionality is best explained by examples, here they come.
52
- #
53
- # Example:
54
- #
55
- # if:
56
- # <div if="@name">#@name</div>
57
- # morphs to:
58
- # <?r if @name ?>
59
- # <div>#@name</div>
60
- # <?r end ?>
61
- #
62
- # unless:
63
- # <div unless="@name">No Name</div>
64
- # morphs to:
65
- # <?r unless @name ?>
66
- # <div>No Name</div>
67
- # <?r end ?>
68
- #
69
- # for:
70
- # <div for="name in @names">#{name}</div>
71
- # morphs to:
72
- # <?r for name in @names ?>
73
- # <div>#{name}</div>
74
- # <?r end ?>
75
- #
76
- # times:
77
- # <div times="3">#{_t}<div>
78
- # morphs to:
79
- # <?r 3.times do |_t| ?>
80
- # <div>#{_t}</div>
81
- # <?r end ?>
82
- #
83
- # each:
84
- # <div each="[1,2,3]">#{_e}</div>
85
- # morphs to:
86
- # <?r [1,2,3].each do |_e| ?>
87
- # <div>#{_e}</div>
88
- # <?r end ?>
89
- #
90
- # The latter two examples show you also one standard introduced by a
91
- # limitation of the replacement-system.
92
- #
93
- # When you yield a value, please name it by the first character(s) of
94
- # the morphs name, with an underscore prefixed.
95
- #
96
- # for each an _e, for times a _t.
97
- #
98
- # This is by far not the best way to handle it and might lead to problems
99
- # due to the lack of proper scoping in ruby (if you define an _e or _t
100
- # before the block it will be overwritten).
101
- #
102
- # So please be careful, I tried to come up with something that is both easy
103
- # to write and doesn't look outright awful while keeping an easy to remember
104
- # mnemonic.
105
- #
106
- # TODO:
107
- # - Add pure Ruby implementation as a fall-back.
108
-
109
- def self.transform template, bound = nil
110
- morphs =
111
- trait[:morphs].map{|k,v| [k.to_s, v.to_s]}.select do |(k,v)|
112
- template.to_s.include?("#{k}=")
113
- end
114
-
115
- morphs = Hash[*morphs.flatten]
116
-
117
- return template if morphs.empty?
118
-
119
- require 'hpricot'
120
-
121
- hp = Hpricot(template)
122
- hp.each_child do |child|
123
- if child.elem?
124
- morphs.each_pair do |morph, replacement|
125
- if expression = child[morph]
126
- old = child.to_html
127
- child.remove_attribute(morph)
128
-
129
- replacement = replacement.dup.
130
- gsub('%morph', morph).
131
- gsub('%expression', expression).
132
- gsub('%content', child.to_html)
133
-
134
- template.gsub!(old, replacement)
135
- end
136
- end
137
- end
138
- end
139
-
140
- template
141
-
142
- rescue LoadError => ex
143
- error "Please install hpricot (for example via `gem install hpricot`) to get morphing"
144
-
145
- # replace this method with a stub that only returns the template.
146
-
147
- self.class_eval do
148
- def self.transform(template, bound = nil)
149
- template
150
- end
151
- end
152
-
153
- template
154
- end
155
- end
156
- end