ramaze 2011.07.25 → 2011.10.23

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 (181) hide show
  1. data/.gitignore +3 -0
  2. data/.mailmap +3 -2
  3. data/.travis.yml +17 -0
  4. data/.yardopts +13 -0
  5. data/README.md +95 -352
  6. data/examples/app/blog/app.rb +25 -64
  7. data/examples/app/blog/config.ru +11 -9
  8. data/examples/app/blog/controller/init.rb +29 -86
  9. data/examples/app/blog/controller/posts.rb +232 -0
  10. data/examples/app/blog/controller/users.rb +160 -0
  11. data/examples/app/blog/layout/default.xhtml +61 -0
  12. data/examples/app/blog/migrations/01_create_schema.rb +50 -0
  13. data/examples/app/blog/model/comment.rb +41 -54
  14. data/examples/app/blog/model/init.rb +41 -13
  15. data/examples/app/blog/model/post.rb +35 -0
  16. data/examples/app/blog/model/user.rb +105 -0
  17. data/examples/app/blog/public/.htaccess +24 -0
  18. data/examples/app/blog/public/css/grid.css +107 -0
  19. data/examples/app/blog/public/css/layout.css +203 -0
  20. data/examples/app/blog/public/css/reset.css +123 -0
  21. data/examples/app/blog/public/css/text.css +109 -0
  22. data/examples/app/blog/public/dispatch.fcgi +11 -0
  23. data/examples/app/blog/public/favicon.ico +0 -0
  24. data/examples/app/blog/public/images/bg.png +0 -0
  25. data/examples/app/blog/start.rb +18 -3
  26. data/examples/app/blog/view/feed.xhtml +23 -0
  27. data/examples/app/blog/view/form.xhtml +11 -0
  28. data/examples/app/blog/view/index.xhtml +44 -0
  29. data/examples/app/blog/view/users/form.xhtml +12 -0
  30. data/examples/app/blog/view/users/index.xhtml +30 -0
  31. data/examples/app/blog/view/users/login.xhtml +8 -0
  32. data/examples/app/blog/view/view.xhtml +68 -0
  33. data/{doc → guide}/AUTHORS +5 -3
  34. data/{doc → guide}/CHANGELOG +428 -0
  35. data/{doc/GPL → guide/GPL_LICENSE} +0 -0
  36. data/{doc/COPYING → guide/RUBY_LICENSE} +3 -6
  37. data/guide/_static/logo.png +0 -0
  38. data/guide/_static/logo.svg +49 -0
  39. data/guide/_static/ramaze_console.png +0 -0
  40. data/guide/css/common.css +20 -0
  41. data/guide/general/cache.md +167 -0
  42. data/guide/general/configuration.md +168 -0
  43. data/guide/general/contributing.md +108 -0
  44. data/guide/general/controllers.md +115 -0
  45. data/guide/general/helpers.md +76 -0
  46. data/guide/general/installation.md +58 -0
  47. data/guide/general/logging.md +99 -0
  48. data/guide/general/middlewares.md +100 -0
  49. data/guide/general/models.md +78 -0
  50. data/guide/general/principles.md +53 -0
  51. data/guide/general/ramaze_command.md +155 -0
  52. data/guide/general/routes.md +81 -0
  53. data/guide/general/sessions.md +140 -0
  54. data/guide/general/special_thanks.md +67 -0
  55. data/guide/general/testing.md +61 -0
  56. data/guide/general/views.md +322 -0
  57. data/guide/tutorials/introduction.md +259 -0
  58. data/lib/proto/config.ru +1 -1
  59. data/lib/proto/public/favicon.ico +0 -0
  60. data/lib/proto/view/index.xhtml +7 -7
  61. data/lib/ramaze.rb +4 -4
  62. data/lib/ramaze/app.rb +11 -11
  63. data/lib/ramaze/app_graph.rb +2 -4
  64. data/lib/ramaze/bin/console.rb +3 -3
  65. data/lib/ramaze/bin/create.rb +2 -2
  66. data/lib/ramaze/bin/restart.rb +4 -4
  67. data/lib/ramaze/bin/runner.rb +5 -5
  68. data/lib/ramaze/bin/start.rb +19 -4
  69. data/lib/ramaze/bin/status.rb +3 -3
  70. data/lib/ramaze/bin/stop.rb +3 -3
  71. data/lib/ramaze/cache.rb +1 -0
  72. data/lib/ramaze/cache/lru.rb +8 -4
  73. data/lib/ramaze/cache/memcache.rb +32 -13
  74. data/lib/ramaze/cache/redis.rb +164 -0
  75. data/lib/ramaze/cache/sequel.rb +43 -28
  76. data/lib/ramaze/controller.rb +1 -2
  77. data/lib/ramaze/dependencies.rb +40 -3
  78. data/lib/ramaze/helper/bench.rb +26 -16
  79. data/lib/ramaze/helper/blue_form.rb +46 -73
  80. data/lib/ramaze/helper/cache.rb +10 -6
  81. data/lib/ramaze/helper/csrf.rb +35 -39
  82. data/lib/ramaze/helper/disqus.rb +5 -4
  83. data/lib/ramaze/helper/email.rb +35 -24
  84. data/lib/ramaze/helper/erector.rb +9 -13
  85. data/lib/ramaze/helper/flash.rb +7 -9
  86. data/lib/ramaze/helper/formatting.rb +194 -179
  87. data/lib/ramaze/helper/gravatar.rb +4 -8
  88. data/lib/ramaze/helper/identity.rb +3 -3
  89. data/lib/ramaze/helper/layout.rb +23 -8
  90. data/lib/ramaze/helper/markaby.rb +1 -1
  91. data/lib/ramaze/helper/paginate.rb +46 -39
  92. data/lib/ramaze/helper/request_accessor.rb +3 -1
  93. data/lib/ramaze/helper/simple_captcha.rb +18 -17
  94. data/lib/ramaze/helper/stack.rb +1 -1
  95. data/lib/ramaze/helper/tagz.rb +4 -2
  96. data/lib/ramaze/helper/upload.rb +523 -0
  97. data/lib/ramaze/helper/user.rb +4 -8
  98. data/lib/ramaze/helper/xhtml.rb +11 -15
  99. data/lib/ramaze/log.rb +9 -6
  100. data/lib/ramaze/log/rotatinginformer.rb +62 -27
  101. data/lib/ramaze/log/syslog.rb +20 -15
  102. data/lib/ramaze/log/xosd.rb +2 -1
  103. data/lib/ramaze/reloader.rb +2 -0
  104. data/lib/ramaze/request.rb +11 -10
  105. data/lib/ramaze/setup.rb +23 -6
  106. data/lib/ramaze/snippets/array/put_within.rb +3 -9
  107. data/lib/ramaze/snippets/binding/locals.rb +5 -10
  108. data/lib/ramaze/snippets/fiber.rb +1 -23
  109. data/lib/ramaze/snippets/kernel/pretty_inspect.rb +3 -6
  110. data/lib/ramaze/snippets/numeric/filesize_format.rb +3 -5
  111. data/lib/ramaze/snippets/numeric/time.rb +3 -7
  112. data/lib/ramaze/snippets/object/__dir__.rb +3 -7
  113. data/lib/ramaze/snippets/object/instance_variable_defined.rb +3 -6
  114. data/lib/ramaze/snippets/object/pretty.rb +3 -7
  115. data/lib/ramaze/snippets/object/scope.rb +7 -9
  116. data/lib/ramaze/snippets/proc/locals.rb +12 -12
  117. data/lib/ramaze/snippets/ramaze/acquire.rb +15 -14
  118. data/lib/ramaze/snippets/ramaze/deprecated.rb +1 -1
  119. data/lib/ramaze/snippets/ramaze/fiber.rb +1 -1
  120. data/lib/ramaze/snippets/ramaze/lru_hash.rb +2 -3
  121. data/lib/ramaze/snippets/ramaze/struct.rb +2 -4
  122. data/lib/ramaze/snippets/string/camel_case.rb +8 -10
  123. data/lib/ramaze/snippets/string/color.rb +3 -4
  124. data/lib/ramaze/snippets/string/end_with.rb +3 -6
  125. data/lib/ramaze/snippets/string/esc.rb +3 -8
  126. data/lib/ramaze/snippets/string/ord.rb +3 -8
  127. data/lib/ramaze/snippets/string/snake_case.rb +6 -9
  128. data/lib/ramaze/snippets/string/start_with.rb +3 -8
  129. data/lib/ramaze/snippets/string/unindent.rb +3 -6
  130. data/lib/ramaze/snippets/thread/into.rb +1 -3
  131. data/lib/ramaze/spec.rb +2 -31
  132. data/lib/ramaze/spec/bacon.rb +18 -2
  133. data/lib/ramaze/version.rb +1 -1
  134. data/lib/ramaze/view.rb +1 -1
  135. data/ramaze.gemspec +1 -1
  136. data/spec/helper.rb +2 -1
  137. data/spec/ramaze/bin/start.rb +16 -20
  138. data/spec/ramaze/cache/localmemcache.rb +4 -7
  139. data/spec/ramaze/cache/memcache.rb +3 -1
  140. data/spec/ramaze/cache/redis.rb +62 -0
  141. data/spec/ramaze/helper/blue_form.rb +33 -4
  142. data/spec/ramaze/helper/layout.rb +40 -7
  143. data/spec/ramaze/helper/upload.rb +149 -0
  144. data/spec/ramaze/helper/uploads/text_1.txt +1 -0
  145. data/spec/ramaze/helper/uploads/text_2.txt +1 -0
  146. data/spec/ramaze/log/growl.rb +4 -6
  147. data/spec/ramaze/log/syslog.rb +6 -0
  148. data/spec/ramaze/view/lokar.rb +5 -0
  149. data/spec/ramaze/view/nagoro.rb +5 -0
  150. data/tasks/authors.rake +1 -1
  151. data/tasks/bacon.rake +14 -5
  152. data/tasks/changelog.rake +1 -1
  153. data/tasks/yard.rake +12 -4
  154. metadata +277 -239
  155. data/doc/LEGAL +0 -26
  156. data/examples/app/blog/README +0 -3
  157. data/examples/app/blog/controller/comment.rb +0 -45
  158. data/examples/app/blog/controller/entry.rb +0 -85
  159. data/examples/app/blog/controller/main.rb +0 -20
  160. data/examples/app/blog/controller/tag.rb +0 -9
  161. data/examples/app/blog/layout/default.nag +0 -31
  162. data/examples/app/blog/model/entry.rb +0 -89
  163. data/examples/app/blog/model/tag.rb +0 -36
  164. data/examples/app/blog/public/css/screen.css +0 -273
  165. data/examples/app/blog/spec/blog.rb +0 -87
  166. data/examples/app/blog/view/comment/form.nag +0 -10
  167. data/examples/app/blog/view/comment/show.nag +0 -16
  168. data/examples/app/blog/view/entry/edit.nag +0 -14
  169. data/examples/app/blog/view/entry/feed.atom.nag +0 -8
  170. data/examples/app/blog/view/entry/feed.rss.nag +0 -7
  171. data/examples/app/blog/view/entry/index.nag +0 -7
  172. data/examples/app/blog/view/entry/new.nag +0 -13
  173. data/examples/app/blog/view/entry/show.nag +0 -36
  174. data/examples/app/blog/view/feed.atom.nag +0 -18
  175. data/examples/app/blog/view/feed.rss.nag +0 -25
  176. data/examples/app/blog/view/index.nag +0 -6
  177. data/examples/app/blog/view/tag/index.nag +0 -5
  178. data/lib/proto/public/ramaze.png +0 -0
  179. data/lib/ramaze/rest.rb +0 -36
  180. data/spec/ramaze/rest.rb +0 -28
  181. data/tasks/rcov.rake +0 -22
