ramaze 0.3.5 → 0.3.9
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +5 -20
- data/bin/ramaze +0 -4
- data/doc/AUTHORS +5 -0
- data/doc/meta/announcement.txt +2 -1
- data/doc/tutorial/todolist.html +20 -21
- data/doc/tutorial/todolist.mkd +10 -9
- data/examples/blog/{src/controller.rb → controller/main.rb} +2 -0
- data/examples/blog/{src/model.rb → model/entry.rb} +0 -0
- data/examples/blog/spec/blog.rb +3 -3
- data/examples/blog/start.rb +2 -3
- data/examples/blog/view/edit.xhtml +17 -0
- data/examples/blog/view/index.xhtml +17 -0
- data/examples/blog/view/layout.xhtml +11 -0
- data/examples/blog/view/new.xhtml +16 -0
- data/examples/facebook.rb +15 -8
- data/examples/identity.rb +1 -1
- data/examples/memleak_detector.rb +1 -1
- data/examples/rammit/src/model.rb +1 -1
- data/examples/rapaste/controller/paste.rb +7 -7
- data/examples/rapaste/model/paste.rb +1 -3
- data/examples/rapaste/public/css/display.css +2 -1
- data/examples/rapaste/start.rb +2 -3
- data/examples/sourceview/sourceview.rb +1 -1
- data/examples/templates/template/external.tenjin +28 -0
- data/examples/templates/template_amrita2.rb +0 -3
- data/examples/templates/template_tenjin.rb +57 -0
- data/examples/todolist/spec/todolist.rb +13 -2
- data/examples/todolist/src/controller/main.rb +1 -1
- data/examples/todolist/template/index.xhtml +1 -1
- data/examples/wiktacular/spec/wiktacular.rb +18 -0
- data/examples/wiktacular/src/controller.rb +2 -2
- data/examples/wiktacular/src/model.rb +8 -2
- data/lib/proto/public/css/ramaze_error.css +4 -0
- data/lib/proto/public/dispatch.fcgi +11 -0
- data/lib/proto/spec/main.rb +1 -1
- data/lib/proto/start.ru +8 -0
- data/lib/proto/view/error.xhtml +2 -0
- data/lib/ramaze.rb +28 -5
- data/lib/ramaze/action.rb +9 -2
- data/lib/ramaze/action/render.rb +40 -24
- data/lib/ramaze/adapter.rb +10 -17
- data/lib/ramaze/adapter/base.rb +8 -12
- data/lib/ramaze/adapter/cgi.rb +14 -13
- data/lib/ramaze/adapter/ebb.rb +34 -0
- data/lib/ramaze/adapter/evented_mongrel.rb +1 -1
- data/lib/ramaze/adapter/fcgi.rb +14 -14
- data/lib/ramaze/adapter/lsws.rb +15 -11
- data/lib/ramaze/adapter/mongrel.rb +2 -1
- data/lib/ramaze/adapter/scgi.rb +24 -0
- data/lib/ramaze/adapter/swiftiplied_mongrel.rb +1 -1
- data/lib/ramaze/adapter/thin.rb +4 -5
- data/lib/ramaze/adapter/webrick.rb +5 -5
- data/lib/ramaze/cache.rb +1 -1
- data/lib/ramaze/cache/memcached.rb +1 -1
- data/lib/ramaze/contrib.rb +70 -10
- data/lib/ramaze/contrib/auto_params.rb +1 -1
- data/lib/ramaze/contrib/email.rb +15 -15
- data/lib/ramaze/contrib/facebook.rb +2 -2
- data/lib/ramaze/contrib/facebook/facebook.rb +7 -4
- data/lib/ramaze/contrib/file_cache.rb +65 -0
- data/lib/ramaze/contrib/gettext.rb +56 -55
- data/lib/ramaze/contrib/profiling.rb +36 -0
- data/lib/ramaze/contrib/sequel_cache.rb +92 -0
- data/lib/ramaze/controller.rb +80 -47
- data/lib/ramaze/controller/error.rb +10 -5
- data/lib/ramaze/controller/resolve.rb +36 -48
- data/lib/ramaze/current.rb +70 -0
- data/lib/ramaze/{trinity → current}/request.rb +62 -15
- data/lib/ramaze/current/response.rb +19 -0
- data/lib/ramaze/{trinity → current}/session.rb +32 -110
- data/lib/ramaze/current/session/flash.rb +67 -0
- data/lib/ramaze/current/session/hash.rb +65 -0
- data/lib/ramaze/dispatcher.rb +1 -28
- data/lib/ramaze/dispatcher/action.rb +6 -3
- data/lib/ramaze/dispatcher/directory.rb +1 -1
- data/lib/ramaze/dispatcher/error.rb +26 -5
- data/lib/ramaze/dispatcher/file.rb +13 -2
- data/lib/ramaze/gestalt.rb +3 -1
- data/lib/ramaze/global.rb +6 -3
- data/lib/ramaze/global/globalstruct.rb +3 -1
- data/lib/ramaze/helper.rb +66 -20
- data/lib/ramaze/helper/aspect.rb +25 -17
- data/lib/ramaze/helper/auth.rb +4 -3
- data/lib/ramaze/helper/cache.rb +5 -4
- data/lib/ramaze/helper/cgi.rb +11 -9
- data/lib/ramaze/helper/flash.rb +28 -3
- data/lib/ramaze/helper/formatting.rb +1 -3
- data/lib/ramaze/helper/identity.rb +2 -3
- data/lib/ramaze/helper/inform.rb +7 -6
- data/lib/ramaze/helper/link.rb +15 -17
- data/lib/ramaze/helper/markaby.rb +2 -4
- data/lib/ramaze/helper/maruku.rb +1 -1
- data/lib/ramaze/helper/nitroform.rb +4 -4
- data/lib/ramaze/helper/pager.rb +4 -6
- data/lib/ramaze/helper/partial.rb +24 -21
- data/lib/ramaze/helper/redirect.rb +8 -5
- data/lib/ramaze/helper/{file.rb → sendfile.rb} +1 -3
- data/lib/ramaze/helper/sequel.rb +1 -3
- data/lib/ramaze/helper/stack.rb +1 -3
- data/lib/ramaze/helper/tagz.rb +19 -0
- data/lib/ramaze/helper/user.rb +63 -0
- data/lib/ramaze/inform.rb +2 -24
- data/lib/ramaze/log.rb +28 -0
- data/lib/ramaze/{inform → log}/analogger.rb +3 -3
- data/lib/ramaze/{inform → log}/growl.rb +2 -2
- data/lib/ramaze/{inform → log}/hub.rb +4 -6
- data/lib/ramaze/{inform → log}/informer.rb +4 -4
- data/lib/ramaze/{inform → log}/knotify.rb +2 -2
- data/lib/ramaze/log/logger.rb +22 -0
- data/lib/ramaze/{inform/informing.rb → log/logging.rb} +16 -16
- data/lib/ramaze/{inform → log}/syslog.rb +0 -0
- data/lib/ramaze/{inform → log}/xosd.rb +2 -2
- data/lib/ramaze/route.rb +64 -36
- data/lib/ramaze/snippets/array/put_within.rb +13 -0
- data/lib/ramaze/snippets/binding/locals.rb +13 -0
- data/lib/ramaze/snippets/kernel/__dir__.rb +10 -8
- data/lib/ramaze/snippets/kernel/aquire.rb +1 -1
- data/lib/ramaze/snippets/kernel/constant.rb +1 -1
- data/lib/ramaze/snippets/object/pretty.rb +6 -0
- data/lib/ramaze/snippets/object/scope.rb +11 -0
- data/lib/ramaze/snippets/object/thread_accessor.rb +5 -0
- data/lib/ramaze/snippets/ordered_set.rb +1 -1
- data/lib/ramaze/snippets/proc/locals.rb +11 -0
- data/lib/ramaze/snippets/ramaze/thread_accessor.rb +44 -0
- data/lib/ramaze/snippets/string/esc.rb +29 -0
- data/lib/ramaze/snippets/string/start_with.rb +7 -0
- data/lib/ramaze/snippets/string/unindent.rb +6 -1
- data/lib/ramaze/snippets/struct/values_at.rb +1 -5
- data/lib/ramaze/sourcereload.rb +16 -14
- data/lib/ramaze/spec.rb +1 -0
- data/lib/ramaze/spec/helper.rb +11 -3
- data/lib/ramaze/spec/helper/browser.rb +25 -1
- data/lib/ramaze/spec/helper/pretty_output.rb +11 -11
- data/lib/ramaze/template.rb +5 -4
- data/lib/ramaze/template/amrita2.rb +2 -3
- data/lib/ramaze/template/ezamar/element.rb +2 -3
- data/lib/ramaze/template/ezamar/morpher.rb +1 -2
- data/lib/ramaze/template/ezamar/render_partial.rb +1 -1
- data/lib/ramaze/template/haml.rb +5 -2
- data/lib/ramaze/template/markaby.rb +2 -0
- data/lib/ramaze/template/tagz.rb +79 -0
- data/lib/ramaze/template/tenjin.rb +34 -0
- data/lib/ramaze/tool/create.rb +0 -3
- data/lib/ramaze/tool/localize.rb +107 -105
- data/lib/ramaze/tool/mime.rb +0 -2
- data/lib/ramaze/trinity.rb +1 -26
- data/lib/ramaze/version.rb +1 -1
- data/lib/vendor/bacon.rb +47 -41
- data/rake_tasks/conf.rake +18 -0
- data/rake_tasks/darcs.rake +5 -0
- data/rake_tasks/maintenance.rake +37 -24
- data/rake_tasks/spec.rake +1 -1
- data/spec/contrib/auto_params.rb +3 -1
- data/spec/contrib/profiling.rb +26 -0
- data/spec/examples/templates/template_redcloth.rb +1 -1
- data/spec/examples/templates/template_tenjin.rb +28 -0
- data/spec/helper.rb +0 -1
- data/spec/ramaze/action/layout.rb +28 -0
- data/spec/ramaze/controller/actionless_templates.rb +32 -0
- data/spec/ramaze/controller/template/other_wrapper.xhtml +1 -0
- data/spec/ramaze/controller/template_resolving.rb +37 -0
- data/spec/ramaze/{trinity → current}/request.rb +12 -2
- data/spec/ramaze/current/session.rb +97 -0
- data/spec/ramaze/dispatcher/directory.rb +2 -1
- data/spec/ramaze/dispatcher/file.rb +8 -3
- data/spec/ramaze/dispatcher/public/file name.txt +1 -0
- data/spec/ramaze/gestalt.rb +11 -0
- data/spec/ramaze/helper/aspect.rb +28 -22
- data/spec/ramaze/helper/cgi.rb +2 -2
- data/spec/ramaze/helper/flash.rb +33 -15
- data/spec/ramaze/helper/formatting.rb +2 -2
- data/spec/ramaze/helper/link.rb +46 -18
- data/spec/ramaze/helper/pager.rb +8 -5
- data/spec/ramaze/helper/partial.rb +8 -1
- data/spec/ramaze/helper/template/recursive_local_ivars.xhtml +7 -0
- data/spec/ramaze/helper/user.rb +46 -0
- data/spec/ramaze/{inform → log}/informer.rb +0 -0
- data/spec/ramaze/{inform → log}/syslog.rb +1 -1
- data/spec/ramaze/request.rb +14 -10
- data/spec/ramaze/route.rb +23 -0
- data/spec/ramaze/template.rb +48 -1
- data/spec/ramaze/template/haml.rb +6 -16
- data/spec/ramaze/template/haml/locals.haml +2 -1
- data/spec/ramaze/template/tagz.rb +62 -0
- data/spec/ramaze/template/tagz/external.tagz +8 -0
- data/spec/ramaze/template/tagz/sum.tagz +1 -0
- data/spec/ramaze/template/tenjin.rb +49 -0
- data/spec/ramaze/template/tenjin/external.tenjin +1 -0
- data/spec/snippets/string/unindent.rb +15 -0
- metadata +509 -475
- data/doc/README.html +0 -729
- data/doc/changes.txt +0 -5757
- data/doc/changes.xml +0 -5759
- data/examples/blog/src/view.rb +0 -16
- data/examples/blog/template/edit.xhtml +0 -19
- data/examples/blog/template/index.xhtml +0 -19
- data/examples/blog/template/new.xhtml +0 -18
- data/examples/wiktacular/mkd/newpagename/current.mkd +0 -1
- data/examples/wiktacular/mkd/newpagename/current.mkd.bak +0 -1
- data/lib/ramaze/contrib/email.rb-darcs-backup0 +0 -81
- data/lib/ramaze/template/bijou.rb +0 -39
- data/lib/ramaze/trinity/response.rb +0 -32
- data/spec/ramaze/template/bijou.rb +0 -25
- data/spec/ramaze/trinity/session.rb +0 -29
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'ramaze/current/request'
|
2
|
+
require 'ramaze/current/response'
|
3
|
+
require 'ramaze/current/session'
|
4
|
+
|
5
|
+
module Ramaze
|
6
|
+
module Current
|
7
|
+
class << self
|
8
|
+
include Trinity
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
setup(env)
|
12
|
+
before_call
|
13
|
+
|
14
|
+
if filter = Global.record
|
15
|
+
request = Current.request
|
16
|
+
Record << request if filter[request]
|
17
|
+
end
|
18
|
+
|
19
|
+
Dispatcher.handle
|
20
|
+
|
21
|
+
finish
|
22
|
+
ensure
|
23
|
+
after_call
|
24
|
+
end
|
25
|
+
|
26
|
+
def setup(env)
|
27
|
+
self.request = Request.new(env)
|
28
|
+
self.response = Response.new
|
29
|
+
self.session = Session.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def finish
|
33
|
+
session.finish if session
|
34
|
+
response.finish
|
35
|
+
end
|
36
|
+
|
37
|
+
def before(&block)
|
38
|
+
@before = block if block
|
39
|
+
@before
|
40
|
+
end
|
41
|
+
|
42
|
+
def before_call
|
43
|
+
if before
|
44
|
+
begin
|
45
|
+
before.call
|
46
|
+
rescue Object => e
|
47
|
+
Ramaze::Log.error e
|
48
|
+
raise
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def after(&block)
|
54
|
+
@after = block if block
|
55
|
+
@after
|
56
|
+
end
|
57
|
+
|
58
|
+
def after_call
|
59
|
+
if after
|
60
|
+
begin
|
61
|
+
after.call
|
62
|
+
rescue Object => e
|
63
|
+
Ramaze::Log.error e
|
64
|
+
raise
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -1,11 +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 'cgi'
|
5
|
-
require 'tmpdir'
|
6
|
-
require 'digest/md5'
|
7
|
-
require 'ipaddr'
|
8
|
-
require 'rack'
|
9
4
|
require 'rack/request'
|
10
5
|
|
11
6
|
module Ramaze
|
@@ -20,9 +15,7 @@ module Ramaze
|
|
20
15
|
#
|
21
16
|
# You can call this from everywhere with Ramaze::Request.current
|
22
17
|
|
23
|
-
def current
|
24
|
-
Thread.current[:request]
|
25
|
-
end
|
18
|
+
def current() Current.request end
|
26
19
|
end
|
27
20
|
|
28
21
|
# you can access the original @request via this method_missing,
|
@@ -48,7 +41,7 @@ module Ramaze
|
|
48
41
|
|
49
42
|
ipv4 = %w[ 127.0.0.1/32 192.168.0.0/16 172.16.0.0/12 10.0.0.0/8 169.254.0.0/16 ]
|
50
43
|
ipv6 = %w[ fc00::/7 fe80::/10 fec0::/10 ::1 ]
|
51
|
-
LOCAL = (ipv4 + ipv6).map{|a| IPAddr.new(a)}
|
44
|
+
LOCAL = (ipv4 + ipv6).map{|a| IPAddr.new(a)} unless defined?(LOCAL)
|
52
45
|
|
53
46
|
# --
|
54
47
|
# Mongrel somehow puts together multiple IPs when proxy is involved.
|
@@ -99,7 +92,7 @@ module Ramaze
|
|
99
92
|
# # In your Controller:
|
100
93
|
#
|
101
94
|
# def paste
|
102
|
-
# name, syntax = request
|
95
|
+
# name, syntax = request['paste'].values_at('name', 'syntax')
|
103
96
|
# paste = Paste.create_with(:name => name, :syntax => syntax)
|
104
97
|
# redirect '/'
|
105
98
|
# end
|
@@ -107,7 +100,7 @@ module Ramaze
|
|
107
100
|
# # Or, easier:
|
108
101
|
#
|
109
102
|
# def paste
|
110
|
-
# paste = Paste.create_with(request
|
103
|
+
# paste = Paste.create_with(request['paste'])
|
111
104
|
# redirect '/'
|
112
105
|
# end
|
113
106
|
|
@@ -118,10 +111,20 @@ module Ramaze
|
|
118
111
|
@ramaze_params = {}
|
119
112
|
|
120
113
|
@rack_params.each do |key, value|
|
121
|
-
|
122
|
-
|
123
|
-
@ramaze_params
|
124
|
-
|
114
|
+
if key =~ /^(.*?)(\[.*\])/
|
115
|
+
prim, nested = $~.captures
|
116
|
+
ref = @ramaze_params
|
117
|
+
|
118
|
+
keys = nested.scan(/\[([^\]]+)\]/).flatten
|
119
|
+
keys.unshift prim
|
120
|
+
|
121
|
+
keys.each_with_index do |k, i|
|
122
|
+
if i + 1 >= keys.size
|
123
|
+
ref[k] = value
|
124
|
+
else
|
125
|
+
ref = ref[k] ||= {}
|
126
|
+
end
|
127
|
+
end
|
125
128
|
else
|
126
129
|
@ramaze_params[key] = value
|
127
130
|
end
|
@@ -130,5 +133,49 @@ module Ramaze
|
|
130
133
|
@ramaze_params
|
131
134
|
end
|
132
135
|
end
|
136
|
+
|
137
|
+
# Interesting HTTP variables from env
|
138
|
+
|
139
|
+
def http_vars
|
140
|
+
env.reject{ |k,v|
|
141
|
+
k.to_s !~ /USER|HOST|REQUEST|REMOTE|FORWARD|REFER|PATH|QUERY|VERSION|KEEP|CACHE/
|
142
|
+
}
|
143
|
+
end
|
144
|
+
|
145
|
+
def to_s
|
146
|
+
p, c, e = params.inspect, cookies.inspect, http_vars.inspect
|
147
|
+
%{#<Ramaze::Request params=#{p} cookies=#{c} env=#{e}>}
|
148
|
+
end
|
149
|
+
alias inspect to_s
|
150
|
+
|
151
|
+
def pretty_print pp
|
152
|
+
p, c, e = params, cookies, http_vars
|
153
|
+
pp.object_group(self){
|
154
|
+
{ 'params' => params,
|
155
|
+
'cookies' => cookies,
|
156
|
+
'env' => http_vars }.each do |name, hash|
|
157
|
+
pp.breakable
|
158
|
+
pp.text " @#{name}="
|
159
|
+
pp.nest(name.length+3){ pp.pp_hash hash }
|
160
|
+
end
|
161
|
+
}
|
162
|
+
end
|
163
|
+
|
164
|
+
# Answers with a subset of request.params with only the key/value pairs for
|
165
|
+
# which you pass the keys.
|
166
|
+
# Valid keys are objects that respond to :to_s
|
167
|
+
#
|
168
|
+
# Example:
|
169
|
+
# request.params
|
170
|
+
# # => {'name' => 'jason', 'age' => '45', 'job' => 'lumberjack'}
|
171
|
+
# request.sub('name')
|
172
|
+
# # => {'name' => 'jason'}
|
173
|
+
# request.sub(:name, :job)
|
174
|
+
# # => {'name' => 'jason', 'job' => 'lumberjack'}
|
175
|
+
|
176
|
+
def subset(*keys)
|
177
|
+
keys = keys.map{|k| k.to_s }
|
178
|
+
params.reject{|k,v| not keys.include?(k) }
|
179
|
+
end
|
133
180
|
end
|
134
181
|
end
|
@@ -0,0 +1,19 @@
|
|
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
|
+
module Ramaze
|
5
|
+
class Response < Rack::Response
|
6
|
+
class << self
|
7
|
+
def current() Current.response end
|
8
|
+
end
|
9
|
+
|
10
|
+
def build(body = body, status = status, header = header)
|
11
|
+
header.each do |key, value|
|
12
|
+
self[key] = value
|
13
|
+
end
|
14
|
+
|
15
|
+
self.body, self.status = body, status
|
16
|
+
self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -7,39 +7,10 @@ else
|
|
7
7
|
require 'digest/sha2'
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
# The SessionHash acts as the wrapper for a simple Hash
|
13
|
-
#
|
14
|
-
# Its purpose is to notify the underlying cache, in which the sessions
|
15
|
-
# are stored, about updates.
|
16
|
-
|
17
|
-
class SessionHash
|
18
|
-
|
19
|
-
# Sets @hash to an empty Hash
|
20
|
-
|
21
|
-
def initialize
|
22
|
-
@hash = {}
|
23
|
-
end
|
24
|
-
|
25
|
-
# relays all the methods to the @hash and updates the session_cache in
|
26
|
-
# Session.current.sessions if anything changes.
|
10
|
+
require 'ramaze/current/session/flash'
|
11
|
+
require 'ramaze/current/session/hash'
|
27
12
|
|
28
|
-
|
29
|
-
old = @hash.dup
|
30
|
-
result = @hash.send(*args, &block)
|
31
|
-
unless old == @hash
|
32
|
-
Cache.sessions[Session.current.session_id] = self
|
33
|
-
end
|
34
|
-
result
|
35
|
-
end
|
36
|
-
|
37
|
-
# Calls #inspect on the wrapped @hash
|
38
|
-
|
39
|
-
def inspect
|
40
|
-
@hash.inspect
|
41
|
-
end
|
42
|
-
end
|
13
|
+
module Ramaze
|
43
14
|
|
44
15
|
# The purpose of Session is to hold key/value pairs like a Hash for a series
|
45
16
|
# of # request/response cycles from the same client.
|
@@ -49,7 +20,6 @@ module Ramaze
|
|
49
20
|
# deleted or expires.
|
50
21
|
|
51
22
|
class Session
|
52
|
-
|
53
23
|
# The unique id for the current session which is also passed on to the cookie.
|
54
24
|
|
55
25
|
attr_accessor :session_id
|
@@ -58,13 +28,17 @@ module Ramaze
|
|
58
28
|
|
59
29
|
attr_accessor :flash
|
60
30
|
|
31
|
+
# secret salt for client-side session data
|
32
|
+
|
33
|
+
trait :secret => 'change_me_please_123'
|
34
|
+
|
61
35
|
# the key used for the cookie
|
62
36
|
|
63
37
|
SESSION_KEY = '_ramaze_session_id' unless defined?(SESSION_KEY)
|
64
38
|
|
65
39
|
# Holds counter for IPs
|
66
40
|
|
67
|
-
IP_COUNT = Hash.new{|h,k| h[k] = OrderedSet.new} unless defined?(IP_COUNT)
|
41
|
+
IP_COUNT = ::Hash.new{|h,k| h[k] = OrderedSet.new} unless defined?(IP_COUNT)
|
68
42
|
|
69
43
|
# Limit the number of sessions one IP is allowed to hold.
|
70
44
|
|
@@ -75,6 +49,9 @@ module Ramaze
|
|
75
49
|
COOKIE = { :path => '/' }
|
76
50
|
|
77
51
|
class << self
|
52
|
+
def current
|
53
|
+
Current.session
|
54
|
+
end
|
78
55
|
|
79
56
|
# called from Ramaze::startup and adds Cache.sessions if cookies are
|
80
57
|
# enabled
|
@@ -82,13 +59,6 @@ module Ramaze
|
|
82
59
|
def startup(options = {})
|
83
60
|
Cache.add(:sessions) if Global.sessions
|
84
61
|
end
|
85
|
-
|
86
|
-
# answers with Thread.current[:session] which holds the current session
|
87
|
-
# set by the Dispatcher#setup_environment.
|
88
|
-
|
89
|
-
def current
|
90
|
-
Thread.current[:session]
|
91
|
-
end
|
92
62
|
end
|
93
63
|
|
94
64
|
# Initialize a new Session, requires the original Rack::Request instance
|
@@ -96,16 +66,17 @@ module Ramaze
|
|
96
66
|
#
|
97
67
|
# sets @session_id and @session_flash
|
98
68
|
|
99
|
-
def initialize request
|
69
|
+
def initialize(request = Current.request)
|
70
|
+
return unless Global.sessions
|
100
71
|
@session_id = (request.cookies[SESSION_KEY] || random_key)
|
101
72
|
|
102
73
|
unless IP_COUNT.nil?
|
103
|
-
ip = request.
|
74
|
+
ip = request.ip
|
104
75
|
IP_COUNT[ip] << @session_id
|
105
76
|
sessions.delete(IP_COUNT[ip].shift) if IP_COUNT[ip].size > IP_COUNT_LIMIT
|
106
77
|
end
|
107
78
|
|
108
|
-
@flash = Ramaze::
|
79
|
+
@flash = Ramaze::Session::Flash.new
|
109
80
|
end
|
110
81
|
|
111
82
|
# relay all messages we don't understand to the currently active session
|
@@ -118,7 +89,7 @@ module Ramaze
|
|
118
89
|
# existing already, the session itself is an instance of SessionHash
|
119
90
|
|
120
91
|
def current
|
121
|
-
sessions[session_id] ||=
|
92
|
+
sessions[session_id] ||= Session::Hash.new
|
122
93
|
end
|
123
94
|
|
124
95
|
# shortcut to Cache.sessions
|
@@ -138,7 +109,7 @@ module Ramaze
|
|
138
109
|
def random_key
|
139
110
|
h = [
|
140
111
|
Time.now.to_f.to_s, rand,
|
141
|
-
|
112
|
+
Current.request.hash, rand,
|
142
113
|
Process.pid, rand,
|
143
114
|
object_id, rand
|
144
115
|
].join
|
@@ -156,76 +127,27 @@ module Ramaze
|
|
156
127
|
#
|
157
128
|
# this is needed so flash can iterate over requests
|
158
129
|
# and always just keep the current and previous key/value pairs.
|
130
|
+
#
|
131
|
+
# finalizes the session and assigns the key to the response via
|
132
|
+
# set_cookie.
|
133
|
+
|
134
|
+
def finish
|
135
|
+
return unless Global.sessions
|
159
136
|
|
160
|
-
def finalize
|
161
137
|
old = current.delete(:FLASH)
|
162
138
|
current[:FLASH_PREVIOUS] = old if old
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
# The purpose of this class is to act as a unifier of the previous
|
167
|
-
# and current flash.
|
168
|
-
#
|
169
|
-
# Flash means pairs of keys and values that are held only over one
|
170
|
-
# request/response cycle. So you can assign a key/value in the current
|
171
|
-
# session and retrieve it in the current and following request.
|
172
|
-
#
|
173
|
-
# Please see the FlashHelper for details on the usage as you won't need
|
174
|
-
# to touch this class at all.
|
175
139
|
|
176
|
-
|
140
|
+
request, response = Current.request, Current.response
|
177
141
|
|
178
|
-
|
142
|
+
hash = {:value => session_id}.merge(COOKIE)
|
143
|
+
response.set_cookie(SESSION_KEY, hash)
|
179
144
|
|
180
|
-
|
181
|
-
session
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
def current
|
187
|
-
session[:FLASH] ||= {}
|
188
|
-
end
|
189
|
-
|
190
|
-
# combined key/value pairs of previous and current
|
191
|
-
# current keys overshadow the old ones.
|
192
|
-
|
193
|
-
def combined
|
194
|
-
previous.merge(current)
|
195
|
-
end
|
196
|
-
|
197
|
-
# flash[key] in your Controller
|
198
|
-
|
199
|
-
def [](key)
|
200
|
-
combined[key]
|
201
|
-
end
|
202
|
-
|
203
|
-
# flash[key] = value in your Controller
|
204
|
-
|
205
|
-
def []=(key, value)
|
206
|
-
prev = session[:FLASH] || {}
|
207
|
-
prev[key] = value
|
208
|
-
session[:FLASH] = prev
|
209
|
-
end
|
210
|
-
|
211
|
-
# Inspects the combined SessionFlash
|
212
|
-
|
213
|
-
def inspect
|
214
|
-
combined.inspect
|
215
|
-
end
|
216
|
-
|
217
|
-
# Delete a key
|
218
|
-
|
219
|
-
def delete(key)
|
220
|
-
session[:FLASH].delete(key)
|
221
|
-
end
|
222
|
-
|
223
|
-
private
|
224
|
-
|
225
|
-
# Session.current or {}
|
226
|
-
|
227
|
-
def session
|
228
|
-
Session.current || {}
|
145
|
+
# set client side session cookie
|
146
|
+
if val = request['session.client'] and
|
147
|
+
(!val.empty? or request.cookies["#{SESSION_KEY}-client"])
|
148
|
+
cookie = hash.merge(:value => marshal(val))
|
149
|
+
response.set_cookie("#{SESSION_KEY}-client", cookie)
|
150
|
+
end
|
229
151
|
end
|
230
152
|
end
|
231
153
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Ramaze
|
2
|
+
class Session
|
3
|
+
# The purpose of this class is to act as a unifier of the previous
|
4
|
+
# and current flash.
|
5
|
+
#
|
6
|
+
# Flash means pairs of keys and values that are held only over one
|
7
|
+
# request/response cycle. So you can assign a key/value in the current
|
8
|
+
# session and retrieve it in the current and following request.
|
9
|
+
#
|
10
|
+
# Please see the FlashHelper for details on the usage as you won't need
|
11
|
+
# to touch this class at all.
|
12
|
+
class Flash
|
13
|
+
include Enumerable
|
14
|
+
def each(&block)
|
15
|
+
combined.each(&block)
|
16
|
+
end
|
17
|
+
|
18
|
+
# the current session[:FLASH_PREVIOUS]
|
19
|
+
def previous
|
20
|
+
session[:FLASH_PREVIOUS] || {}
|
21
|
+
end
|
22
|
+
|
23
|
+
# the current session[:FLASH]
|
24
|
+
def current
|
25
|
+
session[:FLASH] ||= {}
|
26
|
+
end
|
27
|
+
|
28
|
+
# combined key/value pairs of previous and current
|
29
|
+
# current keys overshadow the old ones.
|
30
|
+
def combined
|
31
|
+
previous.merge(current)
|
32
|
+
end
|
33
|
+
|
34
|
+
# flash[key] in your Controller
|
35
|
+
def [](key)
|
36
|
+
combined[key]
|
37
|
+
end
|
38
|
+
|
39
|
+
# flash[key] = value in your Controller
|
40
|
+
def []=(key, value)
|
41
|
+
prev = session[:FLASH] || {}
|
42
|
+
prev[key] = value
|
43
|
+
session[:FLASH] = prev
|
44
|
+
end
|
45
|
+
|
46
|
+
# Inspects the combined SessionFlash
|
47
|
+
|
48
|
+
def inspect
|
49
|
+
combined.inspect
|
50
|
+
end
|
51
|
+
|
52
|
+
# Delete a key
|
53
|
+
|
54
|
+
def delete(key)
|
55
|
+
current.delete(key)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Session.current or {}
|
61
|
+
|
62
|
+
def session
|
63
|
+
Current.session || {}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|