wee 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/ChangeLog +75 -0
  2. data/README +17 -9
  3. data/Rakefile +2 -2
  4. data/TODO +20 -0
  5. data/benchmark/Makefile +14 -9
  6. data/benchmark/counter.rb +11 -30
  7. data/benchmark/report_req.rb +12 -0
  8. data/doc/rdoc/classes/Array.html +12 -12
  9. data/doc/rdoc/classes/Cache/StorageCache.html +38 -38
  10. data/doc/rdoc/classes/Cache/Strategy/CapacityBounded.html +30 -30
  11. data/doc/rdoc/classes/Cache/Strategy/LFU.html +24 -24
  12. data/doc/rdoc/classes/Cache/Strategy/LRU.html +24 -24
  13. data/doc/rdoc/classes/Cache/Strategy/Unbounded.html +24 -24
  14. data/doc/rdoc/classes/Enumerable.html +6 -6
  15. data/doc/rdoc/classes/Object.html +12 -12
  16. data/doc/rdoc/classes/OgApplication.html +126 -0
  17. data/doc/rdoc/classes/OgScaffolder.html +401 -0
  18. data/doc/rdoc/classes/OgSession.html +172 -0
  19. data/doc/rdoc/classes/String.html +12 -12
  20. data/doc/rdoc/classes/Struct.html +12 -12
  21. data/doc/rdoc/classes/Wee.html +5 -62
  22. data/doc/rdoc/classes/Wee/AnswerDecoration.html +9 -9
  23. data/doc/rdoc/classes/Wee/Application.html +107 -39
  24. data/doc/rdoc/classes/Wee/Brush.html +22 -18
  25. data/doc/rdoc/classes/Wee/Brush/ActionCallbackMixin.html +17 -11
  26. data/doc/rdoc/classes/Wee/Brush/ActionURLCallbackMixin.html +18 -10
  27. data/doc/rdoc/classes/Wee/Brush/AnchorTag.html +30 -64
  28. data/doc/rdoc/classes/Wee/Brush/FileUploadTag.html +8 -10
  29. data/doc/rdoc/classes/Wee/Brush/FormTag.html +27 -79
  30. data/doc/rdoc/classes/Wee/Brush/GenericEncodedTextBrush.html +12 -12
  31. data/doc/rdoc/classes/Wee/Brush/GenericSingleTagBrush.html +146 -0
  32. data/doc/rdoc/classes/Wee/Brush/GenericTagBrush.html +179 -65
  33. data/doc/rdoc/classes/Wee/Brush/GenericTextBrush.html +12 -12
  34. data/doc/rdoc/classes/Wee/Brush/ImageButtonTag.html +16 -18
  35. data/doc/rdoc/classes/Wee/Brush/ImageTag.html +203 -0
  36. data/doc/rdoc/classes/Wee/Brush/InputCallbackMixin.html +17 -11
  37. data/doc/rdoc/classes/Wee/Brush/InputTag.html +15 -15
  38. data/doc/rdoc/classes/Wee/Brush/JavascriptTag.html +147 -0
  39. data/doc/rdoc/classes/Wee/Brush/Page.html +17 -17
  40. data/doc/rdoc/classes/Wee/Brush/SelectListTag.html +25 -50
  41. data/doc/rdoc/classes/Wee/Brush/SelectOptionTag.html +7 -38
  42. data/doc/rdoc/classes/Wee/Brush/SubmitButtonTag.html +7 -7
  43. data/doc/rdoc/classes/Wee/Brush/TableDataTag.html +15 -16
  44. data/doc/rdoc/classes/Wee/Brush/TableHeaderTag.html +7 -7
  45. data/doc/rdoc/classes/Wee/Brush/TableRowTag.html +65 -50
  46. data/doc/rdoc/classes/Wee/Brush/TableTag.html +7 -7
  47. data/doc/rdoc/classes/Wee/Brush/TextAreaTag.html +14 -64
  48. data/doc/rdoc/classes/Wee/Brush/TextInputTag.html +7 -7
  49. data/doc/rdoc/classes/Wee/Brush/ToCallback.html +146 -0
  50. data/doc/rdoc/classes/Wee/CallbackRegistry.html +40 -40
  51. data/doc/rdoc/classes/Wee/CallbackStream.html +18 -18
  52. data/doc/rdoc/classes/Wee/Canvas.html +24 -24
  53. data/doc/rdoc/classes/Wee/Component.html +232 -149
  54. data/doc/rdoc/classes/Wee/Component/OnAnswer.html +153 -0
  55. data/doc/rdoc/classes/Wee/Decoration.html +42 -42
  56. data/doc/rdoc/classes/Wee/Delegate.html +27 -27
  57. data/doc/rdoc/classes/Wee/ErrorResponse.html +12 -12
  58. data/doc/rdoc/classes/Wee/FormDecoration.html +148 -0
  59. data/doc/rdoc/classes/Wee/GenericResponse.html +6 -6
  60. data/doc/rdoc/classes/Wee/HtmlCanvas.html +296 -215
  61. data/doc/rdoc/classes/Wee/HtmlWriter.html +83 -81
  62. data/doc/rdoc/classes/Wee/LiteralMethodCallback.html +21 -16
  63. data/doc/rdoc/classes/Wee/MessageBox.html +180 -0
  64. data/doc/rdoc/classes/Wee/PageDecoration.html +30 -30
  65. data/doc/rdoc/classes/Wee/Presenter.html +237 -69
  66. data/doc/rdoc/classes/Wee/RedirectResponse.html +6 -6
  67. data/doc/rdoc/classes/Wee/RefreshResponse.html +6 -6
  68. data/doc/rdoc/classes/Wee/Request.html +18 -18
  69. data/doc/rdoc/classes/Wee/RequestHandler.html +43 -39
  70. data/doc/rdoc/classes/Wee/Response.html +24 -24
  71. data/doc/rdoc/classes/Wee/Session.html +746 -72
  72. data/doc/rdoc/classes/Wee/SimpleIdGenerator.html +18 -18
  73. data/doc/rdoc/classes/Wee/Snapshot.html +19 -19
  74. data/doc/rdoc/classes/Wee/Utils.html +138 -2
  75. data/doc/rdoc/classes/Wee/Utils/LRUCache.html +7 -7
  76. data/doc/rdoc/classes/Wee/ValueHolder.html +18 -18
  77. data/doc/rdoc/classes/Wee/WEBrickAdaptor.html +43 -68
  78. data/doc/rdoc/classes/Wee/WrapperDecoration.html +150 -0
  79. data/doc/rdoc/created.rid +1 -1
  80. data/doc/rdoc/files/README.html +29 -15
  81. data/doc/rdoc/files/lib/wee/adaptors/webrick_rb.html +1 -1
  82. data/doc/rdoc/files/lib/wee/application_rb.html +1 -1
  83. data/doc/rdoc/files/lib/wee/components/form_decoration_rb.html +101 -0
  84. data/doc/rdoc/files/lib/wee/components/messagebox_rb.html +101 -0
  85. data/doc/rdoc/files/lib/wee/components/page_decoration_rb.html +1 -1
  86. data/doc/rdoc/files/lib/wee/components/wrapper_decoration_rb.html +101 -0
  87. data/doc/rdoc/files/lib/wee/components_rb.html +4 -1
  88. data/doc/rdoc/files/lib/wee/continuation/core/component_rb.html +101 -0
  89. data/doc/rdoc/files/lib/wee/continuation/session_rb.html +110 -0
  90. data/doc/rdoc/files/lib/wee/continuation_rb.html +116 -0
  91. data/doc/rdoc/files/lib/wee/core/callback_rb.html +1 -1
  92. data/doc/rdoc/files/lib/wee/core/component_rb.html +1 -1
  93. data/doc/rdoc/files/lib/wee/core/presenter_rb.html +1 -1
  94. data/doc/rdoc/files/lib/wee/core_rb.html +3 -3
  95. data/doc/rdoc/files/lib/wee/databases/og_rb.html +108 -0
  96. data/doc/rdoc/files/lib/wee/renderer/html/brushes_rb.html +1 -1
  97. data/doc/rdoc/files/lib/wee/renderer/html/canvas_rb.html +1 -1
  98. data/doc/rdoc/files/lib/wee/renderer/html/writer_rb.html +1 -1
  99. data/doc/rdoc/files/lib/wee/requesthandler_rb.html +1 -1
  100. data/doc/rdoc/files/lib/wee/session_rb.html +1 -2
  101. data/doc/rdoc/files/lib/wee/utils/autoreload_rb.html +1 -1
  102. data/doc/rdoc/files/lib/wee/utils/cache_rb.html +1 -1
  103. data/doc/rdoc/files/lib/wee/utils/helper_rb.html +1 -8
  104. data/doc/rdoc/files/lib/wee/utils_rb.html +110 -0
  105. data/doc/rdoc/files/lib/wee_rb.html +1 -1
  106. data/doc/rdoc/fr_class_index.html +11 -1
  107. data/doc/rdoc/fr_file_index.html +8 -0
  108. data/doc/rdoc/fr_method_index.html +269 -228
  109. data/examples/calculator.rb +69 -0
  110. data/examples/calendar.rb +5 -17
  111. data/examples/example.rb +2 -2
  112. data/examples/hw.rb +17 -0
  113. data/examples/live-update.rb +45 -0
  114. data/examples/og-test.rb +51 -0
  115. data/lib/wee.rb +1 -1
  116. data/lib/wee/adaptors/webrick.rb +2 -0
  117. data/lib/wee/application.rb +16 -0
  118. data/lib/wee/components.rb +3 -0
  119. data/lib/wee/components/form_decoration.rb +7 -0
  120. data/{test → lib/wee}/components/messagebox.rb +1 -1
  121. data/lib/wee/components/page_decoration.rb +5 -5
  122. data/lib/wee/components/wrapper_decoration.rb +7 -0
  123. data/lib/wee/continuation.rb +5 -0
  124. data/lib/wee/continuation/core/component.rb +55 -0
  125. data/lib/wee/continuation/session.rb +217 -0
  126. data/lib/wee/core/callback.rb +11 -6
  127. data/lib/wee/core/component.rb +45 -33
  128. data/lib/wee/core/presenter.rb +68 -0
  129. data/lib/wee/databases/og.rb +114 -0
  130. data/lib/wee/renderer/html/brushes.rb +179 -98
  131. data/lib/wee/renderer/html/canvas.rb +37 -13
  132. data/lib/wee/renderer/html/writer.rb +34 -32
  133. data/lib/wee/requesthandler.rb +6 -3
  134. data/lib/wee/session.rb +73 -54
  135. data/lib/wee/utils.rb +5 -0
  136. data/lib/wee/utils/autoreload.rb +1 -1
  137. data/lib/wee/utils/cache.rb +0 -2
  138. data/lib/wee/utils/helper.rb +40 -8
  139. data/test/components/calltest-cont.rb +16 -0
  140. data/test/components/calltest.rb +15 -10
  141. data/test/stress.rb +31 -28
  142. data/test/stress_and_measure.rb +53 -0
  143. data/test/stressed_application.rb +15 -0
  144. data/test/test_html_writer.rb +9 -4
  145. metadata +236 -195
  146. data/benchmark/bench.sh +0 -24
@@ -36,27 +36,43 @@ class HtmlCanvas < Canvas
36
36
  attr_reader :document
37
37
  attr_accessor :current_component
38
38
 
39
+ def self.generic_tag(*attrs)
40
+ attrs.each { |a|
41
+ class_eval "
42
+ def #{ a }(*args, &block)
43
+ handle(Brush::GenericTagBrush.new('#{ a }'), *args, &block)
44
+ end
45
+ "
46
+ }
47
+ end
48
+
49
+ def self.generic_single_tag(*attrs)
50
+ attrs.each { |a|
51
+ class_eval "
52
+ def #{ a }(*args, &block)
53
+ handle(Brush::GenericSingleTagBrush.new('#{ a }'), *args, &block)
54
+ end
55
+ "
56
+ }
57
+ end
58
+
39
59
  def initialize(rendering_context)
40
60
  super()
41
61
  @rendering_context = rendering_context
42
62
  @document = rendering_context.document
43
63
  end
44
64
 
45
- def method_missing(id, *args, &block)
46
- handle(Brush::GenericTagBrush.new(id.to_s), *args, &block)
47
- end
65
+ generic_tag :html, :head, :body, :title, :style, :h1, :h2, :h3, :h4, :h5, :div
66
+ generic_single_tag :link
48
67
 
49
- def url_for_callback(symbol=nil, &block)
50
- raise ArgumentError if symbol and block
51
- block = self.current_component.method(symbol) unless block
68
+ def url_for_callback(callback)
52
69
  req = self.rendering_context.request
53
- url = req.build_url(req.request_handler_id, req.page_id, register_callback(:action, &block))
70
+ url = req.build_url(req.request_handler_id, req.page_id, register_callback(:action, callback))
54
71
  return url
55
72
  end
56
73
 
57
- def register_callback(type, &block)
58
- raise ArgumentError, "no callback block given" if block.nil?
59
- self.rendering_context.callbacks.register_for(self.current_component, type, &block)
74
+ def register_callback(type, callback)
75
+ self.rendering_context.callbacks.register_for(self.current_component, type, callback)
60
76
  end
61
77
 
62
78
  def table(*args, &block)
@@ -127,16 +143,24 @@ class HtmlCanvas < Canvas
127
143
  handle(Brush::GenericTagBrush.new("b"), *args, &block)
128
144
  end
129
145
 
146
+ def javascript(*args, &block)
147
+ handle(Brush::JavascriptTag.new, *args, &block)
148
+ end
149
+
130
150
  def paragraph
131
- set_brush(Brush::GenericTagBrush.new("p"))
151
+ set_brush(Brush::GenericSingleTagBrush.new("p"))
132
152
  end
133
153
 
134
154
  def break
135
- set_brush(Brush::GenericTagBrush.new("br"))
155
+ set_brush(Brush::GenericSingleTagBrush.new("br"))
136
156
  end
137
157
 
138
158
  def image
139
- set_brush(Brush::GenericTagBrush.new("img"))
159
+ handle(Brush::ImageTag.new)
160
+ end
161
+
162
+ def link_css(url)
163
+ link.type('text/css').rel('stylesheet').href(url)
140
164
  end
141
165
 
142
166
  def text(str)
@@ -13,7 +13,6 @@ require 'cgi'
13
13
  # w.end_tag('body')
14
14
  # w.end_tag('html')
15
15
  #
16
- # p w.valid? # => true
17
16
  # p doc # => '<html><body><a href="http://...">link</a></body></html>'
18
17
  #
19
18
 
@@ -22,46 +21,51 @@ class Wee::HtmlWriter
22
21
 
23
22
  def initialize(port)
24
23
  @port = port
25
- @open_start_tag = false
26
- @tag_stack = []
27
24
  end
28
25
 
29
- def start_tag(tag, attributes={})
30
- @port << ">" if @open_start_tag
31
- @open_start_tag = true
32
- @tag_stack.push(tag)
33
-
34
- @port << "<#{ tag }"
35
- attributes.each {|k, v|
36
- if v
37
- @port << %[ #{ k }="#{ v }"]
38
- else
39
- @port << %[ #{ k }]
40
- end
41
- }
26
+ def start_tag(tag, attributes=nil)
27
+ if attributes
28
+ @port << "<#{ tag }"
29
+ attributes.each {|k, v|
30
+ if v
31
+ @port << %[ #{ k }="#{ v }"]
32
+ else
33
+ @port << %[ #{ k }]
34
+ end
35
+ }
36
+ @port << ">"
37
+ else
38
+ @port << "<#{ tag }>"
39
+ end
42
40
 
43
41
  self
44
42
  end
45
43
 
46
- def end_tag(tag)
47
- raise "unbalanced html" if @tag_stack.pop != tag
48
-
49
- if @open_start_tag
50
- @port << "/>"
51
- @open_start_tag = false
44
+ def single_tag(tag, attributes=nil)
45
+ if attributes
46
+ @port << "<#{ tag }"
47
+ attributes.each {|k, v|
48
+ if v
49
+ @port << %[ #{ k }="#{ v }"]
50
+ else
51
+ @port << %[ #{ k }]
52
+ end
53
+ }
54
+ @port << " />"
52
55
  else
53
- @port << "</#{ tag }>"
56
+ @port << "<#{ tag } />"
54
57
  end
55
58
 
56
59
  self
57
60
  end
58
61
 
59
- def text(str)
60
- if @open_start_tag
61
- @port << ">"
62
- @open_start_tag = false
63
- end
62
+ def end_tag(tag)
63
+ @port << "</#{ tag }>"
64
+
65
+ self
66
+ end
64
67
 
68
+ def text(str)
65
69
  @port << str.to_s
66
70
 
67
71
  self
@@ -69,10 +73,8 @@ class Wee::HtmlWriter
69
73
  alias << text
70
74
 
71
75
  def encode_text(str)
72
- text(CGI.escapeHTML(str.to_s))
73
- end
76
+ @port << CGI.escapeHTML(str.to_s)
74
77
 
75
- def valid?
76
- @tag_stack.empty?
78
+ self
77
79
  end
78
80
  end
@@ -30,6 +30,9 @@ class Wee::RequestHandler
30
30
 
31
31
 
32
32
  # Terminates the handler.
33
+ #
34
+ # This will usually not immediatly terminate the handler from running, but
35
+ # further requests will not be answered.
33
36
 
34
37
  def teminate
35
38
  @running = false
@@ -39,14 +42,14 @@ class Wee::RequestHandler
39
42
 
40
43
  def alive?
41
44
  return false if not @running
42
- return false if @max_requests and @request_count >= @max_requests
45
+ return @running = false if @max_requests and @request_count >= @max_requests
43
46
 
44
47
  now = Time.now
45
48
  inactivity = now - @last_access
46
49
  lifetime = now - @creation_time
47
50
 
48
- return false if @expire_after and inactivity > @expire_after
49
- return false if @max_lifetime and lifetime > @max_lifetime
51
+ return @running = false if @expire_after and inactivity > @expire_after
52
+ return @running = false if @max_lifetime and lifetime > @max_lifetime
50
53
  return true
51
54
  end
52
55
 
data/lib/wee/session.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'wee/page'
2
2
  require 'thread'
3
- require 'timeout'
4
3
 
5
4
  class Wee::Session < Wee::RequestHandler
6
5
  attr_accessor :root_component, :page_store
@@ -15,7 +14,9 @@ class Wee::Session < Wee::RequestHandler
15
14
  Thread.current[:wee_session] = self
16
15
 
17
16
  @idgen = Wee::SimpleIdGenerator.new
18
- @in_queue, @out_queue = SizedQueue.new(1), SizedQueue.new(1)
17
+
18
+ # to serialize the requests we need a mutex
19
+ @mutex = Mutex.new
19
20
 
20
21
  block.call(self)
21
22
 
@@ -24,7 +25,6 @@ class Wee::Session < Wee::RequestHandler
24
25
 
25
26
  @initial_snapshot = snapshot()
26
27
 
27
- start_request_response_loop
28
28
  super()
29
29
  ensure
30
30
  Thread.current[:wee_session] = nil
@@ -36,50 +36,24 @@ class Wee::Session < Wee::RequestHandler
36
36
  end
37
37
 
38
38
  # called by application to send the session a request
39
- def handle_request(context)
40
- super
41
-
42
- # Send a request to the session. If the session is currently busy
43
- # processing another request, this will block.
44
- @in_queue.push(context)
45
-
46
- # Wait for the response.
47
- return @out_queue.pop
48
- end
49
39
 
50
- def start_request_response_loop
51
- Thread.abort_on_exception = true
52
- Thread.new {
53
- Thread.current[:wee_session] = self
54
- loop {
40
+ def handle_request(context)
41
+ @mutex.synchronize do
42
+ begin
43
+ Thread.current[:wee_session] = self
44
+ @context = context
45
+ super
46
+ awake
47
+ process_request
48
+ sleep
49
+ rescue Exception => exn
50
+ context.response = Wee::ErrorResponse.new(exn)
51
+ ensure
52
+ Thread.current[:wee_session] = nil
55
53
  @context = nil
56
-
57
- # get a request, check whether this session is alive after every 5
58
- # seconds.
59
- while @context.nil?
60
- begin
61
- Timeout.timeout(5) {
62
- @context = @in_queue.pop
63
- }
64
- rescue Timeout::Error
65
- break unless alive?
66
- end
67
- end
68
-
69
- # abort thread if no longer alive
70
- break if not alive?
71
-
72
- raise "invalid request" if @context.nil?
73
-
74
- begin
75
- process_request
76
- rescue Exception => exn
77
- @context.response = Wee::ErrorResponse.new(exn)
78
- end
79
- @out_queue.push(@context)
80
- }
81
- p "session loop terminated" if $DEBUG
82
- }
54
+ @page = nil
55
+ end
56
+ end
83
57
  end
84
58
 
85
59
  def create_page(snapshot)
@@ -87,6 +61,16 @@ class Wee::Session < Wee::RequestHandler
87
61
  page = Wee::Page.new(snapshot, Wee::CallbackRegistry.new(idgen))
88
62
  end
89
63
 
64
+ # Is called before process_request is invoked
65
+ # Can be used to setup e.g. a database connection.
66
+ def awake
67
+ end
68
+
69
+ # Is called after process_request is run
70
+ # Can be used to release e.g. a database connection.
71
+ def sleep
72
+ end
73
+
90
74
  def process_request
91
75
  if @context.request.page_id.nil?
92
76
 
@@ -95,11 +79,11 @@ class Wee::Session < Wee::RequestHandler
95
79
 
96
80
  handle_new_page_view(@context, @initial_snapshot)
97
81
 
98
- elsif page = @page_store.fetch(@context.request.page_id, false)
82
+ elsif @page = @page_store.fetch(@context.request.page_id, false)
99
83
 
100
84
  # A valid page_id was specified and the corresponding page exists.
101
85
 
102
- page.snapshot.restore
86
+ @page.snapshot.restore
103
87
 
104
88
  p @context.request.fields if $DEBUG
105
89
 
@@ -112,9 +96,9 @@ class Wee::Session < Wee::RequestHandler
112
96
  # 2. Render the page (respond).
113
97
  # 3. Store the page back into the store
114
98
 
115
- page = create_page(page.snapshot) # remove all action/input handlers
116
- respond(@context, page.callbacks) # render
117
- @page_store[@context.request.page_id] = page # store
99
+ @page = create_page(@page.snapshot) # remove all action/input handlers
100
+ respond(@context, @page.callbacks) # render
101
+ @page_store[@context.request.page_id] = @page # store
118
102
 
119
103
  else
120
104
 
@@ -123,16 +107,27 @@ class Wee::Session < Wee::RequestHandler
123
107
  # We process the request and invoke actions/inputs. Then we generate a
124
108
  # new page view.
125
109
 
126
- callback_stream = Wee::CallbackStream.new(page.callbacks, @context.request.fields)
110
+ callback_stream = Wee::CallbackStream.new(@page.callbacks, @context.request.fields)
127
111
 
128
112
  if callback_stream.all_of_type(:action).size > 1
129
113
  raise "Not allowed to specify more than one action callback"
130
114
  end
131
115
 
132
- catch(:wee_back_to_session) {
133
- @root_component.process_callbacks_chain(callback_stream)
116
+ live_update_response = catch(:wee_live_update) {
117
+ catch(:wee_back_to_session) {
118
+ @root_component.process_callbacks_chain(callback_stream)
119
+ }
120
+ nil
134
121
  }
135
- handle_new_page_view(@context)
122
+
123
+ if live_update_response
124
+ # replace existing page with new snapshot
125
+ @page.snapshot = self.snapshot
126
+ @page_store[@context.request.page_id] = @page
127
+ @context.response = live_update_response
128
+ else
129
+ handle_new_page_view(@context)
130
+ end
136
131
 
137
132
  end
138
133
 
@@ -150,6 +145,30 @@ class Wee::Session < Wee::RequestHandler
150
145
  end
151
146
  end
152
147
 
148
+ def current_context
149
+ @context
150
+ end
151
+
152
+ def current_page
153
+ @page
154
+ end
155
+
156
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
157
+ # :section: Properties
158
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159
+
160
+ attr_accessor :properties
161
+
162
+ # Returns an "owned" property for the given +klass+.
163
+
164
+ def get_property(prop, klass)
165
+ if @properties
166
+ @properties.fetch(klass, {})[prop]
167
+ else
168
+ nil
169
+ end
170
+ end
171
+
153
172
  private
154
173
 
155
174
  def handle_new_page_view(context, snapshot=nil)
data/lib/wee/utils.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Wee::Utils; end
2
+
3
+ require 'wee/utils/autoreload'
4
+ require 'wee/utils/cache'
5
+ require 'wee/utils/helper'
@@ -1,4 +1,4 @@
1
- module Wee
1
+ module Wee::Utils
2
2
  module_function
3
3
 
4
4
  def autoreload(check_interval=10)
@@ -1,7 +1,5 @@
1
1
  require 'cache/cache'
2
2
 
3
- module Wee::Utils; end
4
-
5
3
  class Wee::Utils::LRUCache < Cache::StorageCache
6
4
  def initialize(capacity=20)
7
5
  super(Cache::Strategy::LRU.new(capacity))
@@ -1,15 +1,47 @@
1
- require 'wee/utils/cache'
1
+ module Wee::Utils
2
2
 
3
- module Wee::Helper
4
- def self.app_for(component, page_cache_capacity=10, id_seed=rand(1_000_000))
5
- Wee::Application.new {|app|
3
+ # [+component+]
4
+ # The component class for which to create an application object. If a
5
+ # block is given, this will be invoked to create a new component object.
6
+ #
7
+ # [+options+]
8
+ # A Hash. Following keys are accepted:
9
+ #
10
+ # [:application]
11
+ # The application class to use (default: Wee::Application)
12
+ #
13
+ # [:session]
14
+ # The session class to use (default: Wee::Session)
15
+ #
16
+ # [:page_cache_capacity]
17
+ # The size of the sessions page_store (default: 10)
18
+ #
19
+ # [:id_seed]
20
+ # Initial value of the SimpleIdGenerator (default: rand(1_000_000))
21
+ #
22
+ def self.app_for(component=nil, options={}, &block)
23
+ raise "either component or block must be given" if component.nil? and block.nil?
24
+
25
+ defaults = {
26
+ :application => Wee::Application,
27
+ :session => Wee::Session,
28
+ :page_cache_capacity => 10,
29
+ :id_seed => rand(1_000_000)
30
+ }
31
+ options = defaults.update(options)
32
+
33
+ options[:application].new {|app|
6
34
  app.default_request_handler {
7
- Wee::Session.new {|sess|
8
- sess.root_component = component.new
9
- sess.page_store = Wee::Utils::LRUCache.new(page_cache_capacity)
35
+ options[:session].new {|sess|
36
+ if component
37
+ sess.root_component = component.new
38
+ else
39
+ sess.root_component = block.call
40
+ end
41
+ sess.page_store = Wee::Utils::LRUCache.new(options[:page_cache_capacity])
10
42
  }
11
43
  }
12
- app.id_generator = Wee::SimpleIdGenerator.new(id_seed)
44
+ app.id_generator = Wee::SimpleIdGenerator.new(options[:id_seed])
13
45
  }
14
46
  end
15
47
  end