@@ -7,11 +7,11 @@ module Ramaze
7
7
  # directory this command will look for a Rackup file in that directory,
8
8
  # otherwise it assumes the specified file is a Rackup file.
9
9
  #
10
- # == Usage
10
+ # Usage:
11
11
  #
12
- # ramaze start
13
- # ramaze start /home/foobar/projects/blog/config.ru
14
- # ramaze start /home/foobar/projects/blog
12
+ # ramaze start
13
+ # ramaze start /home/foobar/projects/blog/config.ru
14
+ # ramaze start /home/foobar/projects/blog
15
15
  #
16
16
  # @author Yorick Peterse
17
17
  # @since 21-07-2011
@@ -199,6 +199,21 @@ Example:
199
199
  params.push("#{opt}#{value}")
200
200
  end
201
201
 
202
+ start_server(rackup_path, rackup_config, *params)
203
+ end
204
+
205
+ ##
206
+ # Starts a server baed on the rackup path, rackup configuration file and
207
+ # additional parameters.
208
+ #
209
+ # @author Yorick Peterse
210
+ # @since 21-10-2011
211
+ # @param [String] rackup_path The path to the Rackup executable.
212
+ # @param [String] rackup_config The path to the config.ru file to use.
213
+ # @param [Array] *params Additional parameters to pass to the ``exec()``
214
+ # method.
215
+ #
216
+ def start_server(rackup_path, rackup_config, *params)
202
217
  exec('ruby', rackup_path, rackup_config, *params)
