From 47728aeb2a2315a3789b765687b0fa11cc34aaba Mon Sep 17 00:00:00 2001 From: Jamon Holmgren Date: Fri, 20 Mar 2015 19:54:57 -0700 Subject: [PATCH] Added find_or_append --- motion/ruby_motion_query/stylesheet.rb | 2 +- motion/ruby_motion_query/subviews.rb | 28 +++++++++++++++++++ spec/subviews.rb | 38 ++++++++++++++++++++++++++ spec/traversing.rb | 2 +- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/motion/ruby_motion_query/stylesheet.rb b/motion/ruby_motion_query/stylesheet.rb index 9e57f46..fc97ed3 100644 --- a/motion/ruby_motion_query/stylesheet.rb +++ b/motion/ruby_motion_query/stylesheet.rb @@ -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 diff --git a/motion/ruby_motion_query/subviews.rb b/motion/ruby_motion_query/subviews.rb index 3bee715..890f153 100644 --- a/motion/ruby_motion_query/subviews.rb +++ b/motion/ruby_motion_query/subviews.rb @@ -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] diff --git a/spec/subviews.rb b/spec/subviews.rb index e4c470f..f7332c0 100644 --- a/spec/subviews.rb +++ b/spec/subviews.rb @@ -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 diff --git a/spec/traversing.rb b/spec/traversing.rb index 479a594..16c842e 100644 --- a/spec/traversing.rb +++ b/spec/traversing.rb @@ -1,4 +1,4 @@ -describe 'transversing' do +describe 'traversing' do before do @vc = UIViewController.alloc.init @root_view = @vc.view