From 707fcf57ecb78cda0b7e53eae963d764a36af194 Mon Sep 17 00:00:00 2001 From: Ryan Linton Date: Tue, 17 Sep 2013 11:05:04 -0700 Subject: [PATCH 1/2] make add_to and remove accept an array of elements --- lib/ProMotion/view/styling.rb | 41 ++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/ProMotion/view/styling.rb b/lib/ProMotion/view/styling.rb index 48325d7..ce5f5d1 100644 --- a/lib/ProMotion/view/styling.rb +++ b/lib/ProMotion/view/styling.rb @@ -1,7 +1,7 @@ module ProMotion module Styling include Conversions - + def set_attributes(element, args = {}) args.each { |k, v| set_attribute(element, k, v) } element @@ -64,33 +64,34 @@ module ProMotion end nil end - + def add(element, attrs = {}) add_to view_or_self, element, attrs end alias :add_element :add alias :add_view :add - def remove(element) - element.removeFromSuperview - element = nil + def remove(elements) + Array(elements).each(&:removeFromSuperview) end alias :remove_element :remove alias :remove_view :remove - def add_to(parent_element, element, attrs = {}) - parent_element.addSubview element - if attrs && attrs.length > 0 - set_attributes(element, attrs) - set_easy_attributes(parent_element, element, attrs) + def add_to(parent_element, elements, attrs = {}) + Array(elements).each do |element| + parent_element.addSubview element + if attrs && attrs.length > 0 + set_attributes(element, attrs) + set_easy_attributes(parent_element, element, attrs) + end end - element + elements end - + def view_or_self self.respond_to?(:view) ? self.view : self end - + # These three color methods are stolen from BubbleWrap. def rgb_color(r,g,b) rgba_color(r,g,b,1) @@ -103,7 +104,7 @@ module ProMotion def hex_color(str) hex_color = str.gsub("#", "") - case hex_color.size + case hex_color.size when 3 colors = hex_color.scan(%r{[0-9A-Fa-f]}).map{ |el| (el * 2).to_i(16) } when 6 @@ -111,16 +112,16 @@ module ProMotion else raise ArgumentError end - + if colors.size == 3 rgb_color(colors[0], colors[1], colors[2]) else raise ArgumentError - end + end end - + protected - + def map_resize_symbol(symbol) @_resize_symbols ||= { left: UIViewAutoresizingFlexibleLeftMargin, @@ -128,10 +129,10 @@ module ProMotion top: UIViewAutoresizingFlexibleTopMargin, bottom: UIViewAutoresizingFlexibleBottomMargin, width: UIViewAutoresizingFlexibleWidth, - height: UIViewAutoresizingFlexibleHeight + height: UIViewAutoresizingFlexibleHeight } @_resize_symbols[symbol] || symbol end - + end end From ea513edc5685f25a406d563dba0f39c79769ae5c Mon Sep 17 00:00:00 2001 From: Ryan Linton Date: Tue, 17 Sep 2013 11:45:56 -0700 Subject: [PATCH 2/2] add tests --- spec/unit/screen_helpers_spec.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/spec/unit/screen_helpers_spec.rb b/spec/unit/screen_helpers_spec.rb index 4b41caa..895a0f8 100644 --- a/spec/unit/screen_helpers_spec.rb +++ b/spec/unit/screen_helpers_spec.rb @@ -24,12 +24,29 @@ describe "screen helpers" do @screen.view.subviews.count.should == 0 end + it "should let you remove an array of subviews" do + subview_a = UIView.alloc.initWithFrame CGRectZero + subview_b = UIView.alloc.initWithFrame CGRectZero + @screen.view.addSubview subview_a + @screen.view.addSubview subview_b + @screen.remove [subview_a, subview_b] + @screen.view.subviews.count.should == 0 + end + it "should add a subview to another element" do sub_subview = UIView.alloc.initWithFrame CGRectZero @screen.add_to @subview, sub_subview @subview.subviews.include?(sub_subview).should == true end + it "should add an array of subviews to another element" do + sub_subview_a = UIView.alloc.initWithFrame CGRectZero + sub_subview_b = UIView.alloc.initWithFrame CGRectZero + @screen.add_to @subview, [sub_subview_a, sub_subview_b] + @subview.subviews.include?(sub_subview_a).should == true + @subview.subviews.include?(sub_subview_b).should == true + end + it "should add a subview to another element with attributes" do sub_subview = UIView.alloc.initWithFrame CGRectZero @screen.add_to @subview, sub_subview, { backgroundColor: UIColor.redColor } @@ -153,7 +170,7 @@ describe "screen helpers" do screen = @screen.open BasicScreen, modal: true screen.should.be.kind_of BasicScreen end - + it "should present a modal screen if open_modal is used" do @screen.mock!(:present_modal_view_controller) do |screen, animated| screen.should.be.instance_of BasicScreen @@ -189,7 +206,7 @@ describe "screen helpers" do screen = @screen.open BasicScreen screen.should.be.kind_of BasicScreen end - + it "should ignore its own navigation controller if current screen has a navigation controller" do basic = BasicScreen.new(nav_bar: true) # creates a dangling nav_bar that will be discarded. screen = @screen.open basic