203
218
  end
204
219
  end # Start
@@ -5,10 +5,10 @@ module Ramaze
5
5
  # The status command can be used to show the details, such as the PID and
6
6
  # the CPU usage, of a running Ramaze application.
7
7
  #
8
- # == Usage
8
+ # Usage:
9
9
  #
10
- # ramaze status
11
- # ramaze status /home/projects/ramaze/ramaze.pid
10
+ # ramaze status
11
+ # ramaze status /home/projects/ramaze/ramaze.pid
12
12
  #
13
13
  # @author Yorick Peterse
14
14
  # @author TJ Vanderpoel
@@ -8,10 +8,10 @@ module Ramaze
8
8
  # case this command tries to find a file of which the name matches the name
9
9
  # of the current directory.
10
10
  #
11
- # == Usage
11
+ # Usage:
12
12
  #
13
- # ramaze stop
14
- # ramaze stop /home/foobar/projects/ramaze/ramaze.pid
13
+ # ramaze stop
14
+ # ramaze stop /home/foobar/projects/ramaze/ramaze.pid
15
15
  #
16
16
  # @author Yorick Peterse
17
17
  # @author TJ Vanderpoel
@@ -11,6 +11,7 @@ module Ramaze
11
11
  autoload :LocalMemCache, 'ramaze/cache/localmemcache'
