Added find_or_append

This commit is contained in:
Jamon Holmgren
2015-03-20 19:54:57 -07:00
parent ef3998f7bb
commit 47728aeb2a
4 changed files with 68 additions and 2 deletions

View File

@@ -145,7 +145,7 @@ module RubyMotionQuery
view.rmq_style_applied
rescue NoMethodError => e
if e.message =~ /.*#{style_name.to_s}.*/
puts "\n[RMQ ERROR] style_name :#{style_name} doesn't exist for a #{view.class.name}. Add 'def #{style_name}(st)' to #{stylesheet.class.name} class\n\n"
$stderr.puts "\n[RMQ ERROR] style_name :#{style_name} doesn't exist for a #{view.class.name}. Add 'def #{style_name}(st)' to #{stylesheet.class.name} class\n\n"
else
raise e
end

View File

@@ -111,6 +111,34 @@ module RubyMotionQuery
append(view_or_constant, style, opts).get
end
# Same as append, but will look for a view with the same name and reapply styles
# to it if it finds one. If it doesn't, it'll append as normal.
#
# @example
# @my_button = rmq.find_or_append(UIButton, :my_button)
# @my_button = rmq.find_or_append(UIButton, :my_button) # Only one created
def find_or_append(view_or_constant, style=nil, opts = {}, &block)
if style && (q = self.find(style)) && q.length > 0
q.reapply_styles
block.call q if block
q
else
append(view_or_constant, style, opts, &block)
end
end
# Same as append!, but will look for a view with the same name and reapply styles
# to it if it finds one. If it doesn't, it'll append! as normal.
#
# @example
# @my_button = rmq.find_or_append!(UIButton, :my_button)
# @my_button = rmq.find_or_append!(UIButton, :my_button) # Only one created
def find_or_append!(view_or_constant, style=nil, opts = {}, &block)
find_or_append(view_or_constant, style, opts).get.tap do |q|
block.call q
end
end
# Just like append, but inserts the view at index 0 of the subview array
#
# @return [RMQ]

View File

@@ -334,6 +334,44 @@ describe 'subviews' do
block_called.should == true
end
end
describe 'find_or_append' do
it 'appends if none existing' do
@vc.view.subviews.length.should == 0
view = @vc.rmq.find_or_append(UIView).get
@vc.view.subviews.length.should == 1
@vc.view.subviews.first.should == view
end
it 'finds if existing' do
@vc.rmq.stylesheet = StyleSheetForSubviewsTests
@vc.view.subviews.length.should == 0
existing_view = @vc.rmq.append(UIView, :my_style).get
@vc.view.subviews.length.should == 1
found_view = @vc.rmq.find_or_append(UIView, :my_style).get
@vc.view.subviews.length.should == 1
found_view.should == existing_view
end
end
describe 'find_or_append!' do
it 'appends if none existing' do
@vc.view.subviews.length.should == 0
view = @vc.rmq.find_or_append!(UIView)
@vc.view.subviews.length.should == 1
@vc.view.subviews.first.should == view
end
it 'finds if existing' do
@vc.rmq.stylesheet = StyleSheetForSubviewsTests
@vc.view.subviews.length.should == 0
existing_view = @vc.rmq.append!(UIView, :my_style)
@vc.view.subviews.length.should == 1
found_view = @vc.rmq.find_or_append!(UIView, :my_style)
@vc.view.subviews.length.should == 1
found_view.should == existing_view
end
end
end
class StyleSheetForSubviewsTests < RubyMotionQuery::Stylesheet

View File

@@ -1,4 +1,4 @@
describe 'transversing' do
describe 'traversing' do
before do
@vc = UIViewController.alloc.init
@root_view = @vc.view