Skip to content

Commit

Permalink
Implemented hash method so that it will work with Sets [#160 state:re…
Browse files Browse the repository at this point in the history
…solved]
  • Loading branch information
andreasronge committed Apr 16, 2011
1 parent 284b0a7 commit 1edef22
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/neo4j/rails/model.rb
Expand Up @@ -15,6 +15,10 @@ def id
neo_id.nil? ? nil : neo_id.to_s
end

def hash
persisted? ? _java_entity.neo_id.hash : super
end

def to_param
persisted? ? neo_id.to_s : nil
end
Expand Down
4 changes: 4 additions & 0 deletions lib/neo4j/rails/relationship.rb
Expand Up @@ -35,6 +35,10 @@ def id
_java_rel.nil? || neo_id.nil? ? nil : neo_id.to_s
end

def hash
persisted? ? _java_entity.neo_id.hash : super
end

def start_node
@start_node ||= _java_rel && _java_rel.start_node
end
Expand Down
42 changes: 42 additions & 0 deletions spec/rails/property_spec.rb
Expand Up @@ -47,6 +47,48 @@ class BooleanProperties < Neo4j::Rails::Model
end



describe "hash" do
class UniqueHashModel < Neo4j::Rails::Model
end

class UniqueHashRelationship < Neo4j::Rails::Relationship
end

it "has a unique hash for persisted models" do
x = UniqueHashModel.create
y = UniqueHashModel.first
x.hash.should == y.hash
x.hash.should be_kind_of(Fixnum)
end

it "has not unique hash for not persisted models" do
x = UniqueHashModel.new
y = UniqueHashModel.new
x.hash.should_not == y.hash
x.hash.should be_kind_of(Fixnum)
end

it "has a unique hash for persisted relationships" do
a = Neo4j::Model.create
b = Neo4j::Model.create
x = UniqueHashRelationship.create(:foo, a, b)
y = UniqueHashRelationship.first
x.hash.should == y.hash
x.hash.should be_kind_of(Fixnum)
end

it "has not unique hash for not persisted relationships" do
a = Neo4j::Model.create
b = Neo4j::Model.create
x = UniqueHashRelationship.new(:foo, a, b)
y = UniqueHashRelationship.new(:foo, a, b)
x.hash.should_not == y.hash
x.hash.should be_kind_of(Fixnum)
end

end

describe BooleanProperties do

[true, '1'].each do |value|
Expand Down

0 comments on commit 1edef22

Please sign in to comment.