12
12
  autoload :MemCache, 'ramaze/cache/memcache'
13
13
  autoload :Sequel, 'ramaze/cache/sequel'
14
+ autoload :Redis, 'ramaze/cache/redis'
14
15
 
15
16
  ##
16
17
  # Clears the cache after a file has been reloaded.
@@ -4,9 +4,13 @@
4
4
  module Ramaze
5
5
  class Cache
6
6
  ##
7
- # Cache class that uses Ramaze::LRUHash as a storage engine. This cache has
8
- # the advantage that unlike Innate::Cache::Memory it does not leak memory
9
- # over time.
7
+ # Cache class that uses {Ramaze::LRUHash} as a storage engine. This cache
8
+ # has the advantage that unlike Innate::Cache::Memory it does not leak
9
+ # memory over time when using the cache for sessions.
10
+ #
11
+ # @example
12
+ # Ramaze::Cache.options.session = Ramaze::Cache::LRU
13
+ # Ramaze.setup_dependencies
10
14
  #
11
15
  # @author Michael Fellinger
12
16
  # @since 17-07-2009
@@ -28,7 +32,7 @@ module Ramaze
28
32
 
29
33
  ##
30
34
  # Prepares the cache by creating a new instance of Ramaze::LRUHash using
31
- # the options set in Ramaze::Cache::LRU::OPTIONS.
35
+ # the options set in {Ramaze::Cache::LRU::OPTIONS}.
32
36
  #
33
37
  # @author Michael Fellinger
34
38
  # @since 17-07-2009
@@ -1,4 +1,6 @@
1
- require 'dalli'
1
+ Ramaze.setup(:verbose => false) do
2
+ gem 'dalli'
3
+ end
2
4
 
3
5
  # Kgio gives a nice performance boost but it isn't required
4
6
  begin; require 'kgio'; rescue LoadError => e; end
@@ -11,7 +13,7 @@ module Ramaze
11
13
  # Cache driver for the Memcache storage engine. Memcache is a key/value
12
14
  # store that's extremely useful for caching data such as views or API
13
15
  # responses. More inforamtion about Memcache can be found on it's website:
14
- # http://memcached.org/.
16
+ # <http://memcached.org/>.
15
17
  #
16
18
  # Note that this cache driver requires the Dalli gem rather than Memcache
17
19
  # Client. The reason for this is that the Memcache client hasn't been
@@ -23,10 +25,27 @@ module Ramaze
23
25
  # This driver works similar to Ramaze::Cache::Sequel in that it allows you
24
26
  # to specify instance specific options uisng the using() method:
25
27
  #
