merb 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/README +66 -31
  2. data/Rakefile +3 -1
  3. data/bin/merb +47 -13
  4. data/examples/app_skeleton/Rakefile +4 -3
  5. data/examples/app_skeleton/dist/app/helpers/global_helper.rb +6 -0
  6. data/examples/app_skeleton/dist/conf/merb.yml +11 -0
  7. data/examples/app_skeleton/dist/conf/mup.conf +5 -0
  8. data/examples/app_skeleton/dist/conf/router.rb +1 -3
  9. data/examples/app_skeleton/scripts/merb_stop +10 -2
  10. data/examples/sample_app/Rakefile +3 -3
  11. data/examples/sample_app/dist/app/controllers/files.rb +3 -3
  12. data/examples/sample_app/dist/app/controllers/posts.rb +25 -23
  13. data/examples/sample_app/dist/app/controllers/test.rb +7 -3
  14. data/examples/sample_app/dist/app/helpers/global_helper.rb +7 -0
  15. data/examples/sample_app/dist/app/helpers/posts_helper.rb +4 -0
  16. data/examples/sample_app/dist/app/views/layout/application.herb +5 -4
  17. data/examples/sample_app/dist/app/views/layout/foo.herb +1 -1
  18. data/examples/sample_app/dist/app/views/posts/new.herb +9 -2
  19. data/examples/sample_app/dist/app/views/shared/_test.herb +1 -0
  20. data/examples/sample_app/dist/conf/merb.yml +7 -7
  21. data/examples/sample_app/dist/conf/merb_init.rb +8 -1
  22. data/examples/sample_app/dist/conf/mup.conf +5 -11
  23. data/examples/sample_app/dist/conf/router.rb +1 -1
  24. data/examples/sample_app/dist/public/test.html +5 -0
  25. data/examples/sample_app/dist/schema/migrations/002_add_sessions_table.rb +1 -1
  26. data/examples/sample_app/dist/schema/schema.rb +1 -1
  27. data/examples/sample_app/log/merb.4000.pid +1 -0
  28. data/lib/merb.rb +35 -17
  29. data/lib/merb/core_ext.rb +2 -0
  30. data/lib/merb/{merb_class_extensions.rb → core_ext/merb_class.rb} +42 -0
  31. data/lib/merb/core_ext/merb_enumerable.rb +7 -0
  32. data/lib/merb/{merb_utils.rb → core_ext/merb_hash.rb} +1 -78
  33. data/lib/merb/core_ext/merb_kernel.rb +16 -0
  34. data/lib/merb/core_ext/merb_module.rb +10 -0
  35. data/lib/merb/core_ext/merb_numeric.rb +20 -0
  36. data/lib/merb/core_ext/merb_object.rb +6 -0
  37. data/lib/merb/core_ext/merb_string.rb +40 -0
  38. data/lib/merb/core_ext/merb_symbol.rb +12 -0
  39. data/lib/merb/merb_constants.rb +18 -0
  40. data/lib/merb/merb_controller.rb +150 -76
  41. data/lib/merb/{session/merb_drb_server.rb → merb_drb_server.rb} +13 -46
  42. data/lib/merb/merb_exceptions.rb +4 -0
  43. data/lib/merb/merb_handler.rb +29 -17
  44. data/lib/merb/merb_request.rb +95 -0
  45. data/lib/merb/merb_upload_handler.rb +46 -0
  46. data/lib/merb/merb_upload_progress.rb +48 -0
  47. data/lib/merb/merb_view_context.rb +46 -0
  48. data/lib/merb/merb_yaml_store.rb +31 -0
  49. data/lib/merb/mixins/basic_authentication_mixin.rb +2 -2
  50. data/lib/merb/mixins/controller_mixin.rb +24 -75
  51. data/lib/merb/mixins/erubis_capture_mixin.rb +84 -0
  52. data/lib/merb/mixins/javascript_mixin.rb +103 -19
  53. data/lib/merb/mixins/merb_status_codes.rb +59 -0
  54. data/lib/merb/mixins/render_mixin.rb +114 -40
  55. data/lib/merb/mixins/responder_mixin.rb +2 -1
  56. data/lib/merb/session/merb_ar_session.rb +120 -0
  57. data/lib/merb/session/merb_drb_session.rb +0 -6
  58. data/lib/merb/vendor/paginator/paginator.rb +102 -99
  59. metadata +44 -8
  60. data/examples/sample_app/script/startdrb +0 -8
  61. data/lib/merb/session/merb_session.rb +0 -64
  62. data/lib/mutex_hotfix.rb +0 -34
