ramaze 0.0.6
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.
- data/Rakefile +360 -0
- data/bin/ramaze +152 -0
- data/doc/CHANGELOG +2021 -0
- data/doc/COPYING +56 -0
- data/doc/COPYING.ja +51 -0
- data/doc/README +275 -0
- data/doc/TODO +33 -0
- data/doc/allison/LICENSE +184 -0
- data/doc/allison/README +37 -0
- data/doc/allison/allison.css +300 -0
- data/doc/allison/allison.gif +0 -0
- data/doc/allison/allison.js +307 -0
- data/doc/allison/allison.rb +287 -0
- data/doc/allison/cache/BODY +588 -0
- data/doc/allison/cache/CLASS_INDEX +4 -0
- data/doc/allison/cache/CLASS_PAGE +1 -0
- data/doc/allison/cache/FILE_INDEX +4 -0
- data/doc/allison/cache/FILE_PAGE +1 -0
- data/doc/allison/cache/FONTS +1 -0
- data/doc/allison/cache/FR_INDEX_BODY +1 -0
- data/doc/allison/cache/IMGPATH +1 -0
- data/doc/allison/cache/INDEX +1 -0
- data/doc/allison/cache/JAVASCRIPT +307 -0
- data/doc/allison/cache/METHOD_INDEX +4 -0
- data/doc/allison/cache/METHOD_LIST +1 -0
- data/doc/allison/cache/SRC_PAGE +1 -0
- data/doc/allison/cache/STYLE +322 -0
- data/doc/allison/cache/URL +1 -0
- data/examples/blog/main.rb +16 -0
- data/examples/blog/public/screen.css +106 -0
- data/examples/blog/src/controller.rb +50 -0
- data/examples/blog/src/element.rb +53 -0
- data/examples/blog/src/model.rb +29 -0
- data/examples/blog/template/edit.xhtml +6 -0
- data/examples/blog/template/index.xhtml +24 -0
- data/examples/blog/template/new.xhtml +5 -0
- data/examples/blog/template/view.xhtml +15 -0
- data/examples/blog/test/tc_entry.rb +18 -0
- data/examples/caching.rb +23 -0
- data/examples/element.rb +40 -0
- data/examples/hello.rb +23 -0
- data/examples/simple.rb +60 -0
- data/examples/templates/template/external.haml +21 -0
- data/examples/templates/template/external.liquid +28 -0
- data/examples/templates/template/external.mab +27 -0
- data/examples/templates/template/external.rhtml +29 -0
- data/examples/templates/template/external.rmze +24 -0
- data/examples/templates/template_erubis.rb +50 -0
- data/examples/templates/template_haml.rb +48 -0
- data/examples/templates/template_liquid.rb +64 -0
- data/examples/templates/template_markaby.rb +52 -0
- data/examples/templates/template_ramaze.rb +49 -0
- data/examples/whywiki/main.rb +56 -0
- data/examples/whywiki/template/edit.xhtml +14 -0
- data/examples/whywiki/template/show.xhtml +17 -0
- data/lib/proto/conf/benchmark.yaml +35 -0
- data/lib/proto/conf/debug.yaml +34 -0
- data/lib/proto/conf/live.yaml +33 -0
- data/lib/proto/conf/silent.yaml +31 -0
- data/lib/proto/conf/stage.yaml +33 -0
- data/lib/proto/main.rb +18 -0
- data/lib/proto/public/404.jpg +0 -0
- data/lib/proto/public/css/coderay.css +105 -0
- data/lib/proto/public/css/ramaze_error.css +42 -0
- data/lib/proto/public/error.xhtml +74 -0
- data/lib/proto/public/favicon.ico +0 -0
- data/lib/proto/public/js/jquery.js +1923 -0
- data/lib/proto/public/ramaze.png +0 -0
- data/lib/proto/src/controller/main.rb +7 -0
- data/lib/proto/src/element/page.rb +16 -0
- data/lib/proto/src/model.rb +5 -0
- data/lib/proto/template/index.xhtml +6 -0
- data/lib/ramaze.rb +317 -0
- data/lib/ramaze/adapter/mongrel.rb +111 -0
- data/lib/ramaze/adapter/webrick.rb +161 -0
- data/lib/ramaze/cache.rb +11 -0
- data/lib/ramaze/cache/memcached.rb +52 -0
- data/lib/ramaze/cache/memory.rb +6 -0
- data/lib/ramaze/cache/yaml_store.rb +37 -0
- data/lib/ramaze/controller.rb +10 -0
- data/lib/ramaze/dispatcher.rb +315 -0
- data/lib/ramaze/error.rb +11 -0
- data/lib/ramaze/gestalt.rb +108 -0
- data/lib/ramaze/global.rb +120 -0
- data/lib/ramaze/helper.rb +32 -0
- data/lib/ramaze/helper/aspect.rb +189 -0
- data/lib/ramaze/helper/auth.rb +120 -0
- data/lib/ramaze/helper/cache.rb +52 -0
- data/lib/ramaze/helper/feed.rb +135 -0
- data/lib/ramaze/helper/form.rb +204 -0
- data/lib/ramaze/helper/link.rb +80 -0
- data/lib/ramaze/helper/redirect.rb +48 -0
- data/lib/ramaze/helper/stack.rb +67 -0
- data/lib/ramaze/http_status.rb +66 -0
- data/lib/ramaze/inform.rb +166 -0
- data/lib/ramaze/snippets.rb +5 -0
- data/lib/ramaze/snippets/hash/keys_to_sym.rb +19 -0
- data/lib/ramaze/snippets/kernel/aquire.rb +22 -0
- data/lib/ramaze/snippets/kernel/autoreload.rb +79 -0
- data/lib/ramaze/snippets/kernel/caller_lines.rb +58 -0
- data/lib/ramaze/snippets/kernel/constant.rb +24 -0
- data/lib/ramaze/snippets/kernel/rescue_require.rb +12 -0
- data/lib/ramaze/snippets/kernel/self_method.rb +41 -0
- data/lib/ramaze/snippets/kernel/silently.rb +13 -0
- data/lib/ramaze/snippets/object/traits.rb +60 -0
- data/lib/ramaze/snippets/openstruct/temp.rb +10 -0
- data/lib/ramaze/snippets/string/DIVIDE.rb +16 -0
- data/lib/ramaze/snippets/string/camel_case.rb +14 -0
- data/lib/ramaze/snippets/string/snake_case.rb +12 -0
- data/lib/ramaze/snippets/symbol/to_proc.rb +14 -0
- data/lib/ramaze/snippets/thread/deadQUESTIONMARK.rb +11 -0
- data/lib/ramaze/store/default.rb +48 -0
- data/lib/ramaze/template.rb +102 -0
- data/lib/ramaze/template/amrita2.rb +40 -0
- data/lib/ramaze/template/erubis.rb +58 -0
- data/lib/ramaze/template/haml.rb +65 -0
- data/lib/ramaze/template/haml/actionview_stub.rb +20 -0
- data/lib/ramaze/template/liquid.rb +74 -0
- data/lib/ramaze/template/markaby.rb +68 -0
- data/lib/ramaze/template/ramaze.rb +177 -0
- data/lib/ramaze/template/ramaze/element.rb +166 -0
- data/lib/ramaze/template/ramaze/morpher.rb +156 -0
- data/lib/ramaze/tool/create.rb +70 -0
- data/lib/ramaze/tool/tidy.rb +71 -0
- data/lib/ramaze/trinity.rb +38 -0
- data/lib/ramaze/trinity/request.rb +244 -0
- data/lib/ramaze/trinity/response.rb +41 -0
- data/lib/ramaze/trinity/session.rb +129 -0
- data/lib/ramaze/version.rb +14 -0
- data/spec/spec_all.rb +73 -0
- data/spec/spec_helper.rb +215 -0
- data/spec/tc_adapter_mongrel.rb +24 -0
- data/spec/tc_adapter_webrick.rb +22 -0
- data/spec/tc_cache.rb +79 -0
- data/spec/tc_controller.rb +39 -0
- data/spec/tc_element.rb +100 -0
- data/spec/tc_error.rb +23 -0
- data/spec/tc_gestalt.rb +90 -0
- data/spec/tc_global.rb +46 -0
- data/spec/tc_helper_aspect.rb +65 -0
- data/spec/tc_helper_auth.rb +61 -0
- data/spec/tc_helper_cache.rb +81 -0
- data/spec/tc_helper_feed.rb +129 -0
- data/spec/tc_helper_form.rb +146 -0
- data/spec/tc_helper_link.rb +58 -0
- data/spec/tc_helper_redirect.rb +51 -0
- data/spec/tc_helper_stack.rb +55 -0
- data/spec/tc_morpher.rb +90 -0
- data/spec/tc_params.rb +84 -0
- data/spec/tc_request.rb +111 -0
- data/spec/tc_session.rb +56 -0
- data/spec/tc_store.rb +25 -0
- data/spec/tc_template_amrita2.rb +34 -0
- data/spec/tc_template_erubis.rb +41 -0
- data/spec/tc_template_haml.rb +44 -0
- data/spec/tc_template_liquid.rb +98 -0
- data/spec/tc_template_markaby.rb +74 -0
- data/spec/tc_template_ramaze.rb +54 -0
- data/spec/tc_tidy.rb +14 -0
- data/spec/template/amrita2/data.html +6 -0
- data/spec/template/amrita2/index.html +1 -0
- data/spec/template/amrita2/sum.html +1 -0
- data/spec/template/erubis/sum.rhtml +1 -0
- data/spec/template/haml/index.haml +5 -0
- data/spec/template/haml/with_vars.haml +4 -0
- data/spec/template/liquid/index.liquid +1 -0
- data/spec/template/liquid/products.liquid +45 -0
- data/spec/template/markaby/external.mab +8 -0
- data/spec/template/markaby/sum.mab +1 -0
- data/spec/template/ramaze/file_only.rmze +1 -0
- data/spec/template/ramaze/index.rmze +1 -0
- data/spec/template/ramaze/nested.rmze +1 -0
- data/spec/template/ramaze/sum.rmze +1 -0
- metadata +317 -0
data/lib/ramaze/error.rb
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
module Error
|
|
6
|
+
class NoAction < StandardError; end
|
|
7
|
+
class NoController < StandardError; end
|
|
8
|
+
class WrongParameterCount < StandardError; end
|
|
9
|
+
class Template < StandardError; end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
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
|
+
=begin rdoc
|
|
5
|
+
Example:
|
|
6
|
+
|
|
7
|
+
require 'ramaze'
|
|
8
|
+
require 'ramaze/gestalt'
|
|
9
|
+
|
|
10
|
+
def random_color
|
|
11
|
+
r = lambda{|n| rand(n).to_s(16) }
|
|
12
|
+
'#' + [255, 255, 255].map{r[255] + r[255] + r[255]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
puts Ramaze::Gestalt.build{
|
|
16
|
+
html do
|
|
17
|
+
head do
|
|
18
|
+
title{"Hello World"}
|
|
19
|
+
end
|
|
20
|
+
body do
|
|
21
|
+
h1{"Hello, World!"}
|
|
22
|
+
div(:style => 'width:100%') do
|
|
23
|
+
10.times do
|
|
24
|
+
div(:style => "width:#{rand(100)}%;height:#{rand(100)}%;background:#{random_color}"){ ' ' }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
}
|
|
30
|
+
=end
|
|
31
|
+
|
|
32
|
+
module Ramaze
|
|
33
|
+
|
|
34
|
+
# Gestalt is the custom HTML/XML builder for Ramaze, based on a very simple
|
|
35
|
+
# DSL it will build your markup.
|
|
36
|
+
|
|
37
|
+
class Gestalt
|
|
38
|
+
attr_accessor :out
|
|
39
|
+
|
|
40
|
+
# The default way to start building your markup.
|
|
41
|
+
# Takes a block and returns the markup.
|
|
42
|
+
#
|
|
43
|
+
# Example:
|
|
44
|
+
# html =
|
|
45
|
+
# Gestalt.build do
|
|
46
|
+
# html do
|
|
47
|
+
# head do
|
|
48
|
+
# title "Hello, World!"
|
|
49
|
+
# end
|
|
50
|
+
# body do
|
|
51
|
+
# h1 "Hello, World!"
|
|
52
|
+
# end
|
|
53
|
+
# end
|
|
54
|
+
# end
|
|
55
|
+
#
|
|
56
|
+
|
|
57
|
+
def self.build &block
|
|
58
|
+
self.new(&block).to_s
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Gestalt.new is like ::build but will return itself.
|
|
62
|
+
# you can either access #out or .to_s it, which will
|
|
63
|
+
# return the actual markup.
|
|
64
|
+
#
|
|
65
|
+
# Useful for distributed building of one page.
|
|
66
|
+
|
|
67
|
+
def initialize &block
|
|
68
|
+
@out = ''
|
|
69
|
+
instance_eval(&block) if block_given?
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# catching all the tags. passing it to _gestalt_build_tag
|
|
73
|
+
|
|
74
|
+
def method_missing meth, *args, &block
|
|
75
|
+
_gestalt_build_tag meth, *args, &block
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# workaround for Kernel#p to make <p /> tags possible.
|
|
79
|
+
|
|
80
|
+
def p *args, &block
|
|
81
|
+
_gestalt_build_tag :p, *args, &block
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# build a tag for `name`, using `args` and an optional block that
|
|
85
|
+
# will be yielded
|
|
86
|
+
|
|
87
|
+
def _gestalt_build_tag name, args = []
|
|
88
|
+
@out << "<#{name}"
|
|
89
|
+
if block_given?
|
|
90
|
+
@out << args.inject(''){ |s,v| s << %{ #{v[0]}="#{v[1]}"} }
|
|
91
|
+
@out << ">"
|
|
92
|
+
text = yield
|
|
93
|
+
@out << text if text != @out and text.respond_to?(:to_str)
|
|
94
|
+
@out << "</#{name}>"
|
|
95
|
+
else
|
|
96
|
+
@out << args.inject(''){ |s,v| s << %{ #{v[0]}="#{v[1]}"} }
|
|
97
|
+
@out << ' />'
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @out.to_s
|
|
102
|
+
|
|
103
|
+
def to_s
|
|
104
|
+
@out.to_s
|
|
105
|
+
end
|
|
106
|
+
alias to_str to_s
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
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 'ostruct'
|
|
5
|
+
require 'set'
|
|
6
|
+
|
|
7
|
+
module Ramaze
|
|
8
|
+
class GlobalStruct < OpenStruct
|
|
9
|
+
# autoreload - Interval for autoreloading changed source in seconds
|
|
10
|
+
# adapter - Webserver-adapter ( :mongrel | :webrick )
|
|
11
|
+
# cache - Cache to use ( MemcachedCache | MemoryCache | YamlStoreCache )
|
|
12
|
+
# cache_actions - Finegrained caching based on actions (see CacheHelper)
|
|
13
|
+
# cache_all - Naive caching for all responses ( true | false )
|
|
14
|
+
# error_page - Show default errorpage with inspection and backtrace ( true | false )
|
|
15
|
+
# host - Host to respond to ( '0.0.0.0' )
|
|
16
|
+
# mapping - Route to controller map ( {} )
|
|
17
|
+
# port - First port of the port-range the adapters run on. ( 7000 )
|
|
18
|
+
# run_loose - Don't wait for the servers to finish, useful for testing ( true | false )
|
|
19
|
+
# tidy - Run all text/html responses through Tidy ( true | false )
|
|
20
|
+
# template_root - Default directory for your templates.
|
|
21
|
+
#
|
|
22
|
+
# inform_to - :stdout/'stdout'/$stderr (similar for stdout) or some path to a file
|
|
23
|
+
# inform_tags - a Set with any of [ :debug, :info, :error ]
|
|
24
|
+
# inform_backtrace_size - size of backtrace to be logged (and show on error-page).
|
|
25
|
+
# inform_timestamp - parameter for Time.strftime
|
|
26
|
+
# inform_prefix_info - prefix for all the Inform#info messages
|
|
27
|
+
# inform_prefix_debug - prefix for all the Inform#debug messages
|
|
28
|
+
# inform_prefix_error - prefix for all the Inform#error messages
|
|
29
|
+
#
|
|
30
|
+
# startup - List of methods and lambdas that are executed on startup
|
|
31
|
+
# ramaze_startup - Internal list of methods and lambdas that are executed on startup
|
|
32
|
+
#
|
|
33
|
+
# shutdown - List of methods and lambdas that are executed on startup
|
|
34
|
+
# ramaze_shutdown - Internal list of methods and lambdas that are executed on shutdown
|
|
35
|
+
|
|
36
|
+
DEFAULT = {
|
|
37
|
+
:autoreload => 5,
|
|
38
|
+
:adapter => :webrick,
|
|
39
|
+
:cache => MemoryCache,
|
|
40
|
+
:cache_actions => Hash.new{|h,k| h[k] = Set.new},
|
|
41
|
+
:cache_all => false,
|
|
42
|
+
:cookies => true,
|
|
43
|
+
:error_page => true,
|
|
44
|
+
:host => '0.0.0.0',
|
|
45
|
+
:mapping => {},
|
|
46
|
+
:port => 7000,
|
|
47
|
+
:run_loose => false,
|
|
48
|
+
:tidy => false,
|
|
49
|
+
:template_root => 'template',
|
|
50
|
+
|
|
51
|
+
:inform_to => $stdout,
|
|
52
|
+
:inform_tags => Set.new([:debug, :info, :error]),
|
|
53
|
+
:inform_backtrace_size => 10,
|
|
54
|
+
:inform_timestamp => "%Y-%m-%d %H:%M:%S",
|
|
55
|
+
:inform_prefix_info => 'INFO ',
|
|
56
|
+
:inform_prefix_debug => 'DEBUG',
|
|
57
|
+
:inform_prefix_error => 'ERROR',
|
|
58
|
+
|
|
59
|
+
:startup => [],
|
|
60
|
+
:ramaze_startup => [
|
|
61
|
+
:find_controllers, :setup_controllers, :init_autoreload, :init_adapter
|
|
62
|
+
],
|
|
63
|
+
|
|
64
|
+
:shutdown => [],
|
|
65
|
+
:ramaze_shutdown => [
|
|
66
|
+
:kill_threads,
|
|
67
|
+
:close_inform,
|
|
68
|
+
lambda{ puts "Shutdown Ramaze (it's save to kill me now if i hang)" },
|
|
69
|
+
:exit
|
|
70
|
+
],
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
# takes an hash of options and optionally an block that is evaled in this
|
|
74
|
+
# instance of GlobalStruct.
|
|
75
|
+
|
|
76
|
+
def setup hash = {}, &block
|
|
77
|
+
Global.instance_eval(&block) if block_given?
|
|
78
|
+
table.merge!( hash.keys_to_sym )
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# just update the hash, not deleting values already set.
|
|
82
|
+
# again, takes a block, but your assignments may be
|
|
83
|
+
# overwritten if they existed before.
|
|
84
|
+
|
|
85
|
+
def update hash = {}, &block
|
|
86
|
+
old_table = table.dup
|
|
87
|
+
Global.instance_eval(&block) if block_given?
|
|
88
|
+
table.merge!( hash.keys_to_sym.merge( old_table ) )
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# synonym to Global.key = value
|
|
92
|
+
|
|
93
|
+
def []=(key, value)
|
|
94
|
+
table[key.to_sym] = value
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# synonym for Global.key
|
|
98
|
+
|
|
99
|
+
def [](key)
|
|
100
|
+
table[key.to_sym]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# get all the values for the given keys in the right order.
|
|
104
|
+
|
|
105
|
+
def values_at(*keys)
|
|
106
|
+
table.values_at(*keys.map(&:to_sym))
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# iterate over the GlobalStruct, no guarantee on the order.
|
|
110
|
+
|
|
111
|
+
def each
|
|
112
|
+
table.each do |e|
|
|
113
|
+
yield(e)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
Thread.current[:global] = GlobalStruct.new(GlobalStruct::DEFAULT)
|
|
119
|
+
Global = Thread.current[:global]
|
|
120
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
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/trinity'
|
|
5
|
+
|
|
6
|
+
module Ramaze
|
|
7
|
+
|
|
8
|
+
# A module used by the Templates and the Controllers
|
|
9
|
+
# it provides both Ramaze::Trinity (request/response/session)
|
|
10
|
+
# and also a helper method, look below for more information about it
|
|
11
|
+
|
|
12
|
+
module Helper
|
|
13
|
+
include Trinity
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
# This loads the helper-files from /ramaze/helper/helpername.rb and
|
|
18
|
+
# includes it into Ramaze::Template (or wherever it is called)
|
|
19
|
+
#
|
|
20
|
+
# Usage:
|
|
21
|
+
# helper :redirect, :link
|
|
22
|
+
|
|
23
|
+
def helper *syms
|
|
24
|
+
syms.each do |sym|
|
|
25
|
+
mod_name = sym.to_s.capitalize + 'Helper'
|
|
26
|
+
require "ramaze/helper/#{sym}"
|
|
27
|
+
include ::Ramaze.const_get(mod_name)
|
|
28
|
+
extend ::Ramaze.const_get(mod_name)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,189 @@
|
|
|
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
|
+
# A helper that provides the means to wrap actions of the controller with
|
|
6
|
+
# other methods.
|
|
7
|
+
# For examples please look at the test/tc_aspect.rb
|
|
8
|
+
# this is not a default helper due to the possible performance-issues.
|
|
9
|
+
# However, it should be only an overhead of about 6-8 calls, so if you
|
|
10
|
+
# want this feature it shouldn't have too large impact ;)
|
|
11
|
+
|
|
12
|
+
module AspectHelper
|
|
13
|
+
|
|
14
|
+
# define the trait[:aspects] for the class that includes us
|
|
15
|
+
# also prepare hijacking of the :render method
|
|
16
|
+
|
|
17
|
+
def self.included(klass)
|
|
18
|
+
# if we haven't been included yet...
|
|
19
|
+
unless defined?(Traits[klass][:aspects]) and Traits[klass][:aspects]
|
|
20
|
+
Traits[klass] = {:aspects => {:pre => {}, :post => {}, :wrap => {}}}
|
|
21
|
+
unless defined?(klass.old_render)
|
|
22
|
+
klass.send(:alias_method, :old_render, :render)
|
|
23
|
+
klass.send(:alias_method, :render, :new_render)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
# define pre-aspect which calls render(:your_pre_aspect)
|
|
31
|
+
# and appends it on your default action
|
|
32
|
+
# please note, that if you give can define
|
|
33
|
+
# pre :all, :all_, :except => [:action1, :action2]
|
|
34
|
+
# pre :all, :all_, :except => :action
|
|
35
|
+
# however, due to the nature of this helper, only action that have been
|
|
36
|
+
# defined so far are wrapped by the :all.
|
|
37
|
+
# methods that are in the :except (which is not mandatory) are ignored.
|
|
38
|
+
#
|
|
39
|
+
# the notion :all_ is a nice reminder that it is a pre-wrapper, you don't
|
|
40
|
+
# have to use the underscore, it's just my way to do it.
|
|
41
|
+
#
|
|
42
|
+
# Also, be careful, since :all will wrap other wraps that have been defined
|
|
43
|
+
# so far as well.
|
|
44
|
+
# Usually i don't think it's a good thing that order defines
|
|
45
|
+
# behaviour, but in this case it gives you quite powerful control
|
|
46
|
+
# over the whole process. Just watch out:
|
|
47
|
+
# With great power comes great responsibility
|
|
48
|
+
#
|
|
49
|
+
# Example:
|
|
50
|
+
# class FooController < Template::Ramaze
|
|
51
|
+
#
|
|
52
|
+
# def index
|
|
53
|
+
# 'foo'
|
|
54
|
+
# end
|
|
55
|
+
# pre :index, :_index
|
|
56
|
+
#
|
|
57
|
+
# def other
|
|
58
|
+
# 'foo'
|
|
59
|
+
# end
|
|
60
|
+
# pre :all, :all_, :except => :index
|
|
61
|
+
#
|
|
62
|
+
# def _index
|
|
63
|
+
# 'I will be put before your action'
|
|
64
|
+
# end
|
|
65
|
+
#
|
|
66
|
+
# def _all
|
|
67
|
+
# '<pre>'
|
|
68
|
+
# end
|
|
69
|
+
# end
|
|
70
|
+
|
|
71
|
+
def pre(*opts)
|
|
72
|
+
enwrap(:pre, *opts)
|
|
73
|
+
end
|
|
74
|
+
alias before pre
|
|
75
|
+
|
|
76
|
+
# define post-aspect which calls render(:your_post_aspect)
|
|
77
|
+
# and appends it on your default action
|
|
78
|
+
# please note, that if you give can define
|
|
79
|
+
# post :all, :all_, :except => [:action1, :action2]
|
|
80
|
+
# post :all, :all_, :except => :action
|
|
81
|
+
# however, due to the nature of this helper, only action that have been
|
|
82
|
+
# defined so far are wrapped by the :all.
|
|
83
|
+
# methods that are in the :except (which is not mandatory) are ignored.
|
|
84
|
+
#
|
|
85
|
+
# the notion :all_ is a nice reminder that it is a post-wrapper, you don't
|
|
86
|
+
# have to use the underscore, it's just my way to do it.
|
|
87
|
+
#
|
|
88
|
+
# Also, be careful, since :all will wrap other wraps that have been defined
|
|
89
|
+
# so far as well.
|
|
90
|
+
# Usually i don't think it's a good thing that order defines
|
|
91
|
+
# behaviour, but in this case it gives you quite powerful control
|
|
92
|
+
# over the whole process. Just watch out:
|
|
93
|
+
# With great power comes great responsibility
|
|
94
|
+
#
|
|
95
|
+
# Example:
|
|
96
|
+
# class FooController < Template::Ramaze
|
|
97
|
+
#
|
|
98
|
+
# def index
|
|
99
|
+
# 'foo'
|
|
100
|
+
# end
|
|
101
|
+
# post :index, :index_
|
|
102
|
+
#
|
|
103
|
+
# def other
|
|
104
|
+
# 'foo'
|
|
105
|
+
# end
|
|
106
|
+
# pre :all, :all_, :except => :index
|
|
107
|
+
#
|
|
108
|
+
# def index_
|
|
109
|
+
# 'I will be put after your action'
|
|
110
|
+
# end
|
|
111
|
+
#
|
|
112
|
+
# def all_
|
|
113
|
+
# '</pre>'
|
|
114
|
+
# end
|
|
115
|
+
# end
|
|
116
|
+
|
|
117
|
+
def post(*opts)
|
|
118
|
+
enwrap(:post, *opts)
|
|
119
|
+
end
|
|
120
|
+
alias after post
|
|
121
|
+
|
|
122
|
+
# a shortcut that combines pre and post
|
|
123
|
+
# same syntax as pre/post, just a linesaver
|
|
124
|
+
|
|
125
|
+
def wrap(*opts)
|
|
126
|
+
pre(*opts)
|
|
127
|
+
post(*opts)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# you shouldn't have to call this method directly
|
|
131
|
+
# however, if you really, really want to:
|
|
132
|
+
#
|
|
133
|
+
# enwrap(:post, :index, :my_post_method, :except => :bar
|
|
134
|
+
|
|
135
|
+
def enwrap(kind, key, meths, hash = {})
|
|
136
|
+
wrapping =
|
|
137
|
+
if key == :all
|
|
138
|
+
instance_methods(false).map{|im| im.to_sym}
|
|
139
|
+
else
|
|
140
|
+
if ([] + key rescue nil)
|
|
141
|
+
key.map{|k| k.to_sym}
|
|
142
|
+
else
|
|
143
|
+
[key.to_sym]
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
if hash[:except]
|
|
148
|
+
wrapping -= [hash[:except]].flatten.map{|m| m.to_sym}
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
wrapping.each do |meth|
|
|
152
|
+
ancestral_trait[:aspects][kind][meth] = meths
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# find the post and pre actions for the current class
|
|
157
|
+
|
|
158
|
+
def resolve_aspect(action)
|
|
159
|
+
action = action.to_sym
|
|
160
|
+
aspects = ancestral_trait[:aspects]
|
|
161
|
+
{
|
|
162
|
+
:pre => aspects[:pre][action],
|
|
163
|
+
:post => aspects[:post][action]
|
|
164
|
+
}
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# this will be exchanged for the :render method which is aliased
|
|
168
|
+
# to old_render
|
|
169
|
+
# it just searches for post and pre wrappers, calls them
|
|
170
|
+
# before/after your action and joins the results
|
|
171
|
+
|
|
172
|
+
def new_render(action, *params)
|
|
173
|
+
arity_for = lambda{|meth| method(meth).arity rescue -1 }
|
|
174
|
+
post, pre = resolve_aspect(action).values_at(:post, :pre)
|
|
175
|
+
|
|
176
|
+
if pre
|
|
177
|
+
arity = arity_for[pre].abs
|
|
178
|
+
pre_content = old_render(pre, *params[0,arity]) if pre
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
unless (pre_content.delete(:skip_next_aspects) rescue false)
|
|
182
|
+
content = old_render(action, *params)
|
|
183
|
+
post_content = old_render(post, *params) if post
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
[pre_content, content, post_content].join
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|