Permalink
Browse files

Add a customized exception when trying to use an non-existent index.

  • Loading branch information...
1 parent 4d600d9 commit 040fe57643533f310bfe3465b0358e6e20907396 Damian Janowski & Michel Martens committed Nov 10, 2009
Showing with 14 additions and 5 deletions.
  1. +11 −2 lib/ohm.rb
  2. +2 −2 test/indices_test.rb
  3. +1 −1 test/validations_test.rb
View
@@ -152,7 +152,6 @@ def replace(values)
# @param value [Ohm::Model#id] Adds the id of the object if it's an Ohm::Model.
def add(model)
- raise ArgumentError unless model.id
self << model.id
end
@@ -343,6 +342,16 @@ def message
end
end
+ class IndexNotFound < Error
+ def initialize(att)
+ @att = att
+ end
+
+ def message
+ "Index #{@att.inspect} not found."
+ end
+ end
+
@@attributes = Hash.new { |hash, key| hash[key] = [] }
@@collections = Hash.new { |hash, key| hash[key] = [] }
@@counters = Hash.new { |hash, key| hash[key] = [] }
@@ -704,7 +713,7 @@ def read_remotes(attrs)
end
def self.index_key_for(att, value)
- raise ArgumentError unless indices.include?(att)
+ raise IndexNotFound, att unless indices.include?(att)
key(att, encode(value))
end
View
@@ -43,7 +43,7 @@ def write
end
should "raise if the field is not indexed" do
- assert_raises(ArgumentError) do
+ assert_raises(Ohm::Model::IndexNotFound) do
User.find(:sandunga => "foo")
end
end
@@ -155,7 +155,7 @@ def days
end
should "raise if the argument is not an index" do
- assert_raises(ArgumentError) do
+ assert_raises(Ohm::Model::IndexNotFound) do
Event.find(:timeline => 1).except(:not_an_index => 1)
end
end
View
@@ -130,7 +130,7 @@ def @event.validate
assert_unique :capacity
end
- assert_raise ArgumentError do
+ assert_raises(Ohm::Model::IndexNotFound) do
@event.valid?
end
end

0 comments on commit 040fe57

Please sign in to comment.