Permalink
Browse files

Add filter_by to DataStoreProxy

  • Loading branch information...
1 parent db1363b commit 9728dfddf7aaffa3e09e747d18b1fd6b0ad4e44f @ijcd committed Mar 2, 2012
Showing with 46 additions and 7 deletions.
  1. +8 −7 lib/hoodwink/data_store_proxy.rb
  2. +38 −0 spec/lib/hoodwink/data_store_proxy_spec.rb
@@ -9,21 +9,22 @@ def initialize(resource_name, datastore, request)
@datastore = datastore
@request = request
end
+
+ def filter_by(model)
+ model
+ end
- def segment_params
+ def segments
request.segment_params
end
- def url_params
- request.uri.query.inject({}) {|hsh,i| sides=i.split("="); hsh[sides[0]]=sides[1]; hsh}
- end
-
def find_all
- @datastore.find_all(@resource_name)
+ @datastore.find_all(@resource_name).select{|m| filter_by(m)}
end
def find(id)
- @datastore.find(@resource_name, id)
+ found = @datastore.find(@resource_name, id)
+ filter_by(found) ? found : nil
end
def create(resource_hash)
@@ -0,0 +1,38 @@
+require File.join(File.dirname(__FILE__), '../../spec_helper.rb')
+
+describe Hoodwink::DataStoreProxy do
+ subject { Hoodwink::DataStoreProxy.new(resource_name, datastore, request) }
+
+ let(:resource_name) { "resource_name" }
+ let(:request) { double("request") }
+ let(:datastore) { double("datastore", :find_all => [result, result, result], :find => result) }
+ let(:result) { {} }
+
+ describe "#find_all" do
+ it "should call datastore.find_all with the current resource_name" do
+ datastore.should_receive(:find_all).with(resource_name)
+ subject.find_all
+ end
+
+ it "should call filter_by to filter the results" do
+ datastore.should_receive(:find_all).with(resource_name)
+ subject.should_receive(:filter_by).with(result).exactly(3)
+ subject.find_all
+ end
+ end
+
+ describe "#find" do
+ it "should call datastore.find with the current resource_name" do
+ datastore.should_receive(:find).with(resource_name, result)
+ subject.find(result)
+ end
+
+ it "should call filter_by to filter the results" do
+ datastore.should_receive(:find).with(resource_name, 5).and_return(result)
+ subject.should_receive(:filter_by).with(result).exactly(1)
+ subject.find(5)
+ end
+ end
+
+end
+

0 comments on commit 9728dfd

Please sign in to comment.