Skip to content

Commit

Permalink
find will raise errors if nil/empty
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed May 14, 2015
1 parent 00f50e3 commit 5915681
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/neo4j.rb
Expand Up @@ -16,6 +16,7 @@
require 'active_support/core_ext/class/attribute.rb'

require 'active_attr'
require 'neo4j/errors'
require 'neo4j/config'
require 'neo4j/wrapper'
require 'neo4j/active_rel/rel_wrapper'
Expand Down
12 changes: 7 additions & 5 deletions lib/neo4j/active_node/labels.rb
Expand Up @@ -85,11 +85,13 @@ module ClassMethods
def find(id)
map_id = proc { |object| object.respond_to?(:id) ? object.send(:id) : object }

if id.is_a?(Array)
find_by_ids(id.map { |o| map_id.call(o) })
else
find_by_id(map_id.call(id))
end
result = if id.is_a?(Array)
find_by_ids(id.map { |o| map_id.call(o) })
else
find_by_id(map_id.call(id))
end
fail Neo4j::RecordNotFound if result.blank?
result
end

# Finds the first record matching the specified conditions. There is no implied ordering so if order matters, you should specify it yourself.
Expand Down
2 changes: 1 addition & 1 deletion lib/neo4j/active_node/orm_adapter.rb
Expand Up @@ -28,7 +28,7 @@ def get!(id)

# Get an instance by id of the model
def get(id)
klass.find(wrap_key(id))
klass.find_by(klass.id_property_name => wrap_key(id))
end

# Find the first instance matching conditions
Expand Down
4 changes: 2 additions & 2 deletions lib/neo4j/active_node/query/query_proxy.rb
Expand Up @@ -196,8 +196,8 @@ def rel
end

def _nodeify!(*args)
other_nodes = [args].flatten.map do |arg|
(arg.is_a?(Integer) || arg.is_a?(String)) ? @model.find(arg) : arg
other_nodes = [args].flatten!.map! do |arg|
(arg.is_a?(Integer) || arg.is_a?(String)) ? @model.find_by(@model.id_property_name => arg) : arg
end.compact

if @model && other_nodes.any? { |other_node| !other_node.is_a?(@model) }
Expand Down
10 changes: 10 additions & 0 deletions lib/neo4j/errors.rb
@@ -0,0 +1,10 @@
module Neo4j
# Neo4j.rb Errors
# Generic Neo4j.rb exception class.
class Neo4jrbError < StandardError
end

# Raised when Neo4j.rb cannot find record by given id.
class RecordNotFound < Neo4jrbError
end
end
12 changes: 12 additions & 0 deletions spec/e2e/label_spec.rb
Expand Up @@ -233,6 +233,12 @@ class Foo2 < Foo1
it 'by object' do
clazz.find(object1).should eq(object1)
end

context 'with no results' do
it 'raises an error' do
expect { clazz.find(8_675_309) }.to raise_error { Neo4j::RecordNotFound }
end
end
end

describe 'finding multiple records' do
Expand All @@ -243,6 +249,12 @@ class Foo2 < Foo1
it 'by object' do
clazz.find([object1, object2]).to_set.should eq([object1, object2].to_set)
end

context 'with no results' do
it 'raises an error' do
expect { clazz.find[8_675_309] }.to raise_error { Neo4j::RecordNotFound }
end
end
end
end
end

0 comments on commit 5915681

Please sign in to comment.