Skip to content
Browse files

Use ActiveSupport's Hash#slice for collections - use values_at to ret…

…rieve ordered values sans keys
  • Loading branch information...
1 parent 65a9ec9 commit 3ea4e5b4b78e9598204e0efb9335aaf3e20678d5 @ravigadad ravigadad committed Mar 27, 2012
View
17 lib/kookaburra/mental_model.rb
@@ -66,23 +66,6 @@ def ===(other)
self.object_id == other.object_id
end
- # Returns the values of multiple keys from the collection.
- #
- # Unlike the `Hash#slice` implementation provided by ActiveSupport, this
- # method returns an array of the values rather than a Hash.
- #
- # @param keys a list of keys to fetch from the collection.
- #
- # @return [Array] the values matching the specified index keys
- #
- # @raise [Kookaburra::UnknownKeyError] if any of the specified keys have
- # not been set
- def slice(*keys)
- results = keys.map do |key|
- self[key]
- end
- end
-
# Deletes a key/value pair from the collection, and persists the deleted pair
# in a subcollection.
#
View
6 spec/integration/test_a_rack_application_spec.rb
@@ -336,13 +336,13 @@ def delete_widget(name)
ui.sign_in(:bob)
ui.view_widget_list
- ui.widget_list.widgets.should == k.get_data(:widgets).slice(:widget_a, :widget_b)
+ ui.widget_list.widgets.should == k.get_data(:widgets).values_at(:widget_a, :widget_b)
ui.create_new_widget(:widget_c, :name => 'Bar')
- ui.widget_list.widgets.should == k.get_data(:widgets).slice(:widget_a, :widget_b, :widget_c)
+ ui.widget_list.widgets.should == k.get_data(:widgets).values_at(:widget_a, :widget_b, :widget_c)
ui.delete_widget(:widget_b)
- ui.widget_list.widgets.should == k.get_data(:widgets).slice(:widget_a, :widget_c)
+ ui.widget_list.widgets.should == k.get_data(:widgets).values_at(:widget_a, :widget_c)
end
end
end
View
13 spec/kookaburra/mental_model_spec.rb
@@ -14,12 +14,21 @@
describe Kookaburra::MentalModel::Collection do
let(:collection) { Kookaburra::MentalModel::Collection.new('widgets') }
- describe '#slice' do
+ describe '#values_at' do
it 'returns an array of items matching the specified keys' do
collection[:foo] = 'foo'
collection[:bar] = 'bar'
collection[:baz] = 'baz'
- collection.slice(:foo, :baz).should == %w(foo baz)
+ collection.values_at(:foo, :baz).should == %w(foo baz)
+ end
+ end
+
+ describe '#slice' do
+ it 'returns a hash of items matching the specified keys' do
+ collection[:foo] = 'foo'
+ collection[:bar] = 'bar'
+ collection[:baz] = 'baz'
+ collection.slice(:foo, :baz).should == { :foo => 'foo', :baz => 'baz' }
end
end

0 comments on commit 3ea4e5b

Please sign in to comment.
Something went wrong with that request. Please try again.