Skip to content

Commit

Permalink
Impl. generation of has_one, _rel method - Access declared/prefixed r…
Browse files Browse the repository at this point in the history
…elationships for has_n [#104 state:open]
  • Loading branch information
andreas committed Jan 30, 2010
1 parent 7cde25d commit 87942d8
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 17 deletions.
14 changes: 11 additions & 3 deletions lib/neo4j/mixins/node_mixin.rb
Expand Up @@ -484,6 +484,14 @@ def has_one(rel_type, params = {})
r = Relationships::HasN.new(self, dsl)
[*r][0]
end}, __FILE__, __LINE__)

module_eval(%Q{
def #{rel_type}_rel
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
r = Relationships::HasN.new(self, dsl).rels
[*r][0]
end}, __FILE__, __LINE__)

decl_relationships[rel_type.to_sym] = Relationships::DeclRelationshipDsl.new(rel_type, params)
end

Expand All @@ -506,9 +514,9 @@ def #{rel_type}(&block)
end}, __FILE__, __LINE__)

module_eval(%Q{
def #{rel_type}_rels
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
Neo4j::Relationships::RelationshipDSL.new(self._java_node, dsl.direction, dsl.namespace_type)
def #{rel_type}_rels
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
Relationships::HasN.new(self, dsl).rels
end}, __FILE__, __LINE__)

decl_relationships[rel_type.to_sym] = Relationships::DeclRelationshipDsl.new(rel_type, params)
Expand Down
4 changes: 4 additions & 0 deletions lib/neo4j/relationships/has_n.rb
Expand Up @@ -30,6 +30,10 @@ def initialize(node, dsl, &filter)
end


def rels
Neo4j::Relationships::RelationshipDSL.new(@node._java_node, (@outgoing)? :outgoing : :incoming, @dsl.namespace_type)
end

# Sets the depth of the traversal.
# Default is 1 if not specified.
#
Expand Down
61 changes: 56 additions & 5 deletions test/neo4j/has_n_spec.rb
Expand Up @@ -93,7 +93,7 @@ class ExB
right_rel = @node.rel("ExB#foo")

@node.foo_rels.should_not include(wrong_rel)
@node.foo_rels.should include(right_rel)
@node.foo_rels.should include(right_rel)
end

it "should include all the relationships of the declared has_n type" do
Expand All @@ -112,11 +112,11 @@ class ExB
end

describe "(rel).from(class)" do
it "should generate outgoing relationships with prefix 'class#rel' from the other node" do
# given
before(:each) do
ExB.has_n(:baaz)
ExA.has_n(:baaz).from(ExB)

end
it "should generate method 'rel' for outgoing relationships with no prefix from the other node" do
# when
a = ExA.new
b = ExB.new
Expand All @@ -125,10 +125,20 @@ class ExB
# then
b.rel?('baaz').should be_true
end

it "should generate method 'rel'_rels returning incoming relationships" do
# when
a = ExA.new
b = ExB.new
a.baaz << b # add relationship on ExB to ExA !
[*a.baaz_rels].size.should == 1
rel = b.rel(:baaz)
a.baaz_rels.should include(rel)
end
end

describe "(rel).from(class, rel2)" do
it "should generate relationships with prefix 'class#rel2' from the other node" do
it "should generate relationships with no prefix from the other node" do
# given
ExB.has_n(:foobar)
ExA.has_n(:hoj).from(ExB, :foobar)
Expand All @@ -140,7 +150,48 @@ class ExB

# then
b.rel?('foobar').should be_true
end
end

describe "(rel).from(class) AND class has namespaced its relationship" do
before(:each) do
ExB.has_n(:baaz).to(ExA) # Add namespace ExA to the relationship
ExA.has_n(:baaz).from(ExB)
end
it "should generate method 'rel' for outgoing relationships WITH prefix from the other node" do
# when
a = ExA.new
b = ExB.new
a.baaz << b # add relationship on ExB to ExA !