26
- # Ramaze::Cache.options.view = Ramaze::Cache::Memcache.using(:compression => false)
28
+ # Ramaze::Cache.options.session = Ramaze::Cache::MemCache.using(
29
+ # :compression => false
30
+ # )
27
31
  #
28
32
  # All options sent to the using() method will be sent to Dalli.
29
33
  #
34
+ # @example Using the default options
35
+ # Ramaze::Cache.options.view = Ramaze::Cache::MemCache
36
+ # Ramaze.setup_dependencies
37
+ #
38
+ # Ramaze::Cache.view.store(:my_view, 'Hello Ramaze')
39
+ #
40
+ # @example Using custom options
41
+ # Ramaze::Cache.options.view = Ramaze::Cache::MemCache.using(
42
+ # :compression => false,
43
+ # :servers => ['localhost:11211', 'localhost:112112']
44
+ # )
45
+ #
46
+ # Ramaze.setup_dependencies
47
+ # Ramaze::Cache.view.store(:my_view, 'Hello Ramaze')
48
+ #
30
49
  # @author Yorick Peterse
31
50
  # @since 04-05-2011
32
51
  #
@@ -49,7 +68,7 @@ module Ramaze
49
68
  :servers => ['localhost:11211']
50
69
  }
51
70
 
52
- # Hash containing all the default options merged with the user specified
71
+ # Hash containing all the default options merged with the user specified
53
72
  # ones
54
73
  attr_accessor :options
55
74
 
@@ -72,7 +91,7 @@ module Ramaze
72
91
  # :compression => false,
73
92
  # :username => 'ramaze',
74
93
  # :password => 'ramaze123',
75
- # :servers => ['othermachine.com:12345'] # Overwrites the default server
94
+ # :servers => ['othermachine.com:12345']
76
95
  # )
77
96
  #
78
97
  # @author Yorick Peterse
@@ -86,31 +105,31 @@ module Ramaze
86
105
  merged = Ramaze::Cache::MemCache.trait[:default].merge(options)
87
106
  Class.new(self) { @options = merged }
88
107
  end
89
- end
108
+ end # class << self
90
109
 
91
110
  ##
92
111
  # Creates a new instance of the cache class.
93
112
  #
94
113
  # @author Michael Fellinger
95
114
  # @since 04-05-2011
96
- # @param [Hash] options A hash with custom options, see
115
+ # @param [Hash] options A hash with custom options, see
97
116
  # Ramaze::Cache::MemCache.using for all available options.
98
117
  #
99
118
  def initialize(options = {})
100
119
  self.class.options ||= Ramaze::Cache::MemCache.trait[:default].merge(
101
120
  options
102
121
  )
103
-
122
+
104
123
  @options = options.merge(self.class.options)
105
124
  end
106
125
 
107
126
  ##
108
- # Prepares the cache by creating the namespace and an instance of a Dalli
127
+ # Prepares the cache by creating the namespace and an instance of a Dalli
109
128
  # client.
110
129
  #
111
130
  # @author Yorick Peterse
112
131
  # @since 04-05-2011
113
- # @param [String] hostname The hostname of the machine running the
132
+ # @param [String] hostname The hostname of the machine running the
114
133
  # application.
115
134
  # @param [String] username The name of the user executing the process
116
135
  # @param [String] appname Unique identifier for the application.
@@ -125,7 +144,7 @@ module Ramaze
125
144
  options[:namespace] = [
126
145
  hostname, username, appname, cachename
127
146
  ].compact.join('-')
128
-
147
+
129
148
  @client = ::Dalli::Client.new(options[:servers], options)
130
149
  end
131
150
 
@@ -153,7 +172,7 @@ module Ramaze
153
172
  end
154
173
 
155
174
  ##
156
- # Fetches the specified key from the cache. It the value was nil the
175
+ # Fetches the specified key from the cache. It the value was nil the
157
176
  # default value will be returned instead.
158
177
  #
159
178
  # @author Yorick Peterse
@@ -182,7 +201,7 @@ module Ramaze
182
201
  # @param [String] key The name of the key to store.
183
202
  # @param [Mixed] value The value to store in Memcache.
184
203
  # @param [Fixnum] ttl The Time To Live to use for the current key.
185
- # @param [Hash] options A hash containing options specific for the
204
+ # @param [Hash] options A hash containing options specific for the
186
205
  # specified key.
187
206
  # @return [Mixed]
188
207
  #
