Added #wrap method

This commit is contained in:
Todd Werth
2013-08-25 20:53:22 -07:00
parent e9fd264dc5
commit 9810e08dc5
2 changed files with 65 additions and 0 deletions

View File

@@ -61,6 +61,27 @@ module RubyMotionQuery
@_selected
end
# Wraps 1 or more views in an rmq instance.
#
# Normally you select a view or views using rmq(my_view). Which doesn't
# work if you have an instance of a RMQ, in which case it isn't a method.
# In some cases you want to save an instance of rmq and use it like the rmq
# method, for this you'd use #wrap
#
# @example
# q = RubyMotionQuery::RMQ.new
#
# # Bad
# q(my_view).view_controller
#
# # Good
# q.wrap(my_view).view_controller
def wrap(*views)
views.flatten!
views.select!{ |v| v.is_a?(UIView) }
RMQ.create_with_array_and_selectors(views, views, views.first)
end
# The view(s) this rmq was derived from
#
# @return [Array]

View File

@@ -200,5 +200,49 @@ describe 'base' do
a[1].is_a?(UILabel).should == true
end
describe 'should wrap' do
before do
@vc = UIViewController.alloc.init
@my_view = @vc.rmq.append(UIView).get
@my_view_2 = @vc.rmq.append(UIView).get
end
it 'a view(s) in an existing view tree with a new rmq instance' do
q = RubyMotionQuery::RMQ.new
q.wrap(UIView).length.should == 0
q.wrap(@my_view).length.should == 1
q.wrap(@my_view, @my_view_2).length.should == 2
q.wrap(@my_view).siblings.get.should == @my_view_2
q.wrap(@my_view).view_controller.should == @vc
end
it 'a view with its vc\'s rmq' do
@vc.rmq.wrap(UIView).length.should == 0
@vc.rmq.wrap(@my_view).length.should == 1
@vc.rmq.wrap(@my_view, @my_view_2).length.should == 2
@vc.rmq.wrap(@my_view).siblings.get.should == @my_view_2
@vc.rmq.wrap(@my_view).view_controller.should == @vc
# Identical to this:
@vc.rmq(@my_view).length.should == 1
@vc.rmq(@my_view, @my_view_2).length.should == 2
@vc.rmq(@my_view).siblings.get.should == @my_view_2
@vc.rmq(@my_view).view_controller.should == @vc
# But this will be different because it selects all UIViews
@vc.rmq(UIView).length.should != 0
end
it 'a view not in a view tree with a new rmq instance' do
q = RubyMotionQuery::RMQ.new
my_view_3 = UIView.alloc.initWithFrame(CGRectZero)
q.wrap(UIView).length.should == 0
q.wrap(my_view_3).length.should == 1
q.wrap(my_view_3).siblings.length.should == 0
q.wrap(my_view_3).view_controller.nil?.should == true
end
end
end