Update to use more efficient array_all_members_of? method.

This commit is contained in:
Mark Rickert
2014-09-15 08:46:52 -05:00
parent 831fa8201d
commit 0e9b11244d
3 changed files with 8 additions and 9 deletions

View File

@@ -132,7 +132,7 @@ module ProMotion
def update_table_data(args = {})
# Try and detect if the args param is a NSIndexPath or an array of them
args = { index_paths: args } if args.is_a?(NSIndexPath) || (args.is_a?(Array) && array_all_members_of(args, NSIndexPath))
args = { index_paths: args } if args.is_a?(NSIndexPath) || (args.is_a?(Array) && array_all_members_of?(args, NSIndexPath))
self.update_table_view_data(self.table_data, args)
self.promotion_table_data.search(search_string) if searching?

View File

@@ -10,9 +10,8 @@ module ProMotion
end
# Determines if all members of an array are a certain class
def array_all_members_of(arr, kalss)
unique_classes = arr.map(&:class).uniq
(unique_classes.count == 1 && unique_classes.first == kalss)
def array_all_members_of?(arr, kalss)
arr.select{ |e| e.is_a?(klass) }.length == arr.length
end
end
end

View File

@@ -23,12 +23,12 @@ describe "PM::Table utils" do
end
it "should properly determine if all members of an array are the same class" do
@subject.array_all_members_of([1, 2, 3, 4], Fixnum).should == true
@subject.array_all_members_of(["string", 'string2'], String).should == true
@subject.array_all_members_of([:sym1, :sym2, :sym3], Symbol).should == true
@subject.array_all_members_of?([1, 2, 3, 4], Fixnum).should == true
@subject.array_all_members_of?(["string", 'string2'], String).should == true
@subject.array_all_members_of?([:sym1, :sym2, :sym3], Symbol).should == true
@subject.array_all_members_of([1, 2, 3, 4, 'String'], Fixnum).should == false
@subject.array_all_members_of([4.4, 2], Fixnum).should == false
@subject.array_all_members_of?([1, 2, 3, 4, 'String'], Fixnum).should == false
@subject.array_all_members_of?([4.4, 2], Fixnum).should == false
end
end