@@ -0,0 +1,164 @@
1
+ Ramaze.setup(:verbose => false) do
2
+ gem 'redis'
3
+ end
4
+
5
+ module Ramaze
6
+ class Cache
7
+ ##
8
+ # The Redis cache is a cache driver for Redis (http://redis.io/). Redis is a
9
+ # key/value store similar to Memcached but with the ability to flush data to
10
+ # a file among various other features.
11
+ #
12
+ # The usage of this cache is very similar to the Memcache driver. You load
13
+ # it by simply specifying the class:
14
+ #
15
+ # Ramaze::Cache.options.session = Ramaze::Cache::Redis
16
+ #
17
+ # If you want to specify custom options you can do so by calling {.using} on
18
+ # the class:
19
+ #
20
+ # Ramaze::Cache.options.session = Ramaze::Cache::Redis.using(...)
21
+ #
22
+ # @example Using a custom Redis host
23
+ # Ramaze::Cache.options.names.push(:redis)
24
+ # Ramaze::Cache.options.redis = Ramaze::Cache::Redis.using(
25
+ # :host => '123.124.125.126',
26
+ # :port => 6478
27
+ # )
28
+ #
29
+ # @author Michael Fellinger
30
+ # @since 09-10-2011
31
+ #
32
+ class Redis
33
+ include Cache::API
34
+ include Innate::Traited
35
+
36
+ # Hash containing all the default options to use when no custom ones are
37
+ # specified in .using().
38
+ trait :default => {
39
+ :expires_in => 604800,
40
+ :host => 'localhost',
41
+ :port => 6379
42
+ }
43
+
44
+ # Hash containing all the default options merged with the user specified
45
+ # ones.
46
+ attr_accessor :options
47
+
48
+ class << self
49
+ attr_accessor :options
50
+
51
+ ##
52
+ # Creates a new instance of the cache class and merges the default
53
+ # options with the custom ones.
54
+ #
55
+ # Using this method you can specify custom options for various caches.
56
+ # For example, the Redis cache for your sessions could be located at
57
+ # server #1 while a custom cache is located on server #2.
58
+ #
59
+ # @author Yorick Peterse
60
+ # @since 09-10-2011
61
+ # @param [Hash] options A hash containing custom options.
62
+ # @option options [Fixnum] :expires_in The default time after which a
63
+ # key should expire.
64
+ # @option options [String] :host The hostname of the machine on which
65
+ # Redis is running.
66
+ # @option options [Fixnum] :port The port number to connect to.
67
+ #
68
+ def using(options = {})
69
+ merged = Ramaze::Cache::Redis.trait[:default].merge(options)
70
+ Class.new(self) { @options = merged }
71
+ end
72
+ end # class << self
73
+
74
+ ##
75
+ # Creates a new instance of the cache and merges the options if they
76
+ # haven't already been set.
77
+ #
78
+ # @author Michael Fellinger
79
+ # @param [Hash] options A hash with custom options. See
80
+ # Ramaze::Cache::Redis.using() and the trait :default for more
81
+ # information.
82
+ #
83
+ def initialize(options = {})
84
+ self.class.options ||= Ramaze::Cache::Redis.trait[:default].merge(
85
+ options
86
+ )
87
+
88
+ @options = options.merge(self.class.options)
89
+ end
90
+
91
+ ##
92
+ # Prepares the cache by setting up the namespace and loading Redis.
93
+ #
94
+ # @author Michael Fellinger
95
+ # @since 09-10-2011
96
+ # @param [String] hostname The host of the machine that's running the
97
+ # Ramaze application.
98
+ # @param [String] username The name of the user that's running the
99
+ # application.
100
+ # @param [String] appname The name of the application (:pristine by
101
+ # default).
102
+ # @param [String] cachename The namespace to use for this cache instance.
103
+ #
104
+ def cache_setup(hostname, username, appname, cachename)
105
+ options[:namespace] = [
106
+ hostname, username, appname, cachename
107
+ ].compact.join('-')
108
+
109
+ @client = ::Redis.new(options)
110
+ end
111
+
112
+ ##
113
+ # Clears the entire cache.
114
+ #
115
+ # @author Michael Fellinger
116
+ # @since 09-10-2011
117
+ #
118
+ def cache_clear
119
+ @client.flushall
120
+ end
121
+
122
+ ##
123
+ # Removes a number of keys from the cache.
124
+ #
125
+ # @author Michael Fellinger
126
+ # @param [Array] *keys An array of key names to remove.
127
+ #
128
+ def cache_delete(*keys)
129
+ @client.del(*keys)
130
+ end
131
+
132
+ ##
133
+ # Retrieves the value of the given key. If no value could be retrieved the
134
+ # default value (set to nil by default) will be returned instead.
135
+ #
136
+ # @author Michael Fellinger
137
+ # @param [String] key The name of the key to retrieve.
138
+ # @param [Mixed] default The default value.
139
+ # @return [Mixed]
140
+ #
141
+ def cache_fetch(key, default = nil)
142
+ value = @client.get(key)
143
+ value.nil? ? default : value
144
+ end
145
+
146
+ ##
147
+ # Stores a new value under the given key.
148
+ #
149
+ # @author Michael Fellinger
150
+ # @param [String] key The name of the key to store.
151
+ # @param [Mixed] value The value of the key.
152
+ # @param [Fixnum] ttl The Time To Live of the key.
153
+ # @param [Hash] options A hash containing key specific options.
154
+ # @option options :expires_in The time after which the key should expire.
155
+ #
156
+ def cache_store(key, value, ttl = nil, options = {})
157
+ ttl = options[:ttl] || @options[:expires_in]
158
+ @client.setex(key, ttl, value)
159
+
160
+ return value
161
+ end
162
+ end # Redis
163
+ end # Cache
164
+ end # Ramaze
@@ -1,4 +1,6 @@
1
- require 'sequel'
1
+ Ramaze.setup(:verbose => false) do
2
+ gem 'sequel'
3
+ end
2
4
 
