Skip to content

Commit

Permalink
Impl. generation of has_n, _rels method - Access declared/prefixed re…
Browse files Browse the repository at this point in the history
…lationships for has_n [#104 state:open]
  • Loading branch information
andreas committed Jan 30, 2010
1 parent dc66f3c commit 7cde25d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 18 deletions.
13 changes: 10 additions & 3 deletions lib/neo4j/mixins/node_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def init_with_node(java_node) # :nodoc:
end

def init_with_hash(hash)
hash.each_pair{|k,v| self[k] = v}
hash.each_pair{|k, v| self[k] = v}
end

# Returns the org.neo4j.graphdb.Node wrapped object
Expand Down Expand Up @@ -132,7 +132,7 @@ def value_object
def equal?(o)
eql?(o)
end

def eql?(o)
o.kind_of?(NodeMixin) && o._java_node == @_java_node
end
Expand Down Expand Up @@ -454,7 +454,7 @@ def index_relationship(rel_name, prop) # :nodoc:
clazz = dsl.to_class || node.class
namespace_type = clazz.decl_relationships[dsl.to_type].namespace_type
end

# add index on the trigger class and connect it to the updater_clazz
# (a trigger may cause an update of the index using the Indexer specified on the updater class)
trigger_clazz.indexer.add_index_in_relationship_on_property(updater_clazz, rel_name, rel_type, prop, namespace_type.to_sym)
Expand Down Expand Up @@ -504,6 +504,13 @@ def #{rel_type}(&block)
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
Relationships::HasN.new(self, dsl, &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)
end}, __FILE__, __LINE__)

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

Expand Down
4 changes: 4 additions & 0 deletions lib/neo4j/relationships/decl_relationship_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def namespace_type
end


def direction
(outgoing?)? :outgoing : :incoming
end

def outgoing?
@outgoing
end
Expand Down
12 changes: 7 additions & 5 deletions lib/neo4j/relationships/relationship_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ class RelationshipDSL
include Enumerable
attr_reader :node

def initialize(node, direction = :outgoing)
@raw = false
def initialize(node, direction = :outgoing, type = nil)
@raw = false
@type = type
@node = node

case direction
when :outgoing
outgoing
outgoing(type)
when :incoming
incoming
incoming(type)
when :both
both
both(type)
end
end

Expand Down
86 changes: 76 additions & 10 deletions test/neo4j/has_n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'spec_helper'



describe "Neo4j::NodeMixin#has_n " do
class ExA
include Neo4j::NodeMixin
Expand All @@ -29,20 +28,87 @@ class ExB


describe "(rel).to(class)" do

it "should generate outgoing relationships with prefix 'class#rel'" do
before (:each) do
# given
ExA.has_n(:foo).to(ExB)
@node = ExA.new
end

# when
ex1 = ExA.new
ex1.foo << ExB.new
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 be of type Enumerable" do
@node.foo.should be_kind_of(Enumerable)
end

it "should contain all nodes that has been added using the << operator" do
a = Neo4j::Node.new
b = Neo4j::Node.new
@node.foo << a << b

# then
@node.foo.should include(a, b)
end
end

it "should generate method 'rel'_rels" do
# then
ex1.rel?('ExB#foo').should be_true
@node.should respond_to(:foo_rels)
end

describe "generated method 'rel'_rels" do
it "should be of type Enumerable" do
@node.foo_rels.should be_kind_of(Enumerable)
end

it "should return the relationship between the nodes" do
a = Neo4j::Node.new
@node.foo << a

# then
[*@node.foo_rels].size.should == 1
rel = @node.foo_rels.first
rel.start_node.should == @node
rel.end_node.should == a
end

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

# then
[*@node.foo_rels].size.should == 1
wrong_rel = @node.rel(:baaz)
right_rel = @node.rel("ExB#foo")

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

it "should include all the relationships of the declared has_n type" do
a = Neo4j::Node.new
b = Neo4j::Node.new
c = Neo4j::Node.new
@node.foo << a << b << c
r_a = a.rel("ExB#foo", :incoming)
r_b = a.rel("ExB#foo", :incoming)
r_c = a.rel("ExB#foo", :incoming)

# then
@node.foo_rels.should include(r_a, r_b, r_c)
end
end
end

describe "(rel).from(class)" do
Expand All @@ -58,7 +124,7 @@ class ExB

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

describe "(rel).from(class, rel2)" do
Expand All @@ -85,7 +151,7 @@ class ExB
describe '<< operator' do

before(:all) do
undefine_class :TestNode # make sure it is not already defined
undefine_class :TestNode # make sure it is not already defined

class TestNode
include Neo4j::NodeMixin
Expand Down Expand Up @@ -143,7 +209,7 @@ class TestNode


it "should be allowed in subclasses" do
undefine_class :SubNode # make sure it is not already defined
undefine_class :SubNode # make sure it is not already defined
class SubNode < TestNode;
end
sub = SubNode.new
Expand Down

0 comments on commit 7cde25d

Please sign in to comment.