# then
b.rel?('ExA#baaz').should be_true
end

it "should generate method 'rel'_rels returning incoming relationships" do
# when
a = ExA.new
b = ExB.new
a.baaz << b # add relationship on ExB to ExA !
[*a.baaz_rels].size.should == 1
rel = b.rel('ExA#baaz')
a.baaz_rels.should include(rel)
end
end

describe "(rel).from(class, rel2) and class has namespaced its relationship" do
it "should generate relationships WITH prefix from the other node" do
# given
ExB.has_n(:foobar).to(ExA) # add namespace ExA
ExA.has_n(:hoj).from(ExB, :foobar)

# when
a = ExA.new
b = ExB.new
a.hoj << b # add relationship on ExB to ExA !

# then
b.rel?('ExA#foobar').should be_true
end
end

Expand Down
98 changes: 89 additions & 9 deletions test/neo4j/has_one_spec.rb
Expand Up @@ -5,6 +5,86 @@
require 'spec_helper'


describe "Neo4j::NodeMixin#has_one " do
class ExA
include Neo4j::NodeMixin
end

class ExB
include Neo4j::NodeMixin
end

before(:all) do
start
end

before(:each) do
Neo4j::Transaction.new
end

after(:each) do
Neo4j::Transaction.finish
end


describe "(rel).to(class)" do
before (:each) do
# given
ExA.has_one(:foo).to(ExB)
@node = ExA.new
end

it "should generate method 'rel' for outgoing nodes in relationships with prefix 'class#rel'" do
@node.should respond_to(:foo)
end

describe "generated method 'rel'" do
it "should have an '=' operator for adding outgoing nodes of relationship 'class#rel'" do
# when
@node.foo = Neo4j::Node.new # it does not have to be of the specified type ExB - no validation is performed

# then
@node.rel?('ExB#foo').should be_true
end

it "should return the node" do
node = Neo4j::Node.new
@node.foo = node
@node.foo.should == node
end
end

it "should generate method 'rel'_rel" do
# then
@node.should respond_to(:foo_rel)
end

describe "generated method 'rel'_rels" do
it "should return the relationship between the nodes" do
a = Neo4j::Node.new
@node.foo = a

# then
rel = @node.foo_rel
rel.start_node.should == @node
rel.end_node.should == a
end

it "should returns relationships to nodes of the correct relationship type" do
a = Neo4j::Node.new
@node.rels.outgoing(:baaz) << Neo4j::Node.new # make sure this relationship is not returned
@node.foo = a
@node.rels.outgoing(:baaz) << Neo4j::Node.new # make sure this relationship is not returned

# then
right_rel = @node.rel("ExB#foo")
@node.foo_rel.should == right_rel
end
end
end
end


describe "Neo4j::NodeMixin#has_one " do
before(:all) do
start
Expand All @@ -26,13 +106,13 @@ class Address

end

before(:each) do
Neo4j::Transaction.new
end
before(:each) do
Neo4j::Transaction.new
end

after(:each) do
Neo4j::Transaction.finish
end
after(:each) do
Neo4j::Transaction.finish
end

it "should create a relationship with assignment like node1.rel = node2" do
# given
Expand All @@ -49,7 +129,7 @@ class Address

it "should create a relationship with the new method, like node1.rel.new(node2)" do
# given
person = Person.new
person = Person.new
address = Address.new {|a| a.city = 'malmoe'; a.road = 'my road'}

# when
Expand All @@ -63,7 +143,7 @@ class Address

it "should create a relationship with correct relationship type" do
# given
person = Person.new
person = Person.new
address = Address.new {|a| a.city = 'malmoe'; a.road = 'my road'}

# when
Expand All @@ -72,7 +152,7 @@ class Address
# then
dynamic_relationship.relationship_type.should == :"Address#address"
end

it "should should return the object using the has_one accessor" do
a = Address.new
p = Person.new
Expand Down

0 comments on commit 87942d8

Please sign in to comment.