Permalink
Browse files

Allow passing options to adapter#read from query methods.

  • Loading branch information...
1 parent c3d8d62 commit c0d9d8e3b328dc0af3e646a20c730ff0f70e21f2 @jnunemaker committed Nov 16, 2012
Showing with 33 additions and 16 deletions.
  1. +2 −2 lib/toy/exceptions.rb
  2. +4 −3 lib/toy/identity_map.rb
  3. +14 −10 lib/toy/querying.rb
  4. +13 −1 spec/toy/querying_spec.rb
View
@@ -12,8 +12,8 @@ def initialize(record)
end
class NotFound < Error
- def initialize(id)
- super("Could not find document with id: #{id.inspect}")
+ def initialize(*args)
+ super("Could not find document with: #{args.inspect}")
end
end
View
@@ -44,11 +44,12 @@ def self.without
end
module ClassMethods
- def get(id)
- get_from_identity_map(id) || super
+ def get(*args)
+ get_from_identity_map(*args) || super
end
- def get_from_identity_map(id)
+ def get_from_identity_map(*args)
+ id = args.first
IdentityMap.repository[id] if IdentityMap.enabled?
end
private :get_from_identity_map
View
@@ -3,17 +3,19 @@ module Querying
extend ActiveSupport::Concern
module ClassMethods
- def get(id)
- if (attrs = adapter.read(id))
+ def get(*args)
+ id = args.first
+ if (attrs = adapter.read(*args))
load(id, attrs)
end
end
alias_method :read, :get
alias_method :find, :get
- def get!(id)
- get(id) || raise(Toy::NotFound.new(id))
+ def get!(*args)
+ id = args.first
+ get(*args) || raise(Toy::NotFound.new(id))
end
alias_method :read!, :get!
@@ -31,16 +33,18 @@ def get_multiple(*ids)
alias_method :read_multiple, :get_multiple
alias_method :find_multiple, :get_multiple
- def get_or_new(id)
- get(id) || new(:id => id)
+ def get_or_new(*args)
+ id = args.first
+ get(*args) || new(:id => id)
end
- def get_or_create(id)
- get(id) || create(:id => id)
+ def get_or_create(*args)
+ id = args.first
+ get(*args) || create(:id => id)
end
- def key?(id)
- adapter.key?(id)
+ def key?(*args)
+ adapter.key?(*args)
end
alias :has_key? :key?
View
@@ -16,6 +16,12 @@
it "returns nil if not found" do
User.send(method_name, '1').should be_nil
end
+
+ it "passes all arguments to adapter read" do
+ john = User.create(:name => 'John')
+ User.adapter.should_receive(:read).with(john.id, my: 'options').and_return({'name' => 'John'})
+ User.send(method_name, john.id, my: 'options')
+ end
end
shared_examples_for "adapter read and load instance with bang" do |method_name|
@@ -27,7 +33,13 @@
it "raises not found exception if not found" do
lambda {
User.send(method_name, '1')
- }.should raise_error(Toy::NotFound, 'Could not find document with id: "1"')
+ }.should raise_error(Toy::NotFound, 'Could not find document with: ["1"]')
+ end
+
+ it "passes all arguments to adapter read" do
+ john = User.create(:name => 'John')
+ User.adapter.should_receive(:read).with(john.id, my: 'options').and_return({'name' => 'John'})
+ User.send(method_name, john.id, my: 'options')
end
end

0 comments on commit c0d9d8e

Please sign in to comment.