3
5
  module Ramaze
4
6
  class Cache
@@ -8,15 +10,15 @@ module Ramaze
8
10
  # databases are MySQL, SQLite3 and so on. In order to use this cache you'd
9
11
  # have to do the following:
10
12
  #
11
- # Ramaze::Cache.options.view = Ramaze::Cache::Sequel.using(
12
- # :connection => Sequel.mysql(
13
- # :host => 'localhost',
14
- # :user => 'user',
15
- # :password => 'password',
16
- # :database => 'blog'
17
- # ),
18
- # :table => :blog_sessions
19
- # )
13
+ # Ramaze::Cache.options.view = Ramaze::Cache::Sequel.using(
14
+ # :connection => Sequel.mysql(
15
+ # :host => 'localhost',
16
+ # :user => 'user',
17
+ # :password => 'password',
18
+ # :database => 'blog'
19
+ # ),
20
+ # :table => :blog_sessions
21
+ # )
20
22
  #
21
23
  # If you already have an existing connection you can just pass the object to
22
24
  # the :connection option instead of creating a new connection manually.
@@ -24,6 +26,18 @@ module Ramaze
24
26
  # Massive thanks to Lars Olsson for patching the original Sequel cache so
25
27
  # that it supports multiple connections and other useful features.
26
28
  #
29
+ # @example Setting a custom database connection
30
+ # Ramaze::Cache.options.names.push(:sequel)
31
+ # Ramaze::Cache.options.sequel = Ramaze::Cache::Sequel.using(
32
+ # :connection => Sequel.connect(
33
+ # :adapter => 'mysql2',
34
+ # :host => 'localhost',
35
+ # :username => 'cache',
36
+ # :password => 'cache123',
37
+ # :database => 'ramaze_cache'
38
+ # )
39
+ # )
40
+ #
27
41
  # @author Lars Olsson
28
42
  # @since 18-04-2011
29
43
  #
@@ -46,7 +60,7 @@ module Ramaze
46
60
  :ttl => nil
47
61
  }
48
62
 
49
- # Hash containing all the default options merged with the user specified
63
+ # Hash containing all the default options merged with the user specified
50
64
  # ones
51
65
  attr_accessor :options
52
66
 
@@ -71,9 +85,9 @@ module Ramaze
71
85
  # #
72
86
  # Ramaze.options.cache.session = Ramaze::Cache::Sequel.using(
73
87
  # :connection => Sequel.mysql(
74
- # :host =>'localhost',
88
+ # :host =>'localhost',
75
89
  # :user =>'user',
76
- # :password =>'password',
90
+ # :password =>'password',
77
91
  # :database =>'blog'
78
92
  # ),
79
93
  # :table => :blog_sessions
@@ -117,21 +131,21 @@ module Ramaze
117
131
  #
118
132
  # @author Michael Fellinger
119
133
  # @since 04-05-2011