@@ -24,7 +24,8 @@ module Merb
24
24
  :yaml => %w[application/yaml text/yaml],
25
25
  :text => %w[text/plain],
26
26
  :html => %w[text/html */* application/html],
27
- :xml => %w[application/xml]
27
+ :xml => %w[application/xml],
28
+ :js => %w[application/json text/x-json]
28
29
  }
29
30
 
30
31
  def method_missing(method, *args)
@@ -0,0 +1,120 @@
1
+ require 'active_record'
2
+
3
+ module Merb
4
+
5
+ module SessionMixin
6
+
7
+ def setup_session
8
+ MERB_LOGGER.info("Setting up session")
9
+ @session = Merb::Session.persist(cookies)
10
+ @fingerprint_before = Marshal.dump(@session).hash
11
+ end
12
+
13
+ def finalize_session
14
+ MERB_LOGGER.info("Finalize session")
15
+ unless Marshal.dump(@session).hash == @fingerprint_before
16
+ @session.save
17
+ end
18
+ @headers['Set-Cookie'] = @cookies.map { |k,v| "#{k}=#{escape(v)}; path=/" if v != @cookies[k] } - [nil]
19
+ end
20
+
21
+ end
22
+
23
+ class Session < ::ActiveRecord::Base
24
+ # Customizable data column name. Defaults to 'data'.
25
+ cattr_accessor :data_column_name
26
+ self.data_column_name = 'data'
27
+
28
+ before_save :marshal_data!
29
+ before_save :raise_on_session_data_overflow!
30
+
31
+ RAND_CHARS = [*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z']
32
+
33
+ class << self
34
+ # Generates a new session ID and creates a row for the new session in the database.
35
+ def generate(cookies)
36
+ rand_max = RAND_CHARS.size
37
+ sid = (0...32).inject("") { |ret,_| ret << RAND_CHARS[rand(rand_max)] }
38
+ sess = create(:session_id => sid, :data => {})
39
+ cookies[:session_id] = sess.session_id
40
+ sess
41
+ end
42
+
43
+ # Gets the existing session based on the <tt>session_id</tt> available in cookies.
44
+ # If none is found, generates a new session.
45
+ def persist(cookies)
46
+ if cookies[:session_id]
47
+ session = find_by_session_id(cookies[:session_id])
48
+ end
49
+ unless session
50
+ session = generate(cookies)
51
+ end
52
+ session
53
+ end
54
+
55
+ # Don't try to reload ARStore::Session in dev mode.
56
+ def reloadable? #:nodoc:
57
+ false
58
+ end
59
+
60
+ def data_column_size_limit
61
+ @data_column_size_limit ||= columns_hash[@@data_column_name].limit
62
+ end
63
+
64
+ def marshal(data) Base64.encode64(Marshal.dump(data)) if data end
65
+ def unmarshal(data) Marshal.load(Base64.decode64(data)) if data end
66
+
67
+ def create_table!
68
+ connection.execute <<-end_sql
69
+ CREATE TABLE #{table_name} (
70
+ id INTEGER PRIMARY KEY,
71
+ #{connection.quote_column_name('session_id')} TEXT UNIQUE,
72
+ #{connection.quote_column_name(@@data_column_name)} TEXT(255)
73
+ )
74
+ end_sql
75
+ end
76
+
77
+ def drop_table!
78
+ connection.execute "DROP TABLE #{table_name}"
79
+ end
80
+ end
81
+
82
+ def [](key)
83
+ data[key]
84
+ end
85
+
86
+ def []=(key, val)
87
+ data[key] = val
88
+ end
89
+
90
+ # Lazy-unmarshal session state.
91
+ def data
92
+ @data ||= self.class.unmarshal(read_attribute(@@data_column_name)) || {}
93
+ end
94
+
95
+ # Has the session been loaded yet?
96
+ def loaded?
97
+ !! @data
98
+ end
99
+
100
+ private
101
+ attr_writer :data
102
+
103
+ def marshal_data!
104
+ return false if !loaded?
105
+ write_attribute(@@data_column_name, self.class.marshal(self.data))
106
+ end
107
+
108
+ # Ensures that the data about to be stored in the database is not
109
+ # larger than the data storage column. Raises
110
+ # ActionController::SessionOverflowError.
111
+ def raise_on_session_data_overflow!
112
+ return false if !loaded?
113
+ limit = self.class.data_column_size_limit
114
+ if loaded? and limit and read_attribute(@@data_column_name).size > limit
115
+ raise MerbController::SessionOverflowError
116
+ end
117
+ end
118
+ end
119
+
120
+ end
@@ -16,12 +16,6 @@ module Merb
16
16
  @headers['Set-Cookie'] = @cookies.map { |k,v| "#{k}=#{escape(v)}; path=/" if v != @k[k] } - [nil]
17
17
  end
18
18
 
19
- # accessor for @session. Please use session and
20
- # never @session directly.
21
- def session
22
- @session
23
- end
24
-
25
19
  end
26
20
 
27
21
  class Session
@@ -1,121 +1,124 @@
1
1
  require 'forwardable'
2
2
 
3
- class Paginator
4
-
5
- VERSION = '1.0.6'
6
-
7
- class ArgumentError < ::ArgumentError; end
8
- class MissingCountError < ArgumentError; end
9
- class MissingSelectError < ArgumentError; end
10
-
11
- attr_reader :per_page
12
-
13
- # Instantiate a new Paginator object
14
- #
15
- # Provide:
16
- # * A total count of the number of objects to paginate
17
- # * The number of objects in each page
18
- # * A block that returns the array of items
19
- # * The block is passed the item offset
20
- # (and the number of items to show per page, for
21
- # convenience, if the arity is 2)
22
- def initialize(count, per_page, &select)
23
- @count, @per_page = count, per_page
24
- unless select
25
- raise MissingSelectError, "Must provide block to select data for each page"
26
- end
27
- @select = select
28
- end
29
-
30
- # Total number of pages
31
- def number_of_pages
32
- (@count / @per_page) + (@count % @per_page > 0 ? 1 : 0)
33
- end
34
-
35
- # First page object
36
- def first
37
- page 1
38
- end
39
-
40
- # Last page object
41
- def last
42
- page number_of_pages
43
- end
44
-
45
- # Iterate through pages
46
- def each
47
- each_with_index do |item, index|
48
- yield item
49
- end
50
- end
51
-
52
- # Iterate through pages with indices
53
- def each_with_index
54
- 1.upto(number_of_pages) do |number|
55
- yield(page(number), number - 1)
56
- end
57
- end
58
-
59
- # Retrieve page object by number
60
- def page(number)
61
- number = (n = number.to_i) > 0 ? n : 1
62
- Page.new(self, number, lambda {
63
- offset = (number - 1) * @per_page
64
- args = [offset]
65
- args << @per_page if @select.arity == 2
66
- @select.call(*args)
67
- })
68
- end
3
+ module Merb
4
+ class Paginator
5
+
6
+ VERSION = '1.0.6'
69
7
 
70
- # Page object
71
- #
72
- # Retrieves items for a page and provides metadata about the position
73
- # of the page in the paginator
74
- class Page
8
+ class ArgumentError < ::ArgumentError; end
9
+ class MissingCountError < ArgumentError; end
10
+ class MissingSelectError < ArgumentError; end
75
11
 
76
- extend Forwardable
77
- def_delegator :@pager, :first, :first
78
- def_delegator :@pager, :last, :last
79
- def_delegator :items, :each
80
- def_delegator :items, :each_with_index
81
-
82
- attr_reader :number, :pager
12
+ attr_reader :per_page
83
13
 
84
- def initialize(pager, number, select) #:nodoc:
85
- @pager, @number = pager, number
14
+ # Instantiate a new Paginator object
15
+ #
16
+ # Provide:
17
+ # * A total count of the number of objects to paginate
18
+ # * The number of objects in each page
19
+ # * A block that returns the array of items
20
+ # * The block is passed the item offset
21
+ # (and the number of items to show per page, for
22
+ # convenience, if the arity is 2)
23
+ def initialize(count, per_page, &select)
24
+ @count, @per_page = count, per_page
25
+ unless select
26
+ raise MissingSelectError, "Must provide block to select data for each page"
27
+ end
86
28
  @select = select
87
29
  end
88
30
 
89
- # Retrieve the items for this page
90
- # * Caches
91
- def items
92
- @items ||= @select.call
31
+ # Total number of pages
32
+ def number_of_pages
33
+ (@count / @per_page) + (@count % @per_page > 0 ? 1 : 0)
34
+ end
35
+
36
+ # First page object
37
+ def first
38
+ page 1
93
39
  end
94
40
 
95
- # Checks to see if there's a page before this one
96
- def prev?
97
- @number > 1
41
+ # Last page object
42
+ def last
43
+ page number_of_pages
98
44
  end
99
45
 
100
- # Get previous page (if possible)
101
- def prev
102
- @pager.page(@number - 1) if prev?
46
+ # Iterate through pages
47
+ def each
48
+ each_with_index do |item, index|
49
+ yield item
50
+ end
103
51
  end
104
52
 
105
- # Checks to see if there's a page after this one
106
- def next?
107
- @number < @pager.number_of_pages
53
+ # Iterate through pages with indices
54
+ def each_with_index
55
+ 1.upto(number_of_pages) do |number|
56
+ yield(page(number), number - 1)
57
+ end
108
58
  end
109
59
 
110
- # Get next page (if possible)
111
- def next
112
- @pager.page(@number + 1) if next?
60
+ # Retrieve page object by number
61
+ def page(number)
62
+ number = (n = number.to_i) > 0 ? n : 1
63
+ Page.new(self, number, lambda {
64
+ offset = (number - 1) * @per_page
65
+ args = [offset]
66
+ args << @per_page if @select.arity == 2
67
+ @select.call(*args)
68
+ })
113
69
  end
114
70
 
115
- def ==(other) #:nodoc:
116
- @pager == other.pager && self.number == other.number
71
+ # Page object
72
+ #
73
+ # Retrieves items for a page and provides metadata about the position
74
+ # of the page in the paginator
75
+ class Page
76
+
77
+ extend Forwardable
78
+ def_delegator :@pager, :first, :first
79
+ def_delegator :@pager, :last, :last
80
+ def_delegator :items, :each
81
+ def_delegator :items, :each_with_index
82
+
83
+ attr_reader :number, :pager
84
+
85
+ def initialize(pager, number, select) #:nodoc:
86
+ @pager, @number = pager, number
87
+ @select = select
88
+ end
89
+
90
+ # Retrieve the items for this page
91
+ # * Caches
92
+ def items
93
+ @items ||= @select.call
94
+ end
95
+
96
+ # Checks to see if there's a page before this one
97
+ def prev?
98
+ @number > 1
99
+ end
100
+
101
+ # Get previous page (if possible)
102
+ def prev
103
+ @pager.page(@number - 1) if prev?
104
+ end
105
+
106
+ # Checks to see if there's a page after this one
107
+ def next?
108
+ @number < @pager.number_of_pages
109
+ end
110
+
111
+ # Get next page (if possible)
112
+ def next
113
+ @pager.page(@number + 1) if next?
114
+ end
115
+
116
+ def ==(other) #:nodoc:
117
+ @pager == other.pager && self.number == other.number
118
+ end
119
+
117
120
  end
118
121
 
119
122
  end
120
123
 
121
- end
124
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: merb
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.7
7
- date: 2006-11-29 00:00:00 -08:00
6
+ version: 0.0.8
7
+ date: 2006-12-17 00:00:00 -08:00
8
8
  summary: Merb == Mongrel + Erb. Pocket rocket web framework.
9
9
  require_paths:
10
10
  - lib
@@ -40,24 +40,41 @@ files:
40
40
  - lib/merb
41
41
  - lib/merb.rb
42
42
  - lib/merb_tasks.rb
43
- - lib/mutex_hotfix.rb
44
43
  - lib/tasks
45
- - lib/merb/merb_class_extensions.rb
44
+ - lib/merb/core_ext
45
+ - lib/merb/core_ext.rb
46
+ - lib/merb/merb_constants.rb
46
47
  - lib/merb/merb_controller.rb
48
+ - lib/merb/merb_drb_server.rb
49
+ - lib/merb/merb_exceptions.rb
47
50
  - lib/merb/merb_handler.rb
51
+ - lib/merb/merb_request.rb
48
52
  - lib/merb/merb_router.rb
49
- - lib/merb/merb_utils.rb
53
+ - lib/merb/merb_upload_handler.rb
54
+ - lib/merb/merb_upload_progress.rb
55
+ - lib/merb/merb_view_context.rb
56
+ - lib/merb/merb_yaml_store.rb
50
57
  - lib/merb/mixins
51
58
  - lib/merb/session
52
59
  - lib/merb/vendor
60
+ - lib/merb/core_ext/merb_class.rb
61
+ - lib/merb/core_ext/merb_enumerable.rb
62
+ - lib/merb/core_ext/merb_hash.rb
63
+ - lib/merb/core_ext/merb_kernel.rb
64
+ - lib/merb/core_ext/merb_module.rb
65
+ - lib/merb/core_ext/merb_numeric.rb
66
+ - lib/merb/core_ext/merb_object.rb
67
+ - lib/merb/core_ext/merb_string.rb
68
+ - lib/merb/core_ext/merb_symbol.rb
53
69
  - lib/merb/mixins/basic_authentication_mixin.rb
54
70
  - lib/merb/mixins/controller_mixin.rb
71
+ - lib/merb/mixins/erubis_capture_mixin.rb
55
72
  - lib/merb/mixins/javascript_mixin.rb
73
+ - lib/merb/mixins/merb_status_codes.rb
56
74
  - lib/merb/mixins/render_mixin.rb
57
75
  - lib/merb/mixins/responder_mixin.rb
58
- - lib/merb/session/merb_drb_server.rb
76
+ - lib/merb/session/merb_ar_session.rb
59
77
  - lib/merb/session/merb_drb_session.rb
60
- - lib/merb/session/merb_session.rb
61
78
  - lib/merb/vendor/paginator
62
79
  - lib/merb/vendor/paginator/paginator.rb
63
80
  - lib/merb/vendor/paginator/README.txt
@@ -77,10 +94,14 @@ files:
77
94
  - examples/app_skeleton/dist/public
78
95
  - examples/app_skeleton/dist/schema
79
96
  - examples/app_skeleton/dist/app/controllers
97
+ - examples/app_skeleton/dist/app/helpers
80
98
  - examples/app_skeleton/dist/app/models
81
99
  - examples/app_skeleton/dist/app/views
100
+ - examples/app_skeleton/dist/app/helpers/global_helper.rb
82
101
  - examples/app_skeleton/dist/app/views/layout
102
+ - examples/app_skeleton/dist/conf/merb.yml
83
103
  - examples/app_skeleton/dist/conf/merb_init.rb
104
+ - examples/app_skeleton/dist/conf/mup.conf
84
105
  - examples/app_skeleton/dist/conf/router.rb
85
106
  - examples/app_skeleton/dist/public/files
86
107
  - examples/app_skeleton/dist/public/images
@@ -107,16 +128,20 @@ files:
107
128
  - examples/sample_app/dist/public
108
129
  - examples/sample_app/dist/schema
109
130
  - examples/sample_app/dist/app/controllers
131
+ - examples/sample_app/dist/app/helpers
110
132
  - examples/sample_app/dist/app/models
111
133
  - examples/sample_app/dist/app/views
112
134
  - examples/sample_app/dist/app/controllers/files.rb
113
135
  - examples/sample_app/dist/app/controllers/posts.rb
114
136
  - examples/sample_app/dist/app/controllers/test.rb
137
+ - examples/sample_app/dist/app/helpers/global_helper.rb
138
+ - examples/sample_app/dist/app/helpers/posts_helper.rb
115
139
  - examples/sample_app/dist/app/models/comment.rb
116
140
  - examples/sample_app/dist/app/models/post.rb
117
141
  - examples/sample_app/dist/app/views/files
118
142
  - examples/sample_app/dist/app/views/layout
119
143
  - examples/sample_app/dist/app/views/posts
144
+ - examples/sample_app/dist/app/views/shared
120
145
  - examples/sample_app/dist/app/views/test
121
146
  - examples/sample_app/dist/app/views/files/progress.jerb
122
147
  - examples/sample_app/dist/app/views/files/start.herb
@@ -129,6 +154,7 @@ files:
129
154
  - examples/sample_app/dist/app/views/posts/new.herb
130
155
  - examples/sample_app/dist/app/views/posts/show.herb
131
156
  - examples/sample_app/dist/app/views/posts/xml_test.xerb
157
+ - examples/sample_app/dist/app/views/shared/_test.herb
132
158
  - examples/sample_app/dist/app/views/test/foo.herb
133
159
  - examples/sample_app/dist/app/views/test/hello.herb
134
160
  - examples/sample_app/dist/app/views/test/json.jerb
@@ -140,6 +166,7 @@ files:
140
166
  - examples/sample_app/dist/public/images
141
167
  - examples/sample_app/dist/public/javascripts
142
168
  - examples/sample_app/dist/public/stylesheets
169
+ - examples/sample_app/dist/public/test.html
143
170
  - examples/sample_app/dist/public/images/bg.jpg
144
171
  - examples/sample_app/dist/public/images/book.gif
145
172
  - examples/sample_app/dist/public/images/booksmall.gif
@@ -160,9 +187,9 @@ files:
160
187
  - examples/sample_app/dist/schema/schema.rb
161
188
  - examples/sample_app/dist/schema/migrations/001_add_comments_to_posts.rb
162
189
  - examples/sample_app/dist/schema/migrations/002_add_sessions_table.rb
190
+ - examples/sample_app/log/merb.4000.pid
163
191
  - examples/sample_app/script/merb_stop
164
192
  - examples/sample_app/script/new_migration
165
- - examples/sample_app/script/startdrb
166
193
  - examples/sample_app/test/fixtures
167
194
  - examples/sample_app/test/spec
168
195
  - examples/sample_app/test/test_helper.rb
@@ -220,3 +247,12 @@ dependencies:
220
247
  - !ruby/object:Gem::Version
221
248
  version: 0.0.0
222
249
  version:
250
+ - !ruby/object:Gem::Dependency
251
+ name: mime-types
252
+ version_requirement:
253
+ version_requirements: !ruby/object:Gem::Version::Requirement
254
+ requirements:
255
+ - - ">"
256
+ - !ruby/object:Gem::Version
257
+ version: 0.0.0
258
+ version: