Skip to content

Commit

Permalink
added StatefulWidget.flush_widgets and .stateful_branches_for.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Aug 2, 2010
1 parent 1eef4b1 commit afe340b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 15 deletions.
36 changes: 36 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,36 @@
GEM
specs:
actionmailer (2.3.8)
actionpack (= 2.3.8)
actionpack (2.3.8)
activesupport (= 2.3.8)
rack (~> 1.1.0)
activerecord (2.3.8)
activesupport (= 2.3.8)
activeresource (2.3.8)
activesupport (= 2.3.8)
activesupport (2.3.8)
cells (3.3.4)
mocha (0.9.8)
rake
onfire (0.1.0)
rack (1.1.0)
rails (2.3.8)
actionmailer (= 2.3.8)
actionpack (= 2.3.8)
activerecord (= 2.3.8)
activeresource (= 2.3.8)
activesupport (= 2.3.8)
rake (>= 0.8.3)
rake (0.8.7)
shoulda (2.11.1)

PLATFORMS
ruby

DEPENDENCIES
cells (= 3.3.4)
mocha
onfire
rails (= 2.3.8)
shoulda
21 changes: 21 additions & 0 deletions lib/apotomo/persistence.rb
Expand Up @@ -88,6 +88,27 @@ def thaw_from(storage)
def frozen_widget_in?(storage)
storage[:apotomo_root].kind_of? Apotomo::StatefulWidget
end

def flush_storage(storage)
storage[:apotomo_root] = nil
storage[:apotomo_widget_ivars] = nil
end

# Find the first stateful widgets on each branch from +root+.
def stateful_branches_for(root)
to_traverse = [root]
stateful_roots = []

while node = to_traverse.shift
if node.kind_of?(StatefulWidget)
stateful_roots << node and next
end
to_traverse += node.children
end

stateful_roots
end

end
end
end
1 change: 1 addition & 0 deletions lib/apotomo/request_processor.rb
Expand Up @@ -25,6 +25,7 @@ def js_generator
end

def flushed_root
StatefulWidget.flush_storage(session)
@widgets_flushed = true
widget('apotomo/stateful_widget', :content, 'root')
end
Expand Down
3 changes: 1 addition & 2 deletions test/support/test_case_methods.rb
Expand Up @@ -51,8 +51,7 @@ def self.default_url_options; {:controller => :barn}; end
@controller.session = {}
end

def hibernate_widget(widget)
session = {}
def hibernate_widget(widget, session = {})
widget.freeze_to(session)

session = Marshal.load(Marshal.dump(session))
Expand Down
29 changes: 26 additions & 3 deletions test/unit/persistence_test.rb
Expand Up @@ -14,6 +14,19 @@ def educate
def recap; render :nothing => true; end
end

context "StatefulWidget" do

context ".stateful_branches_for" do
should "provide all stateful branch-roots seen from root" do
@root = Apotomo::Widget.new('root', :eat)
@root << mum_and_kid!
@root << Apotomo::Widget.new('berry', :eat) << @jerry = mouse_mock('jerry', :eat)

assert_equal ['mum', 'jerry'], Apotomo::StatefulWidget.stateful_branches_for(@root).collect {|n| n.name}
end
end
end

context "After #hibernate_widget (request) the widget" do
should "still have the same ivars" do
@mum = PersistentMouse.new('mum', :educate)
Expand Down Expand Up @@ -52,6 +65,16 @@ def recap; render :nothing => true; end
@storage = {}
end

context "and calling #flush_storage" do
should "clear the storage from frozen data" do
@mum.freeze_to(@storage)
Apotomo::StatefulWidget.flush_storage(@storage)

assert_not @storage[:apotomo_root]
assert_not @storage[:apotomo_widget_ivars]
end
end

should "push @mum's freezable ivars to the storage when calling #freeze_ivars_to" do
@mum.freeze_ivars_to(@storage)

Expand All @@ -77,7 +100,7 @@ def recap; render :nothing => true; end

should "ignore stateless widgets when calling #freeze_to" do
@mum << Apotomo::Widget.new('berry', :eating)
@mum.freeze_to(@storage)
@mum = hibernate_widget(@mum, @storage)

assert_equal ['mum', 'mum/kid'], @storage[:apotomo_widget_ivars].keys
end
Expand Down Expand Up @@ -120,7 +143,7 @@ def recap; render :nothing => true; end
@mum << @kid = PersistentMouse.new('kid', :eating)
end

context "a single widget" do
context "a single stateful widget" do
should "provide a serialized widget on #node_dump" do
assert_equal "mum|PersistenceTest::PersistentMouse|mum", @mum.dump_node
assert_equal "kid|PersistenceTest::PersistentMouse|mum", @kid.dump_node
Expand All @@ -135,7 +158,7 @@ def recap; render :nothing => true; end
end
end

context "a widget family" do
context "a stateful widget family" do
should "provide the serialized tree on _dump" do
assert_equal "mum|PersistenceTest::PersistentMouse|mum\nkid|PersistenceTest::PersistentMouse|mum\n", @mum._dump(10)
end
Expand Down
22 changes: 12 additions & 10 deletions test/unit/request_processor_test.rb
Expand Up @@ -42,16 +42,18 @@ class RequestProcessorTest < Test::Unit::TestCase
assert_not @processor.widgets_flushed?
end

should "provide a single root for #root when :flush_tree is set" do
@processor = Apotomo::RequestProcessor.new({:apotomo_root => @mum}, :flush_widgets => true)
assert_equal 1, @processor.root.size
assert @processor.widgets_flushed?
end

should "provide a single root for #root when :version differs" do
@processor = Apotomo::RequestProcessor.new({:apotomo_root => @mum}, :version => 0)
assert_equal 1, @processor.root.size
assert @processor.widgets_flushed?
context "having a flush flag set" do
should "provide a single root for #root when :flush_widgets is set" do
@processor = Apotomo::RequestProcessor.new({:apotomo_root => @mum}, :flush_widgets => true)
assert_equal 1, @processor.root.size
assert @processor.widgets_flushed?
end

should "provide a single root for #root when :version differs" do
@processor = Apotomo::RequestProcessor.new({:apotomo_root => @mum}, :version => 0)
assert_equal 1, @processor.root.size
assert @processor.widgets_flushed?
end
end

should "provide a widget family for #root when :version is correct" do
Expand Down

0 comments on commit afe340b

Please sign in to comment.