120
- # @param [Hash] options A hash with custom options, see
134
+ # @param [Hash] options A hash with custom options, see
121
135
  # Ramaze::Cache::Sequel.using for all available options.
122
136
  #
123
137
  def initialize(options = {})
124
138
  self.class.options ||= Ramaze::Cache::Sequel.trait[:default].merge(
125
139
  options
126
140
  )
127
-
141
+
128
142
  @options = options.merge(self.class.options)
129
143
  end
130
144
 
131
145
  ##
132
146
  # Executed after #initialize and before any other method.
133
147
  #
134
- # Some parameters identifying the current process will be passed so caches
148
+ # Some parameters identifying the current process will be passed so caches
135
149
  # that act in one global name-space can use them as a prefix.
136
150
  #
137
151
  # @author Lars Olsson
@@ -159,7 +173,7 @@ module Ramaze
159
173
  end
160
174
 
161
175
  ##
162
- # Remove all key/value pairs from the cache. Should behave as if #delete
176
+ # Remove all key/value pairs from the cache. Should behave as if #delete
163
177
  # had been called with all +keys+ as argument.
164
178
  #
165
179
  # @author Lars Olsson
@@ -173,7 +187,7 @@ module Ramaze
173
187
  # Remove the corresponding key/value pair for each key passed. If removing
174
188
  # is not an option it should set the corresponding value to nil.
175
189
  #
176
- # If only one key was deleted, answer with the corresponding value. If
190
+ # If only one key was deleted, answer with the corresponding value. If
177
191
  # multiple keys were deleted, answer with an Array containing the values.
178
192
  #
179
193
  # @author Lars Olsson
@@ -207,12 +221,12 @@ module Ramaze
207
221
 
208
222
  @dataset.filter(:key => nkeys).delete
209
223
  end
210
-
224
+
211
225
  return result
212
226
  end
213
227
 
214
228
  ##
215
- # Answer with the value associated with the +key+, +nil+ if not found or
229
+ # Answer with the value associated with the +key+, +nil+ if not found or
216
230
  # expired.
217
231
  #
218
232
  # @author Lars Olsson
@@ -251,7 +265,7 @@ module Ramaze
251
265
  # @param [Object] key The value is stored with this key
252
266
  # @param [Object] value The key points to this value
253
267
  # @param [Hash] options for now, only :ttl => Fixnum is used.
254
- # @option options [Fixnum] :ttl The time in seconds after which the cache
268
+ # @option options [Fixnum] :ttl The time in seconds after which the cache
255
269
  # item should be expired.
256
270
  #
257
271
  def cache_store(key, value, options = {})
@@ -263,9 +277,9 @@ module Ramaze
263
277
  else
264
278
  ttl = Ramaze::Cache::Sequel.options[:ttl]
265
279
  end
266
-
280
+
267
281
  expires = Time.now + ttl if ttl
268
-
282
+
269
283
  # The row already exists, update it.
270
284
  if @dataset.filter(:key => nkey).count == 1
271
285
  serialized_value = serialize(value)
@@ -284,9 +298,10 @@ module Ramaze
284
298
  end
285
299
  end
286
300
 
287
- # Try to deserialize the value. If this fails we'll return a different
301
+ # Try to deserialize the value. If this fails we'll return a different
288
302
  # value
289
- deserialized = deserialize(@dataset.select(:value).filter(:key => nkey)
303
+ deserialized = deserialize(@dataset.select(:value) \
304
+ .filter(:key => nkey) \
290
305
  .limit(1).first[:value])
291
306
 
292
307
  if deserialized
@@ -295,7 +310,7 @@ module Ramaze
295
310
  return value
296
311
  end
297
312
  end
298
-
313
+
299
314
  ##
300
315
  # Prefixes the given key with current namespace.
301
316
  #
@@ -307,7 +322,7 @@ module Ramaze
307
322
  def namespaced(key)
308
323
  return [@namespace, key].join(':')
309
324
  end
310
-
325
+
311
326
  ##
312
327
  # Deserialize method, adapted from Sequels serialize plugin
313
328
  # This method will try to deserialize a value using Marshal.load
@@ -329,7 +344,7 @@ module Ramaze
329
344
  Ramaze::Log::warn("Failed to deserialize #{value.inspect}")
330
345
  end
331
346
 
332
- return nil
347
+ return nil
333
348
  end
334
349
  end
335
350
  end