Permalink
Browse files

moved #widget behaviour to Apotomo::WidgetShortcuts::FactoryProxy.

  • Loading branch information...
apotonick committed Oct 11, 2011
1 parent 4cb2d02 commit f453aa22d2f13ba7ea2c4222ba903a9e34cf333f
Showing with 83 additions and 69 deletions.
  1. +2 −9 lib/apotomo/widget/tree_node.rb
  2. +38 −11 lib/apotomo/widget_shortcuts.rb
  3. +43 −49 test/widget_shortcuts_test.rb
@@ -1,6 +1,7 @@
module Apotomo
module TreeNode
include Enumerable
+ include Apotomo::WidgetShortcuts::DSL
attr_reader :name, :childrenHash
attr_accessor :parent
@@ -19,18 +20,10 @@ def to_s
" Children: #{children.length}" + " Total Nodes: #{size}"
end
- # Convenience synonym for Tree#add method.
- # This method allows a convenient method to add
- # children hierarchies in the tree.
- # E.g. root << child << grand_child
- def <<(child)
- constant_for(child[:class]).new(self, child[:id], child[:options]) if child.is_a?(Hash) # TODO: move to WidgetCreationProxy.
- end
-
def add_widget(child) # TODO: rename #add, make private
raise "Child already added" if @childrenHash.has_key?(child.name)
- @childrenHash[child.widget_id] = child
+ @childrenHash[child.widget_id] = child
@children << child
child.parent = self
@@ -1,13 +1,15 @@
module Apotomo
- # Shortcut methods for creating widget trees.
+ # Create widget trees using the #widget DSL.
module WidgetShortcuts
# Shortcut for creating an instance of <tt>class_name+"_widget"</tt> named +id+. Yields self.
+ # Note that this creates a proxy object, only. The actual widget is built not until you added
+ # it, e.g. using #<<.
#
# Example:
#
- # widget(:comments)
+ # root << widget(:comments)
#
- # will create a +CommentsWidget+ with id :comments.
+ # will create a +CommentsWidget+ with id :comments attached to +root+.
#
# widget(:comments, 'post-comments', :user => current_user)
#
@@ -16,19 +18,44 @@ module WidgetShortcuts
# You can also use namespaces.
#
# widget('jquery/tabs', 'panel')
- def widget(prefix, *args)
- options = args.extract_options!
- id = args.shift || prefix
-
- return {:class => prefix, :id => id, :options => options}
- constant_for(prefix).new(parent_controller, id, options).tap do |object|
- yield object if block_given?
- end
+ #
+ # Add a block if you need to grab the created widget right away.
+ #
+ # root << widget(:comments) do |comments|
+ # comments.markdown!
+ # end
+ #
+ # Using #widget is just a shortcut, you can always use the constructor as well.
+ #
+ # CommentsWidget.new(root, :comments)
+ def widget(*args, &block)
+ FactoryProxy.new(*args, &block)
end
+ class FactoryProxy
+ def initialize(prefix, *args, &block)
+ options = args.extract_options!
+ id = args.shift || prefix
+
+ @prefix, @id, @options, @block = prefix, id, options, block
+ end
+
+ def build(parent)
+ widget = constant_for(@prefix).new(parent, @id, @options)
+ @block.call(widget) if @block
+ widget
+ end
+
private
def constant_for(class_name) # TODO: use Cell.class_from_cell_name.
"#{class_name}_widget".classify.constantize
end
+ end
+
+ module DSL
+ def <<(child)
+ child.build(self)
+ end
+ end
end
end
@@ -4,73 +4,67 @@ class MumWidget < MouseWidget; end
class MouseTabsWidget;end
class WidgetShortcutsTest < Test::Unit::TestCase
- include Apotomo::TestCaseMethods::TestController
-
- context "#constant_for" do
- should "constantize symbols" do
- assert_equal MumWidget, constant_for(:mum)
+ context "FactoryProxy" do
+ setup do
+ @factory = Apotomo::WidgetShortcuts::FactoryProxy
end
- should "not try to singularize the widget class" do
- assert_equal MouseTabsWidget, constant_for(:mouse_tabs)
- end
- end
-
- context "#widget" do
- context "with all arguments" do
+ context "#constant_for" do
setup do
- @mum = widget(:mum, 'mum', :eating, :color => 'grey', :type => :hungry)
+ @dsl = @factory.new(:class, :id)
end
- should "create a MumWidget instance" do
- assert_kind_of MumWidget, @mum
- assert_equal 'mum', @mum.name
+ should "constantize symbols" do
+ assert_equal MouseWidget, @dsl.send(:constant_for, :mouse)
end
-
- should "accept options" do
- assert_equal({:color => "grey", :type => :hungry}, @mum.options)
- end
- end
-
- context "with 3 arguments and no options" do
- should "not set options" do
- @mum = widget(:mum, 'mum', :squeak)
- assert_kind_of MumWidget, @mum
- assert_equal 'mum', @mum.name
- assert_equal({}, @mum.options)
+
+ should "not try to singularize the widget class" do
+ assert_equal MouseTabsWidget, @dsl.send(:constant_for, :mouse_tabs)
end
end
- context "with class and id" do
+ context "#widget and #<<" do
setup do
- @mum = widget(:mum, 'mummy')
+ @root = Apotomo::Widget.new(nil, :root)
end
-
- should "create a MumWidget instance" do
- assert_kind_of MumWidget, @mum
- assert_equal 'mummy', @mum.name
+
+ context "with all arguments" do
+ should "create a MumWidget instance with options" do
+ proxy = widget(:mum, :mummy, :eating, :color => 'grey', :type => :hungry)
+ @root << proxy
+
+ assert_kind_of MumWidget, @root[:mummy]
+ assert_equal :mummy, @root[:mummy].name
+ assert_equal({:color => "grey", :type => :hungry}, @root[:mummy].options)
+ end
end
- end
- context "with class, only" do
- setup do
- @mum = widget(:mum)
+ should "not set options with 2 arguments" do
+ @root << widget(:mum, :mummy)
+ @mum = @root[:mummy]
+
+ assert_kind_of MumWidget, @mum
+ assert_equal :mummy, @mum.widget_id
+ assert_equal({}, @mum.options)
end
-
- should "create a MumWidget instance named :mum" do
+
+ should "set defaults with prefix, only" do
+ @root << widget(:mum)
+ @mum = @root[:mum]
+
assert_kind_of MumWidget, @mum
assert_equal :mum, @mum.name
+ assert_equal({}, @mum.options)
end
- end
-
-
- should "yield itself" do
- @mum = widget(:mum, 'mum') do |mum|
- assert_kind_of MumWidget, mum
- mum << widget(:mum, 'kid')
+
+ should "yield itself" do
+ ficken = widget(:mum) do |mum|
+ mum << widget(:mouse, :kid)
+ end
+ @root << ficken
+ assert_equal 2, @root[:mum].size
+ assert_kind_of MouseWidget, @root[:mum][:kid]
end
- assert_equal 2, @mum.size
- assert_kind_of MumWidget, @mum['kid']
end
end
end

0 comments on commit f453aa2

Please sign in to comment.